FileManageController.php
8.9 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?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('参数错误:path 参数缺失或为空', Code::SYSTEM_ERROR);
}
// 获取文件名和完整 URL
$username = basename($this->param['path']);
$parsed_url = parse_url($this->param['path']);
$fileUrl = isset($parsed_url['scheme'])
? $this->param['path']
: 'https://file.globalso.com' . $this->param['path'];
// 使用 cURL 获取文件头信息
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 不直接输出,而是返回结果
curl_setopt($ch, CURLOPT_HEADER, true); // 获取响应头
curl_setopt($ch, CURLOPT_NOBODY, true); // 只请求头部,不下载实际文件
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
// 检查文件大小和状态码
if ($httpCode != 200 || $fileSize <= 0) {
$this->response('无法获取文件信息或文件不存在', Code::SYSTEM_ERROR);
}
// 获取文件类型
$contentType = 'application/octet-stream'; // 默认类型为二进制流
if (preg_match('/Content-Type: (.+)/i', $response, $matches)) {
$contentType = $matches[1];
}
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: ' . $contentType);
header('Content-Disposition: attachment; filename="' . $username . '"');
header('Content-Length: ' . $fileSize);
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Expires: 0');
// 清空输出缓冲区
while (ob_get_level() > 0) {
ob_end_clean();
}
// 初始化 cURL 下载文件
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $fileUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出内容
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 跟随重定向
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8192); // 设置缓冲区大小
curl_setopt($ch, CURLOPT_TIMEOUT, 300); // 设置超时时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 连接超时时间
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用 SSL 验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// 分块输出文件内容
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) {
echo $data;
flush(); // 强制输出缓冲
return strlen($data);
});
// 执行 cURL
curl_exec($ch);
// 检查 cURL 错误或 HTTP 状态码
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch) || $httpCode != 200) {
$errorMsg = curl_errno($ch)
? 'cURL 错误: ' . curl_error($ch)
: 'HTTP 错误: ' . $httpCode;
curl_close($ch);
$this->response('文件下载失败: ' . $errorMsg, Code::SYSTEM_ERROR);
}
// 关闭 cURL
curl_close($ch);
exit;
}
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');
}
}