作者 lyh

gx

... ... @@ -142,8 +142,7 @@ class FileController
'file.required'=>'必须填写',
]);
$files = $this->request->file('file');
$size = $files->getSize();
$file_type = $files->getClientOriginalExtension();
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
... ... @@ -152,6 +151,8 @@ class FileController
if ($type == 'multi') {
return $this->multi($files);
} else {
$size = $files->getSize();
$file_type = $files->getClientOriginalExtension();
return $this->single($files,$size,$file_type);
}
}
... ... @@ -223,7 +224,6 @@ class FileController
* @time :2023/6/17 16:32
*/
private function multi($files) {
$save_data = [];
$data = [];
foreach ($files as $file) {
$fileModel = new File();
... ... @@ -235,24 +235,27 @@ class FileController
}
$url = $this->config['root'].'/'.$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$is_cos = 1;//上传到cos
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$is_cos = 0;
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$save_data[] = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->param['refer']
];
$size = $file->getSize();
$file_type = $file->getClientOriginalExtension();
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
$data[] = $hash;
}
$fileModel->insert($save_data);
return $this->response('资源',Code::SUCCESS,['file'=>$data]);
}
/**
* @param $msg
* @param string $code
... ...
... ... @@ -41,6 +41,7 @@ class ImageController extends Controller
public $cache = '';//缓存数据
public $upload_location = 0;//是否同步到cos
//上传图片类型
public $image_type = [
1 => 'image_product',
2 => 'image_news',
... ... @@ -157,15 +158,15 @@ class ImageController extends Controller
return $this->response('图片资源',Code::SUCCESS,['image'=>$hash]);
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
//同步数据到cos
$fileName = uniqid().rand(10000,99999).'.'.$image_type;
//上传到cos
if($this->upload_location == 1){
$cosService = new CosService();
$is_cos = 1;//上传到cos
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
$is_cos = 0;//上传到cos
$is_cos = 0;
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
... ... @@ -223,7 +224,7 @@ class ImageController extends Controller
* @time :2023/6/17 16:31
*/
private function multi(&$files) {
$save_data = [];
$save_data = [];//保存数据
$data = [];
foreach ($files as $file) {
$size = $file->getSize();
... ... @@ -243,21 +244,14 @@ class ImageController extends Controller
$is_cos = 1;//上传到cos
$cosService->uploadFile($file,$this->path,$fileName);
}else{
$is_cos = 0;
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
}
$save_data[] = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'updated_at'=>date('Y-m-d H:i:s',time()),
'size' => $size,
'hash' => $hash,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 0,
'is_cos'=>$is_cos ?? 0
];
//批量存储
$save_data = $this->saveMysqlAll($save_data,$fileName,$size,$hash,$image_type,$is_cos);
$data[] = ['image'=>$hash];
}
$imageModel->insert($save_data);
... ... @@ -265,6 +259,31 @@ class ImageController extends Controller
}
/**
* @param $save_data
* @param $fileName
* @param $size
* @param $hash
* @param $image_type
* @remark :批量拼接数据
* @name :saveMysqlAll
* @author :lyh
* @method :post
* @time :2023/7/20 9:26
*/
public function saveMysqlAll(&$save_data,$fileName,$size,$hash,$image_type,$is_cos){
$save_data[] = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'updated_at'=>date('Y-m-d H:i:s',time()),
'size' => $size,
'hash' => $hash,
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 0,
'is_cos'=>$is_cos ?? 0
];
return $save_data;
}
/**
* @param $filename
* @remark :下载
* @name :download
... ...