作者 lyh

gx

... ... @@ -6,6 +6,7 @@ use App\Enums\Common\Code;
use App\Models\File\File;
use App\Models\File\Image as ImageModel;
use App\Models\Project\Project;
use App\Services\CosService;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
... ... @@ -135,7 +136,9 @@ class FileController
],[
'file.required'=>'必须填写',
]);
$files = $this->param['file'];
$files = $this->request->file('file');
$size = $files->getSize();
$file_type = $files->getClientOriginalExtension();
if (empty($files)) {
$this->response('没有上传的文件!', 400);
}
... ... @@ -144,7 +147,7 @@ class FileController
if ($type == 'multi') {
return $this->multi($files);
} else {
return $this->single($files);
return $this->single($files,$size,$file_type);
}
}
... ... @@ -156,7 +159,7 @@ class FileController
* @method :post
* @time :2023/6/17 16:32
*/
public function single($files){
public function single($files,$size,$file_type){
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$fileModel = new File();
... ... @@ -166,37 +169,52 @@ class FileController
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($files,$this->path,$fileName);
}else{
$res = $files->move($url,$fileName);
if ($res === false) {
return $this->response($files->getError(), Code::USER_ERROR);
}
}
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash);
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
}
/**
* @remark :保存数据库
* @name :saveMysql
* @author :lyh
* @method :post
* @time :2023/7/19 16:38
*/
public function saveMysql(&$fileModel,$size,$image_type,$fileName,$hash){
$data = [
'path' => $this->path.'/'.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'size' => $size,
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
'refer'=>$this->param['refer'] ?? 0
'type'=>$image_type,
'refer'=>$this->param['refer'] ?? 1,
];
$rs = $fileModel->add($data);
if ($rs === false) {
return $this->response('添加失败', Code::USER_ERROR);
}
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
return true;
}
/**
* @param $files
* @remark :多文件上传
* @remark :多文件上传(暂时未用)
* @name :multi
* @author :lyh
* @method :post
* @time :2023/6/17 16:32
*/
private function multi($files) {
if (!is_array($files)) {
$files = [$files];
}
$save_data = [];
$data = [];
foreach ($files as $file) {
... ... @@ -325,9 +343,7 @@ class FileController
}else{
$projectModel = new Project();
$project_info = $projectModel->read(['id'=>$this->cache['project_id']],['upload_location']);
if($project_info['upload_location'] != 1){
$this->upload_location = 0;
}
$this->upload_location = $project_info['upload_location'];
$this->path = $this->uploads['path_b'].'/'.$this->cache['project_id'].'/'.$this->image_type[$this->param['refer']].'/'.date('Y-m');
}
}
... ...
... ... @@ -214,9 +214,6 @@ class ImageController extends Controller
* @time :2023/6/17 16:31
*/
private function multi(&$files) {
if (!is_array($files)) {
$files = [$files];
}
$save_data = [];
$data = [];
foreach ($files as $file) {
... ... @@ -231,9 +228,15 @@ class ImageController extends Controller
}
$url = $this->config['root'].$this->path;
$fileName = uniqid().rand(10000,99999).'.'.$file->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();
$cosService->uploadFile($file,$this->path,$fileName);
}else{
$res = $file->move($url,$fileName);
if ($res === false) {
return $this->response($file->getError(), Code::USER_ERROR);
}
}
$save_data[] = [
'path' => $this->path.'/'.$fileName,
... ... @@ -245,11 +248,6 @@ class ImageController extends Controller
'refer'=>$this->param['refer'] ?? 0,
];
$data[] = ['image'=>$hash];
//同步数据到cos
if($this->upload_location == 1){
$cosService = new CosService();
$cosService->uploadFile($file,$this->path,$fileName);
}
}
$imageModel->insert($save_data);
return $this->response('图片资源',Code::SUCCESS,$data);
... ...