FileManageController.php
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace App\Http\Controllers\Bside\FileManage;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Models\FileManage\FileManage;
use App\Models\Project\Project;
use App\Services\CosService;
use Illuminate\Http\Request;
/**
* 文件管理
* Class FileManageController
* @package App\Http\Controllers\Bside\FileManage
* @author zbj
* @date 2023/10/9
*/
class FileManageController extends BaseController
{
protected $upload_config;
public function __construct(Request $request)
{
parent::__construct($request);
//判断是否开启
$project = Project::find($this->user['project_id']);
if(empty($project['is_upload_manage'])){
$this->fail('文件上传管理功能未开启');
}
$this->upload_config = $project['upload_config'];
}
public function index(FileManage $fileManage){
$this->map['project_id'] = $this->user['project_id'];
$this->request['name'] && $this->map['name'] = ['like','%'.$this->request['name'].'%'];
$lists = $fileManage->lists($this->map, $this->page, $this->row);
$this->response('success',Code::SUCCESS,$lists);
}
public function upload(Request $request, FileManage $fileManage){
$request->validate([
'file'=>['required'],
],[
'file.required'=>'请上传文件',
]);
$file = $request->file('file');
$data = $this->checkFile($file);
$path = '/file_manage/' . $this->user['project_id'] . '/' . date('Ymd');
$fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
$cosService = new CosService();
$cosService->uploadFile($file,$path,$fileName);
$data['project_id'] = $this->user['project_id'];
$data['path'] = $path.'/'.$fileName;
$rs = $fileManage->add($data);
if ($rs === false) {
$this->fail('上传失败');
}
$this->response('success');
}
protected function checkFile($file){
$count = FileManage::where('project_id', $this->user['project_id'])->count();
if($count >= $this->upload_config['upload_max_num']){
$this->fail('超出最大上传文件数量限制:'. $this->upload_config['upload_max_num']);
}
$size = $file->getSize();
if($file->getSize()/1024/1024 > $this->upload_config['upload_max_size']){
$this->fail('超出最大允许上传文件大小:'. $this->upload_config['upload_max_size'] .'M');
}
$extension = $file->getClientOriginalExtension();
if(!in_array($extension, explode(',', $this->upload_config['allow_file_type']))){
$this->fail('不允许上传的文件类型');
}
$hash = hash_file('md5', $file->getPathname());
$info = FileManage::where('hash', $hash)->first();
if($info){
$this->fail('文件已存在');
}
return [
'size' => $size,
'extension' => $extension,
'hash' => $hash,
'name' => $file->getClientOriginalName(),
'mime' => $file->getMimeType(),
];
}
public function delete(Request $request){
$request->validate([
'id'=>'required',
],[
'id.required' => 'ID不能为空',
]);
$fileManage = FileManage::find($this->param['id']);
if(!$fileManage){
$this->response('数据不存在或者已经删除');
}
$fileManage->delete();
$this->response('success');
}
}