作者 lyh

变更数据

... ... @@ -1642,5 +1642,16 @@ if (!function_exists('httpGetSsl')) {
return $truncated;
}
/**
* @remark :写入日志
* @name :outMessage
* @author :lyh
* @method :post
* @time :2025/11/20 16:04
*/
function outMessage($filename = 'lyh_info',$message = '')
{
@file_put_contents(storage_path('logs/'.$filename.'.log'), "上传失败: " . $message . PHP_EOL, FILE_APPEND);
return true;
}
}
... ...
... ... @@ -208,13 +208,13 @@ class ImageController extends Controller
$watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0);
$cosService = new CosService();
$res = $cosService->uploadFile($files,$this->path,$fileName,false,$watermarkOptions);
if($res === false){
$this->response('上传失败,请重新上传',Code::SYSTEM_ERROR);
}
}else{
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($files,$this->path,$fileName);
$res = $amazonS3Service->uploadFiles($files,$this->path,$fileName);
}
if($res === false){
$this->response('上传失败,请重新上传',Code::SYSTEM_ERROR);
}
$this->saveMysql($imageModel,$files->getSize(),$image_type,$fileName,$hash,$this->upload_location,$files->getMimeType(), $name);
$this->synchronizationImage($fileName,$this->upload_location);
... ... @@ -357,11 +357,15 @@ class ImageController extends Controller
if($this->upload_location == 0){
$watermarkOptions = $this->getProjectConfig($this->cache['project_id'] ?? 0);
$cosService = new CosService();
$cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions);
$res = $cosService->uploadFile($file,$this->path,$fileName,false,$watermarkOptions);
}else{
//TODO::上传亚马逊
$amazonS3Service = new AmazonS3Service();
$amazonS3Service->uploadFiles($file,$this->path,$fileName);
$res = $amazonS3Service->uploadFiles($file,$this->path,$fileName);
}
if($res === false){
outMessage('upload_images','文件上传失败'.$name);
continue;
}
$this->synchronizationImage($fileName,$this->upload_location);
$data[] = $this->responseData($this->path.'/'.$fileName,$name);
... ...
... ... @@ -24,7 +24,7 @@ class CosService
* @method :post
* @time :2023/7/19 15:28
*/
public function uploadFile(&$files,$path,$filename, $binary = false,$watermarkOptions = '')
public function uploadFile(&$files, $path, $filename, $binary = false, $watermarkOptions = '')
{
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
... ... @@ -34,7 +34,9 @@ class CosService
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$key = $path.'/'.$filename;
$key = $path . '/' . $filename;
// 判断是否为 Base64 编码的图片流文件
if (Str::startsWith($files, 'data:image')) {
// 分离 Base64 头部和数据部分
... ... @@ -42,11 +44,22 @@ class CosService
// 解码 Base64 数据
$Body = base64_decode($base64Data);
if ($Body === false) {
throw new \Exception("Base64 数据解码失败");
outMessage('upload_images',"解码失败");
return false;
}
} else {
// 如果不是 Base64 流文件,处理为普通文件上传
try {
$Body = $binary ? $files : fopen($files->getRealPath(), 'r');
// 检查文件是否有效
if (!$Body) {
outMessage('upload_images',"文件打开失败");
return false;
}
} catch (\Exception $e) {
outMessage('upload_images',"文件处理失败: " . $e->getMessage());
return false;
}
}
try {
$options = [
... ... @@ -54,7 +67,7 @@ class CosService
'Key' => $key,
'Body' => $Body,
];
//水印
// 水印处理
if ($watermarkOptions) {
$options['PicOperations'] = json_encode([
'is_pic_info' => 1,
... ... @@ -66,15 +79,21 @@ class CosService
]
], true);
}
$res = $cosClient->putObject($options);
@file_put_contents(storage_path('logs/upload_images'), var_export($res, true) . PHP_EOL, FILE_APPEND);
}catch (\Exception $e){
@file_put_contents(storage_path('logs/upload_images'), var_export($e->getMessage(), true) . PHP_EOL, FILE_APPEND);
$cosClient->putObject($options);
} catch (\Exception $e) {
// 记录上传失败日志
outMessage('upload_images',$e->getMessage());
return false;
} finally {
// 确保非二进制模式下关闭文件资源
if (!$binary && is_resource($Body)) {
fclose($Body);
}
}
return $key;
}
/**
* @param $image_name
* @remark :获取图片访问链接
... ...