Browse Source

拉黑上传图片优化

master
wanghongjun 4 weeks ago
parent
commit
3e38c8cd20
  1. 38
      app/common/controller/Upload.php

38
app/common/controller/Upload.php

@ -37,6 +37,14 @@ class Upload extends BaseController
$message=$data['message'] ?? '';
if($message){
$message=json_decode($message,true);
// 判断链接里的图片
if (isset($message['content'])) {
$imagePath = $this->imageFileUrl($message['content']);
if (file_exists($imagePath) && !$oldFile) {
$fileObj = false;
$path = $imagePath;
}
}
}
$uid=request()->userInfo['user_id'] ?? 1;
if($fileObj){
@ -92,7 +100,14 @@ class Upload extends BaseController
$file=FileModel::where(['md5'=>$info['md5'],'user_id'=>$uid])->find();
}
// 判断文件是否存在,如果有则不再上传
if($filecate == 'image' || !$file){
if ($file) {
$existsFile = public_path() . '/'.ltrim($file['src'],'/');
if (!file_exists($existsFile)) {
$file = [];
}
}
if(!$file){
$newName = uniqid() . '.' . $info['ext'];
$object = $prefix . $newName;
if($this->disk=='local'){
@ -105,7 +120,10 @@ class Upload extends BaseController
// 把左边的/去掉再加上,避免有些有/有些没有
$object='/'.ltrim($object,'/');
// 压缩图片
$compress_img = $file['compress_img'] ?? '';
$compress_img = '';
if (isset($file['compress_img']) && !$oldFile) {
$compress_img = $file['compress_img'];
}
if ($filecate == 'image') {
$compress_img = $this->getCompressImg($object, $info['ext'], $prefix);
}
@ -204,6 +222,12 @@ class Upload extends BaseController
}
}
protected function imageFileUrl($url)
{
$main_host = getMainHost();
return public_path() . '/' . ltrim(str_replace($main_host,'',$url),'/');
}
public function uploadFileImage()
{
$param=$this->request->param();
@ -218,10 +242,12 @@ class Upload extends BaseController
if (!$data['status']) {
throw new \Exception(lang('file.error'));
}
$info=$this->upload($param,$data['path'],'', false);
$info=$this->upload($param,$data['path'],'', false, true);
} elseif ($type == 'file') {
$main_host = getMainHost();
$path = public_path() . '/' . ltrim(str_replace($main_host,'',$image_url),'/');
$path = $this->imageFileUrl($image_url);
if (!file_exists($path)) {
throw new \Exception(lang('file.error'));
}
$info = $this->upload($param,$path,'', false, true);
} else {
throw new \Exception(lang('file.error'));
@ -672,7 +698,7 @@ class Upload extends BaseController
}
// 返回相对路径(适用于web访问)
return ['status' => 1, 'path' => $savePath . '/' . $filename];
return ['status' => 1, 'path' => $localPath];
} catch (\Exception $e) {
return ['status' => 0];
}

Loading…
Cancel
Save