作者 lyh

gx

@@ -3,8 +3,10 @@ @@ -3,8 +3,10 @@
3 namespace App\Http\Controllers\File; 3 namespace App\Http\Controllers\File;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
  6 +use App\Helper\Translate;
6 use App\Models\File\ErrorFile; 7 use App\Models\File\ErrorFile;
7 use App\Models\File\File; 8 use App\Models\File\File;
  9 +use App\Models\File\Image as ImageModel;
8 use App\Models\Project\Project; 10 use App\Models\Project\Project;
9 use App\Services\AmazonS3Service; 11 use App\Services\AmazonS3Service;
10 use App\Services\CosService; 12 use App\Services\CosService;
@@ -120,7 +122,7 @@ class FileController @@ -120,7 +122,7 @@ class FileController
120 if($file_hash !== false){ 122 if($file_hash !== false){
121 return $this->response('资源',Code::SUCCESS,$this->responseData($file_hash['path'], $name)); 123 return $this->response('资源',Code::SUCCESS,$this->responseData($file_hash['path'], $name));
122 } 124 }
123 - $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 125 + $fileName = $this->getOnlyFilename($name,$param['project_id'] ?? 0);
124 //同步数据到cos 126 //同步数据到cos
125 if($this->upload_location == 0){ 127 if($this->upload_location == 0){
126 $cosService = new CosService(); 128 $cosService = new CosService();
@@ -155,6 +157,40 @@ class FileController @@ -155,6 +157,40 @@ class FileController
155 } 157 }
156 158
157 /** 159 /**
  160 + * @remark :获取唯一名称
  161 + * @name :getOnlyFilename
  162 + * @author :lyh
  163 + * @method :post
  164 + * @time :2024/4/26 16:10
  165 + */
  166 + public function getOnlyFilename($name,$project_id = 0){
  167 + $nameArr = explode('.',$name);
  168 + $enName = generateRoute(Translate::tran($nameArr[0], 'en'));
  169 + $i=1;
  170 + while($this->onlyName($enName.'.'.$nameArr[1],$project_id)){
  171 + $enName = $enName .'-'.$i;
  172 + $i++;
  173 + }
  174 + return $enName.'.'.$nameArr[1];
  175 + }
  176 +
  177 + /**
  178 + * @remark :唯一名称
  179 + * @name :onlyName
  180 + * @author :lyh
  181 + * @method :post
  182 + * @time :2024/4/26 16:21
  183 + */
  184 + public function onlyName($enName,$project_id){
  185 + $fileModel = new File();
  186 + $info = $fileModel->read(['project_id' => $project_id, 'en_name' => $enName]);
  187 + if($info !== false){
  188 + return true;
  189 + }
  190 + return false;
  191 + }
  192 +
  193 + /**
158 * 接口上传单文件 194 * 接口上传单文件
159 * @param $files 195 * @param $files
160 * @return array 196 * @return array
@@ -254,7 +290,7 @@ class FileController @@ -254,7 +290,7 @@ class FileController
254 } 290 }
255 $url = $this->config['root'].'/'.$this->path; 291 $url = $this->config['root'].'/'.$this->path;
256 $file_type = $file->getClientOriginalExtension(); 292 $file_type = $file->getClientOriginalExtension();
257 - $fileName = uniqid().rand(10000,99999).'.'.$file_type; 293 + $fileName = $this->getOnlyFilename($name,$param['project_id'] ?? 0);
258 //同步数据到cos 294 //同步数据到cos
259 if($this->upload_location == 0){ 295 if($this->upload_location == 0){
260 $cosService = new CosService(); 296 $cosService = new CosService();
@@ -3,6 +3,7 @@ @@ -3,6 +3,7 @@
3 namespace App\Http\Controllers\File; 3 namespace App\Http\Controllers\File;
4 4
5 use App\Enums\Common\Code; 5 use App\Enums\Common\Code;
  6 +use App\Helper\Translate;
6 use App\Http\Controllers\Controller; 7 use App\Http\Controllers\Controller;
7 use App\Http\Controllers\type; 8 use App\Http\Controllers\type;
8 use App\Models\File\ErrorFile; 9 use App\Models\File\ErrorFile;
@@ -200,7 +201,7 @@ class ImageController extends Controller @@ -200,7 +201,7 @@ class ImageController extends Controller
200 if(strlen($image_type) > 7){ 201 if(strlen($image_type) > 7){
201 $this->response('不支持当前格式',Code::SYSTEM_ERROR); 202 $this->response('不支持当前格式',Code::SYSTEM_ERROR);
202 } 203 }
203 - $fileName = uniqid().rand(10000,99999).'.'.$image_type; 204 + $fileName = $this->getOnlyFilename($name,$param['project_id'] ?? 0);
204 //上传到cos 205 //上传到cos
205 if($this->upload_location == 0){ 206 if($this->upload_location == 0){
206 $cosService = new CosService(); 207 $cosService = new CosService();
@@ -216,6 +217,41 @@ class ImageController extends Controller @@ -216,6 +217,41 @@ class ImageController extends Controller
216 } 217 }
217 218
218 /** 219 /**
  220 + * @remark :获取唯一名称
  221 + * @name :getOnlyFilename
  222 + * @author :lyh
  223 + * @method :post
  224 + * @time :2024/4/26 16:10
  225 + */
  226 + public function getOnlyFilename($name,$project_id = 0){
  227 + $nameArr = explode('.',$name);
  228 + $enName = generateRoute(Translate::tran($nameArr[0], 'en'));
  229 + $i=1;
  230 + while($this->onlyName($enName.'.'.$nameArr[1],$project_id)){
  231 + $enName = $enName .'-'.$i;
  232 + $i++;
  233 + }
  234 + return $enName.'.'.$nameArr[1];
  235 + }
  236 +
  237 + /**
  238 + * @remark :唯一名称
  239 + * @name :onlyName
  240 + * @author :lyh
  241 + * @method :post
  242 + * @time :2024/4/26 16:21
  243 + */
  244 + public function onlyName($enName,$project_id){
  245 + $imageModel = new ImageModel();
  246 + $info = $imageModel->read(['project_id' => $project_id, 'en_name' => $enName]);
  247 + if($info !== false){
  248 + return true;
  249 + }
  250 + return false;
  251 + }
  252 +
  253 +
  254 + /**
219 * @remark :指定同步文件到獨立177服務器 255 * @remark :指定同步文件到獨立177服務器
220 * @name :synchronizationFile 256 * @name :synchronizationFile
221 * @author :lyh 257 * @author :lyh
@@ -299,7 +335,7 @@ class ImageController extends Controller @@ -299,7 +335,7 @@ class ImageController extends Controller
299 continue; 335 continue;
300 } 336 }
301 $image_type = $file->getClientOriginalExtension(); 337 $image_type = $file->getClientOriginalExtension();
302 - $fileName = uniqid().rand(10000,99999).'.'.$image_type; 338 + $fileName = $this->getOnlyFilename($name,$param['project_id'] ?? 0);
303 //同步数据到cos 339 //同步数据到cos
304 if($this->upload_location == 0){ 340 if($this->upload_location == 0){
305 $cosService = new CosService(); 341 $cosService = new CosService();