作者 lyh

gx

@@ -6,6 +6,7 @@ use App\Enums\Common\Code; @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
6 use App\Models\File\File; 6 use App\Models\File\File;
7 use App\Models\File\Image as ImageModel; 7 use App\Models\File\Image as ImageModel;
8 use App\Models\Project\Project; 8 use App\Models\Project\Project;
  9 +use App\Services\CosService;
9 use Illuminate\Http\Exceptions\HttpResponseException; 10 use Illuminate\Http\Exceptions\HttpResponseException;
10 use Illuminate\Http\JsonResponse; 11 use Illuminate\Http\JsonResponse;
11 use Illuminate\Support\Facades\Cache; 12 use Illuminate\Support\Facades\Cache;
@@ -135,7 +136,9 @@ class FileController @@ -135,7 +136,9 @@ class FileController
135 ],[ 136 ],[
136 'file.required'=>'必须填写', 137 'file.required'=>'必须填写',
137 ]); 138 ]);
138 - $files = $this->param['file']; 139 + $files = $this->request->file('file');
  140 + $size = $files->getSize();
  141 + $file_type = $files->getClientOriginalExtension();
139 if (empty($files)) { 142 if (empty($files)) {
140 $this->response('没有上传的文件!', 400); 143 $this->response('没有上传的文件!', 400);
141 } 144 }
@@ -144,7 +147,7 @@ class FileController @@ -144,7 +147,7 @@ class FileController
144 if ($type == 'multi') { 147 if ($type == 'multi') {
145 return $this->multi($files); 148 return $this->multi($files);
146 } else { 149 } else {
147 - return $this->single($files); 150 + return $this->single($files,$size,$file_type);
148 } 151 }
149 } 152 }
150 153
@@ -156,7 +159,7 @@ class FileController @@ -156,7 +159,7 @@ class FileController
156 * @method :post 159 * @method :post
157 * @time :2023/6/17 16:32 160 * @time :2023/6/17 16:32
158 */ 161 */
159 - public function single($files){ 162 + public function single($files,$size,$file_type){
160 $hash = hash_file('md5', $files->getPathname()); 163 $hash = hash_file('md5', $files->getPathname());
161 //查看文件是否存在 164 //查看文件是否存在
162 $fileModel = new File(); 165 $fileModel = new File();
@@ -166,37 +169,52 @@ class FileController @@ -166,37 +169,52 @@ class FileController
166 } 169 }
167 $url = $this->config['root'].$this->path; 170 $url = $this->config['root'].$this->path;
168 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension(); 171 $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
  172 + //同步数据到cos
  173 + if($this->upload_location == 1){
  174 + $cosService = new CosService();
  175 + $cosService->uploadFile($files,$this->path,$fileName);
  176 + }else{
169 $res = $files->move($url,$fileName); 177 $res = $files->move($url,$fileName);
170 if ($res === false) { 178 if ($res === false) {
171 return $this->response($files->getError(), Code::USER_ERROR); 179 return $this->response($files->getError(), Code::USER_ERROR);
172 } 180 }
  181 + }
  182 + $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash);
  183 + return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
  184 + }
  185 +
  186 + /**
  187 + * @remark :保存数据库
  188 + * @name :saveMysql
  189 + * @author :lyh
  190 + * @method :post
  191 + * @time :2023/7/19 16:38
  192 + */
  193 + public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash){
173 $data = [ 194 $data = [
174 'path' => $this->path.'/'.$fileName, 195 'path' => $this->path.'/'.$fileName,
175 'created_at' => date('Y-m-d H:i:s',time()), 196 'created_at' => date('Y-m-d H:i:s',time()),
176 - 'size' => $res->getSize(), 197 + 'size' => $size,
177 'hash' => $hash, 198 'hash' => $hash,
178 - 'type'=>$files->getClientOriginalExtension(),  
179 - 'refer'=>$this->param['refer'] ?? 0 199 + 'type'=>$image_type,
  200 + 'refer'=>$this->param['refer'] ?? 1,
180 ]; 201 ];
181 $rs = $fileModel->add($data); 202 $rs = $fileModel->add($data);
182 if ($rs === false) { 203 if ($rs === false) {
183 return $this->response('添加失败', Code::USER_ERROR); 204 return $this->response('添加失败', Code::USER_ERROR);
184 } 205 }
185 - return $this->response('资源',Code::SUCCESS,['file'=>$hash]); 206 + return true;
186 } 207 }
187 208
188 /** 209 /**
189 * @param $files 210 * @param $files
190 - * @remark :多文件上传 211 + * @remark :多文件上传(暂时未用)
191 * @name :multi 212 * @name :multi
192 * @author :lyh 213 * @author :lyh
193 * @method :post 214 * @method :post
194 * @time :2023/6/17 16:32 215 * @time :2023/6/17 16:32
195 */ 216 */
196 private function multi($files) { 217 private function multi($files) {
197 - if (!is_array($files)) {  
198 - $files = [$files];  
199 - }  
200 $save_data = []; 218 $save_data = [];
201 $data = []; 219 $data = [];
202 foreach ($files as $file) { 220 foreach ($files as $file) {
@@ -325,9 +343,7 @@ class FileController @@ -325,9 +343,7 @@ class FileController
325 }else{ 343 }else{
326 $projectModel = new Project(); 344 $projectModel = new Project();
327 $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']); 345 $project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
328 - if($project_info['upload_location'] != 1){  
329 - $this->upload_location = 0;  
330 - } 346 + $this->upload_location = $project_info['upload_location'];
331 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m'); 347 $this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
332 } 348 }
333 } 349 }
@@ -214,9 +214,6 @@ class ImageController extends Controller @@ -214,9 +214,6 @@ class ImageController extends Controller
214 * @time :2023/6/17 16:31 214 * @time :2023/6/17 16:31
215 */ 215 */
216 private function multi(&$files) { 216 private function multi(&$files) {
217 - if (!is_array($files)) {  
218 - $files = [$files];  
219 - }  
220 $save_data = []; 217 $save_data = [];
221 $data = []; 218 $data = [];
222 foreach ($files as $file) { 219 foreach ($files as $file) {
@@ -231,10 +228,16 @@ class ImageController extends Controller @@ -231,10 +228,16 @@ class ImageController extends Controller
231 } 228 }
232 $url = $this->config['root'].$this->path; 229 $url = $this->config['root'].$this->path;
233 $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension(); 230 $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
  231 + //同步数据到cos
  232 + if($this->upload_location == 1){
  233 + $cosService = new CosService();
  234 + $cosService->uploadFile($file,$this->path,$fileName);
  235 + }else{
234 $res = $file->move($url,$fileName); 236 $res = $file->move($url,$fileName);
235 if ($res === false) { 237 if ($res === false) {
236 return $this->response($file->getError(), Code::USER_ERROR); 238 return $this->response($file->getError(), Code::USER_ERROR);
237 } 239 }
  240 + }
238 $save_data[] = [ 241 $save_data[] = [
239 'path' => $this->path.'/'.$fileName, 242 'path' => $this->path.'/'.$fileName,
240 'created_at' => date('Y-m-d H:i:s',time()), 243 'created_at' => date('Y-m-d H:i:s',time()),
@@ -245,11 +248,6 @@ class ImageController extends Controller @@ -245,11 +248,6 @@ class ImageController extends Controller
245 'refer'=>$this->param['refer'] ?? 0, 248 'refer'=>$this->param['refer'] ?? 0,
246 ]; 249 ];
247 $data[] = ['image'=>$hash]; 250 $data[] = ['image'=>$hash];
248 - //同步数据到cos  
249 - if($this->upload_location == 1){  
250 - $cosService = new CosService();  
251 - $cosService->uploadFile($file,$this->path,$fileName);  
252 - }  
253 } 251 }
254 $imageModel->insert($save_data); 252 $imageModel->insert($save_data);
255 return $this->response('图片资源',Code::SUCCESS,$data); 253 return $this->response('图片资源',Code::SUCCESS,$data);