作者 李宇航

合并分支 'develop' 到 'master'

更新图片文件生成名称规则,同时更新同步s3数据查看



查看合并请求 !511
@@ -125,7 +125,8 @@ class Demo extends Command @@ -125,7 +125,8 @@ class Demo extends Command
125 if(!empty($fileList)){ 125 if(!empty($fileList)){
126 $amazonS3Service = new AmazonS3Service(); 126 $amazonS3Service = new AmazonS3Service();
127 foreach ($fileList as $k => $v){ 127 foreach ($fileList as $k => $v){
128 - $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); 128 + echo date('Y-m-d H:i:s') . '执行的数据id:' . $v['id'] . '名称:'.$v['name'] . PHP_EOL;
  129 + $amazonS3Service->syncImageFiles(getFileUrl($v['path']));
129 $fileModel->edit(['is_cos'=>0],['id'=>$v['id']]); 130 $fileModel->edit(['is_cos'=>0],['id'=>$v['id']]);
130 } 131 }
131 } 132 }
@@ -4,7 +4,9 @@ namespace App\Http\Controllers\Bside\FileManage; @@ -4,7 +4,9 @@ namespace App\Http\Controllers\Bside\FileManage;
4 4
5 5
6 use App\Enums\Common\Code; 6 use App\Enums\Common\Code;
  7 +use App\Helper\Translate;
7 use App\Http\Controllers\Bside\BaseController; 8 use App\Http\Controllers\Bside\BaseController;
  9 +use App\Models\File\File;
8 use App\Models\FileManage\FileManage; 10 use App\Models\FileManage\FileManage;
9 use App\Models\Project\Project; 11 use App\Models\Project\Project;
10 use App\Services\CosService; 12 use App\Services\CosService;
@@ -79,18 +81,14 @@ class FileManageController extends BaseController @@ -79,18 +81,14 @@ class FileManageController extends BaseController
79 'file.required'=>'请上传文件', 81 'file.required'=>'请上传文件',
80 ]); 82 ]);
81 $file = $request->file('file'); 83 $file = $request->file('file');
82 -  
83 $data = $this->checkFile($file); 84 $data = $this->checkFile($file);
84 -  
85 $path = '/file_manage/' . $this->user['project_id'] . '/' . date('Ymd'); 85 $path = '/file_manage/' . $this->user['project_id'] . '/' . date('Ymd');
86 - $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();  
87 - 86 + $name = $file->getClientOriginalName();
  87 + $fileName = $this->getOnlyFilename($name,$this->user['project_id']);
88 $cosService = new CosService(); 88 $cosService = new CosService();
89 $cosService->uploadFile($file,$path,$fileName); 89 $cosService->uploadFile($file,$path,$fileName);
90 -  
91 $data['project_id'] = $this->user['project_id']; 90 $data['project_id'] = $this->user['project_id'];
92 $data['path'] = $path.'/'.$fileName; 91 $data['path'] = $path.'/'.$fileName;
93 -  
94 $rs = $fileManage->add($data); 92 $rs = $fileManage->add($data);
95 if ($rs === false) { 93 if ($rs === false) {
96 $this->fail('上传失败'); 94 $this->fail('上传失败');
@@ -98,6 +96,41 @@ class FileManageController extends BaseController @@ -98,6 +96,41 @@ class FileManageController extends BaseController
98 $this->response('success'); 96 $this->response('success');
99 } 97 }
100 98
  99 + /**
  100 + * @remark :获取唯一名称
  101 + * @name :getOnlyFilename
  102 + * @author :lyh
  103 + * @method :post
  104 + * @time :2024/4/26 16:10
  105 + */
  106 + public function getOnlyFilename($name,$project_id = 0){
  107 + $nameArr = explode('.',$name);
  108 + $enName = generateRoute(Translate::tran($nameArr[0], 'en'));
  109 + $i=1;
  110 + while($this->onlyName($enName.'.'.$nameArr[1],$project_id)){
  111 + $enName = $enName .'-'.$i;
  112 + $i++;
  113 + }
  114 + return $enName.'.'.$nameArr[1];
  115 + }
  116 +
  117 + /**
  118 + * @remark :唯一名称
  119 + * @name :onlyName
  120 + * @author :lyh
  121 + * @method :post
  122 + * @time :2024/4/26 16:21
  123 + */
  124 + public function onlyName($enName,$project_id){
  125 + $fileModel = new FileManage();
  126 + $info = $fileModel->read(['project_id' => $project_id, 'en_name' => $enName]);
  127 + if($info !== false){
  128 + return true;
  129 + }
  130 + return false;
  131 + }
  132 +
  133 +
101 protected function checkFile($file){ 134 protected function checkFile($file){
102 $count = FileManage::where('project_id', $this->user['project_id'])->count(); 135 $count = FileManage::where('project_id', $this->user['project_id'])->count();
103 if($count >= $this->upload_config['upload_max_num']){ 136 if($count >= $this->upload_config['upload_max_num']){
@@ -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();
@@ -53,7 +53,7 @@ class CopyImageFileJob implements ShouldQueue @@ -53,7 +53,7 @@ class CopyImageFileJob implements ShouldQueue
53 if(!empty($fileList)){ 53 if(!empty($fileList)){
54 $amazonS3Service = new AmazonS3Service(); 54 $amazonS3Service = new AmazonS3Service();
55 foreach ($fileList as $k => $v){ 55 foreach ($fileList as $k => $v){
56 - $amazonS3Service->syncImageFiles(getImageUrl($v['path'])); 56 + $amazonS3Service->syncImageFiles(getFileUrl($v['path']));
57 $fileModel->edit(['is_cos'=>0],['id'=>$v['id']]); 57 $fileModel->edit(['is_cos'=>0],['id'=>$v['id']]);
58 } 58 }
59 } 59 }