|
...
|
...
|
@@ -4,6 +4,9 @@ namespace App\Http\Controllers\file; |
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Models\File\File;
|
|
|
|
use App\Models\File\Image as ImageModel;
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
|
|
|
class FileController
|
|
|
|
{
|
|
...
|
...
|
@@ -30,10 +33,17 @@ class FileController |
|
|
|
{
|
|
|
|
$this->request = request();
|
|
|
|
$this->config = config('filesystems.disks.upload');
|
|
|
|
$this->path = $this->config['root'];
|
|
|
|
$this->path = $this->config['root'].'/file/';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index($hash = '', $w = 0 ,$h = 0){
|
|
|
|
/**
|
|
|
|
* @param :(获取文件)$hash
|
|
|
|
* @name :index
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/5/9 9:15
|
|
|
|
*/
|
|
|
|
public function index($hash = ''){
|
|
|
|
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) || isset($_SERVER['HTTP_IF_NONE_MATCH'])) {
|
|
|
|
header("HTTP/1.1 304 Not Modified");
|
|
|
|
exit;
|
|
...
|
...
|
@@ -51,4 +61,112 @@ class FileController |
|
|
|
$header['Content-Length'] = $info['size'];
|
|
|
|
return response($content, 200, $header);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 图片上传
|
|
|
|
*/
|
|
|
|
public function upload() {
|
|
|
|
$this->request->validate([
|
|
|
|
'image'=>['required'],
|
|
|
|
],[
|
|
|
|
'image.required'=>'图片必须填写',
|
|
|
|
]);
|
|
|
|
$files = $this->request->file('file');
|
|
|
|
if (empty($files)) {
|
|
|
|
$this->response('没有上传的文件!', 400);
|
|
|
|
}
|
|
|
|
$type = $this->request->post('type', 'single');
|
|
|
|
if ($type == 'multi') {
|
|
|
|
return $this->multi($files);
|
|
|
|
} else {
|
|
|
|
return $this->single($files);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @name :上传
|
|
|
|
* @return void
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function single($files){
|
|
|
|
$hash = hash_file('md5', $files->getPathname());
|
|
|
|
//查看文件是否存在
|
|
|
|
$fileModel = new File();
|
|
|
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
|
|
|
if($file_hash !== false){
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
$url = $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);
|
|
|
|
}
|
|
|
|
$data = [
|
|
|
|
'path' => $url.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
];
|
|
|
|
$rs = $fileModel->add($data);
|
|
|
|
if ($rs === false) {
|
|
|
|
return $this->response('添加失败', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 多文件上传
|
|
|
|
* @param type $files file对象集合
|
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
private function multi($files) {
|
|
|
|
if (!is_array($files)) {
|
|
|
|
$files = [$files];
|
|
|
|
}
|
|
|
|
$save_data = [];
|
|
|
|
$data = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$fileModel = new File();
|
|
|
|
$hash = hash_file('md5', $file->getPathname());
|
|
|
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
|
|
|
if($file_hash !== false){
|
|
|
|
$data[] = $hash;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $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);
|
|
|
|
}
|
|
|
|
$save_data[] = [
|
|
|
|
'path' => $url.'/'.$fileName,
|
|
|
|
'created_at' => date('Y-m-d H:i:s',time()),
|
|
|
|
'size' => $res->getSize(),
|
|
|
|
'hash' => $hash,
|
|
|
|
'type'=>$files->getClientOriginalExtension(),
|
|
|
|
];
|
|
|
|
$data[] = $hash;
|
|
|
|
}
|
|
|
|
$fileModel->insert($save_data);
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @name 统一返回参数
|
|
|
|
* @return JsonResponse
|
|
|
|
* @author :liyuhang
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
public function response($msg = null,string $code = Code::SUCCESS,$data = [],$result_code = 200,$type = 'application/json'): JsonResponse
|
|
|
|
{
|
|
|
|
$code = Code::fromValue($code);
|
|
|
|
$result = [
|
|
|
|
'msg' => $msg == ' ' ? $code->description : $msg,
|
|
|
|
'code' => $code->value,
|
|
|
|
'data' => $data,
|
|
|
|
];
|
|
|
|
$this->header['Content-Type'] = $type;
|
|
|
|
$response = response($result,$result_code,$this->header);
|
|
|
|
throw new HttpResponseException($response);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|