作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !3297
@@ -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 }
@@ -232,7 +232,7 @@ class IndexController extends BaseController @@ -232,7 +232,7 @@ class IndexController extends BaseController
232 public function prInfoDownload() 232 public function prInfoDownload()
233 { 233 {
234 $url = 'http://crawl.scraper.waimaoq.com/export/issuewire?token=MT0CM7y4tdFTFTm'; 234 $url = 'http://crawl.scraper.waimaoq.com/export/issuewire?token=MT0CM7y4tdFTFTm';
235 - $response = Http::get($url,['page'=>$this->param['page'],'pagesize'=>$this->param['row']]); 235 + $response = Http::get($url,['page'=>$this->page,'pagesize'=>$this->row]);
236 if ($response->successful()) { 236 if ($response->successful()) {
237 $result = $response->json(); 237 $result = $response->json();
238 } 238 }
@@ -207,11 +207,14 @@ class ImageController extends Controller @@ -207,11 +207,14 @@ class ImageController extends Controller
207 if($this->upload_location == 0){ 207 if($this->upload_location == 0){
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 - $cosService->uploadFile($files,$this->path,$fileName,false,$watermarkOptions); 210 + $res = $cosService->uploadFile($files,$this->path,$fileName,false,$watermarkOptions);
211 }else{ 211 }else{
212 //TODO::上传亚马逊 212 //TODO::上传亚马逊
213 $amazonS3Service = new AmazonS3Service(); 213 $amazonS3Service = new AmazonS3Service();
214 - $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);
215 } 218 }
216 $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);
217 $this->synchronizationImage($fileName,$this->upload_location); 220 $this->synchronizationImage($fileName,$this->upload_location);
@@ -354,11 +357,15 @@ class ImageController extends Controller @@ -354,11 +357,15 @@ class ImageController extends Controller
354 if($this->upload_location == 0){ 357 if($this->upload_location == 0){
355 $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0); 358 $watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0);
356 $cosService = new CosService(); 359 $cosService = new CosService();
357 - $cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions); 360 + $res = $cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions);
358 }else{ 361 }else{
359 //TODO::上传亚马逊 362 //TODO::上传亚马逊
360 $amazonS3Service = new AmazonS3Service(); 363 $amazonS3Service = new AmazonS3Service();
361 - $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;
362 } 369 }
363 $this->synchronizationImage($fileName,$this->upload_location); 370 $this->synchronizationImage($fileName,$this->upload_location);
364 $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,33 +44,56 @@ class CosService @@ -42,33 +44,56 @@ 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 流文件,处理为普通文件上传
49 - $Body = $binary ? $files : fopen($files->getRealPath(), 'r'); 52 + try {
  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 - $options = [  
52 - 'Bucket' => $cos['bucket'],  
53 - 'Key' => $key,  
54 - 'Body' => $Body,  
55 - ];  
56 - //水印  
57 - if ($watermarkOptions) {  
58 - $options['PicOperations'] = json_encode([  
59 - 'is_pic_info' => 1,  
60 - 'rules' => [  
61 - [  
62 - 'fileid' => $key, // 使用相同的文件名保存  
63 - 'rule' => $watermarkOptions, 64 + try {
  65 + $options = [
  66 + 'Bucket' => $cos['bucket'],
  67 + 'Key' => $key,
  68 + 'Body' => $Body,
  69 + ];
  70 + // 水印处理
  71 + if ($watermarkOptions) {
  72 + $options['PicOperations'] = json_encode([
  73 + 'is_pic_info' => 1,
  74 + 'rules' => [
  75 + [
  76 + 'fileid' => $key, // 使用相同的文件名保存
  77 + 'rule' => $watermarkOptions,
  78 + ]
64 ] 79 ]
65 - ]  
66 - ], true); 80 + ], true);
  81 + }
  82 + $cosClient->putObject($options);
  83 + } catch (\Exception $e) {
  84 + // 记录上传失败日志
  85 + outMessage('upload_images',$e->getMessage());
  86 + return false;
  87 + } finally {
  88 + // 确保非二进制模式下关闭文件资源
  89 + if (!$binary && is_resource($Body)) {
  90 + fclose($Body);
  91 + }
67 } 92 }
68 - $res = $cosClient->putObject($options);  
69 return $key; 93 return $key;
70 } 94 }
71 95
  96 +
72 /** 97 /**
73 * @param $image_name 98 * @param $image_name
74 * @remark :获取图片访问链接 99 * @remark :获取图片访问链接