FileManageController.php
6.0 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
namespace App\Http\Controllers\Bside\FileManage;
use App\Enums\Common\Code;
use App\Helper\Translate;
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);
if(!empty($this->user)){
$project = Project::find($this->user['project_id']);
if(empty($project['is_upload_manage'])){
$this->fail('文件上传管理功能未开启');
}
//判断是否开启
$this->upload_config = $project['upload_config'];
}
}
/**
* @remark :列表
* @name :index
* @author :lyh
* @method :post
* @time :2023/12/28 17:03
*/
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);
if(!empty($lists) && !empty($lists['list'])){
foreach ($lists['list'] as $k => $v){
$v['download_url'] = url('b/file_manager_downLoad?path='.$v['path']);
$lists['list'][$k] = $v;
}
}
$this->response('success',Code::SUCCESS,$lists);
}
/**
* @remark :下载方法
* @name :downLoad
* @author :lyh
* @method :post
* @time :2023/12/28 17:18
*/
public function downLoad(){
if(!isset($this->param['path']) || empty($this->param['path'])){
$this->response('参数错误',Code::SYSTEM_ERROR);
}
$username = basename($this->param['path']);
$parsed_url = parse_url($this->param['path']);
if(isset($parsed_url['scheme'])){
$fileUrl = $this->param['path'];
} else {
$fileUrl = 'https://file.globalso.com'.$this->param['path'];
}
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $username . '"');
// 下载文件
readfile($fileUrl);
}
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');
$name = $file->getClientOriginalName();
$fileName = $this->getOnlyFilename($name,$this->user['project_id']);
$cosService = new CosService();
$cosService->uploadFile($file,$path,$fileName);
$data['project_id'] = $this->user['project_id'];
$data['en_name'] = $fileName;
$data['path'] = $path.'/'.$fileName;
$rs = $fileManage->add($data);
if ($rs === false) {
$this->fail('上传失败');
}
$this->response('success');
}
/**
* @remark :获取唯一名称
* @name :getOnlyFilename
* @author :lyh
* @method :post
* @time :2024/4/26 16:10
*/
public function getOnlyFilename($name,$project_id = 0){
$nameArr = explode('.',$name);
$suffix = array_pop($nameArr) ?? 'txt';
$nameStr = implode('-', $nameArr);
$enName = generateRoute(Translate::tran($nameStr, 'en'));
$fileName = $enName;
$i=1;
while($this->onlyName($enName.'.'.$suffix,$project_id)){
$enName = $fileName .'-'.$i;
$i++;
}
return $enName.'.'.$suffix;
}
/**
* @remark :唯一名称
* @name :onlyName
* @author :lyh
* @method :post
* @time :2024/4/26 16:21
*/
public function onlyName($enName,$project_id){
$fileModel = new FileManage();
$info = $fileModel->read(['project_id' => $project_id, 'en_name' => $enName]);
if($info !== false){
return true;
}
return false;
}
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 = strtolower($file->getClientOriginalExtension());
if(!in_array($extension, explode(',', $this->upload_config['allow_file_type']))){
$this->fail('不允许上传的文件类型');
}
$hash = hash_file('sha256', $file->getPathname());
$info = FileManage::where('hash', $hash)->first();
if($info){
$this->fail('文件已上传,文件名称.'.$info['name']);
}
return [
'size' => $size,
'extension' => $extension,
'hash' => $hash,
'name' => $file->getClientOriginalName(),
'mime' => $file->getMimeType(),
];
}
public function delete(Request $request){
$request->validate([
'id'=>'required | array',
],[
'id.required' => 'ID不能为空',
]);
if(!is_array($this->param['id'])){
$this->param['id'] = [$this->param['id']];
}
$fileManage = new FileManage();
$fileManage->del(['id'=>['in',$this->param['id']]]);
$this->response('success');
}
}