正在显示
3 个修改的文件
包含
49 行增加
和
15 行删除
| @@ -1642,5 +1642,16 @@ if (!function_exists('httpGetSsl')) { | @@ -1642,5 +1642,16 @@ if (!function_exists('httpGetSsl')) { | ||
| 1642 | return $truncated; | 1642 | return $truncated; |
| 1643 | } | 1643 | } |
| 1644 | 1644 | ||
| 1645 | - | 1645 | + /** |
| 1646 | + * @remark :写入日志 | ||
| 1647 | + * @name :outMessage | ||
| 1648 | + * @author :lyh | ||
| 1649 | + * @method :post | ||
| 1650 | + * @time :2025/11/20 16:04 | ||
| 1651 | + */ | ||
| 1652 | + function outMessage($filename = 'lyh_info',$message = '') | ||
| 1653 | + { | ||
| 1654 | + @file_put_contents(storage_path('logs/'.$filename.'.log'), "上传失败: " . $message . PHP_EOL, FILE_APPEND); | ||
| 1655 | + return true; | ||
| 1656 | + } | ||
| 1646 | } | 1657 | } |
| @@ -208,13 +208,13 @@ class ImageController extends Controller | @@ -208,13 +208,13 @@ class ImageController extends Controller | ||
| 208 | $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0); | 208 | $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0); |
| 209 | $cosService = new CosService(); | 209 | $cosService = new CosService(); |
| 210 | $res = $cosService->uploadFile($files,$this->path,$fileName,false,$watermarkOptions); | 210 | $res = $cosService->uploadFile($files,$this->path,$fileName,false,$watermarkOptions); |
| 211 | - if($res === false){ | ||
| 212 | - $this->response('上传失败,请重新上传',Code::SYSTEM_ERROR); | ||
| 213 | - } | ||
| 214 | }else{ | 211 | }else{ |
| 215 | //TODO::上传亚马逊 | 212 | //TODO::上传亚马逊 |
| 216 | $amazonS3Service = new AmazonS3Service(); | 213 | $amazonS3Service = new AmazonS3Service(); |
| 217 | - $amazonS3Service->uploadFiles($files,$this->path,$fileName); | 214 | + $res = $amazonS3Service->uploadFiles($files,$this->path,$fileName); |
| 215 | + } | ||
| 216 | + if($res === false){ | ||
| 217 | + $this->response('上传失败,请重新上传',Code::SYSTEM_ERROR); | ||
| 218 | } | 218 | } |
| 219 | $this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name); | 219 | $this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name); |
| 220 | $this->synchronizationImage($fileName,$this->upload_location); | 220 | $this->synchronizationImage($fileName,$this->upload_location); |
| @@ -357,11 +357,15 @@ class ImageController extends Controller | @@ -357,11 +357,15 @@ class ImageController extends Controller | ||
| 357 | if($this->upload_location == 0){ | 357 | if($this->upload_location == 0){ |
| 358 | $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0); | 358 | $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0); |
| 359 | $cosService = new CosService(); | 359 | $cosService = new CosService(); |
| 360 | - $cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions); | 360 | + $res = $cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions); |
| 361 | }else{ | 361 | }else{ |
| 362 | //TODO::上传亚马逊 | 362 | //TODO::上传亚马逊 |
| 363 | $amazonS3Service = new AmazonS3Service(); | 363 | $amazonS3Service = new AmazonS3Service(); |
| 364 | - $amazonS3Service->uploadFiles($file,$this->path,$fileName); | 364 | + $res = $amazonS3Service->uploadFiles($file,$this->path,$fileName); |
| 365 | + } | ||
| 366 | + if($res === false){ | ||
| 367 | + outMessage('upload_images','文件上传失败'.$name); | ||
| 368 | + continue; | ||
| 365 | } | 369 | } |
| 366 | $this->synchronizationImage($fileName,$this->upload_location); | 370 | $this->synchronizationImage($fileName,$this->upload_location); |
| 367 | $data[] = $this->responseData($this->path.'/'.$fileName,$name); | 371 | $data[] = $this->responseData($this->path.'/'.$fileName,$name); |
| @@ -24,7 +24,7 @@ class CosService | @@ -24,7 +24,7 @@ class CosService | ||
| 24 | * @method :post | 24 | * @method :post |
| 25 | * @time :2023/7/19 15:28 | 25 | * @time :2023/7/19 15:28 |
| 26 | */ | 26 | */ |
| 27 | - public function uploadFile(&$files,$path,$filename, $binary = false,$watermarkOptions = '') | 27 | + public function uploadFile(&$files, $path, $filename, $binary = false, $watermarkOptions = '') |
| 28 | { | 28 | { |
| 29 | $cos = config('filesystems.disks.cos'); | 29 | $cos = config('filesystems.disks.cos'); |
| 30 | $cosClient = new Client([ | 30 | $cosClient = new Client([ |
| @@ -34,7 +34,9 @@ class CosService | @@ -34,7 +34,9 @@ class CosService | ||
| 34 | 'secretKey' => $cos['credentials']['secretKey'], | 34 | 'secretKey' => $cos['credentials']['secretKey'], |
| 35 | ], | 35 | ], |
| 36 | ]); | 36 | ]); |
| 37 | - $key = $path.'/'.$filename; | 37 | + |
| 38 | + $key = $path . '/' . $filename; | ||
| 39 | + | ||
| 38 | // 判断是否为 Base64 编码的图片流文件 | 40 | // 判断是否为 Base64 编码的图片流文件 |
| 39 | if (Str::startsWith($files, 'data:image')) { | 41 | if (Str::startsWith($files, 'data:image')) { |
| 40 | // 分离 Base64 头部和数据部分 | 42 | // 分离 Base64 头部和数据部分 |
| @@ -42,11 +44,22 @@ class CosService | @@ -42,11 +44,22 @@ class CosService | ||
| 42 | // 解码 Base64 数据 | 44 | // 解码 Base64 数据 |
| 43 | $Body = base64_decode($base64Data); | 45 | $Body = base64_decode($base64Data); |
| 44 | if ($Body === false) { | 46 | if ($Body === false) { |
| 45 | - throw new \Exception("Base64 数据解码失败"); | 47 | + outMessage('upload_images',"解码失败"); |
| 48 | + return false; | ||
| 46 | } | 49 | } |
| 47 | } else { | 50 | } else { |
| 48 | // 如果不是 Base64 流文件,处理为普通文件上传 | 51 | // 如果不是 Base64 流文件,处理为普通文件上传 |
| 52 | + try { | ||
| 49 | $Body = $binary ? $files : fopen($files->getRealPath(), 'r'); | 53 | $Body = $binary ? $files : fopen($files->getRealPath(), 'r'); |
| 54 | + // 检查文件是否有效 | ||
| 55 | + if (!$Body) { | ||
| 56 | + outMessage('upload_images',"文件打开失败"); | ||
| 57 | + return false; | ||
| 58 | + } | ||
| 59 | + } catch (\Exception $e) { | ||
| 60 | + outMessage('upload_images',"文件处理失败: " . $e->getMessage()); | ||
| 61 | + return false; | ||
| 62 | + } | ||
| 50 | } | 63 | } |
| 51 | try { | 64 | try { |
| 52 | $options = [ | 65 | $options = [ |
| @@ -54,7 +67,7 @@ class CosService | @@ -54,7 +67,7 @@ class CosService | ||
| 54 | 'Key' => $key, | 67 | 'Key' => $key, |
| 55 | 'Body' => $Body, | 68 | 'Body' => $Body, |
| 56 | ]; | 69 | ]; |
| 57 | - //水印 | 70 | + // 水印处理 |
| 58 | if ($watermarkOptions) { | 71 | if ($watermarkOptions) { |
| 59 | $options['PicOperations'] = json_encode([ | 72 | $options['PicOperations'] = json_encode([ |
| 60 | 'is_pic_info' => 1, | 73 | 'is_pic_info' => 1, |
| @@ -66,15 +79,21 @@ class CosService | @@ -66,15 +79,21 @@ class CosService | ||
| 66 | ] | 79 | ] |
| 67 | ], true); | 80 | ], true); |
| 68 | } | 81 | } |
| 69 | - $res = $cosClient->putObject($options); | ||
| 70 | - @file_put_contents(storage_path('logs/upload_images'), var_export($res, true) . PHP_EOL, FILE_APPEND); | ||
| 71 | - }catch (\Exception $e){ | ||
| 72 | - @file_put_contents(storage_path('logs/upload_images'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND); | 82 | + $cosClient->putObject($options); |
| 83 | + } catch (\Exception $e) { | ||
| 84 | + // 记录上传失败日志 | ||
| 85 | + outMessage('upload_images',$e->getMessage()); | ||
| 73 | return false; | 86 | return false; |
| 87 | + } finally { | ||
| 88 | + // 确保非二进制模式下关闭文件资源 | ||
| 89 | + if (!$binary && is_resource($Body)) { | ||
| 90 | + fclose($Body); | ||
| 91 | + } | ||
| 74 | } | 92 | } |
| 75 | return $key; | 93 | return $key; |
| 76 | } | 94 | } |
| 77 | 95 | ||
| 96 | + | ||
| 78 | /** | 97 | /** |
| 79 | * @param $image_name | 98 | * @param $image_name |
| 80 | * @remark :获取图片访问链接 | 99 | * @remark :获取图片访问链接 |
-
请 注册 或 登录 后发表评论