|
@@ -38,8 +38,9 @@ class FileController |
|
@@ -38,8 +38,9 @@ class FileController |
|
38
|
public $cache = '';//缓存数据
|
38
|
public $cache = '';//缓存数据
|
|
39
|
public $upload_location = 1;
|
39
|
public $upload_location = 1;
|
|
40
|
public $file_type = [
|
40
|
public $file_type = [
|
|
41
|
- 1 => 'file_other',
|
|
|
|
42
|
- 0 => 'file'
|
41
|
+ 2 => 'other',//其他
|
|
|
|
42
|
+ 1 => 'video',//视频
|
|
|
|
43
|
+ 0 => 'file'//文件
|
|
43
|
];
|
44
|
];
|
|
44
|
public function __construct()
|
45
|
public function __construct()
|
|
45
|
{
|
46
|
{
|
|
@@ -169,7 +170,7 @@ class FileController |
|
@@ -169,7 +170,7 @@ class FileController |
|
169
|
$hash = hash_file('md5', $files->getPathname());
|
170
|
$hash = hash_file('md5', $files->getPathname());
|
|
170
|
//查看文件是否存在
|
171
|
//查看文件是否存在
|
|
171
|
$fileModel = new File();
|
172
|
$fileModel = new File();
|
|
172
|
- $file_hash = $fileModel->read(['hash'=>$hash]);
|
173
|
+ $file_hash = $fileModel->read(['hash'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
|
|
173
|
if($file_hash !== false){
|
174
|
if($file_hash !== false){
|
|
174
|
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
|
175
|
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
|
|
175
|
}
|
176
|
}
|
|
@@ -178,17 +179,15 @@ class FileController |
|
@@ -178,17 +179,15 @@ class FileController |
|
178
|
//同步数据到cos
|
179
|
//同步数据到cos
|
|
179
|
if($this->upload_location == 1){
|
180
|
if($this->upload_location == 1){
|
|
180
|
$cosService = new CosService();
|
181
|
$cosService = new CosService();
|
|
181
|
- $is_cos = 1;//上传到cos
|
|
|
|
182
|
$cosService->uploadFile($files,$this->path,$fileName);
|
182
|
$cosService->uploadFile($files,$this->path,$fileName);
|
|
183
|
}else{
|
183
|
}else{
|
|
184
|
- $is_cos = 0;
|
|
|
|
185
|
$res = $files->move($url,$fileName);
|
184
|
$res = $files->move($url,$fileName);
|
|
186
|
if ($res === false) {
|
185
|
if ($res === false) {
|
|
187
|
return $this->response($files->getError(), Code::USER_ERROR);
|
186
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
188
|
}
|
187
|
}
|
|
189
|
}
|
188
|
}
|
|
190
|
- $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
|
|
|
|
191
|
- return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]);
|
189
|
+ $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
|
|
|
|
190
|
+ return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
|
|
192
|
}
|
191
|
}
|
|
193
|
|
192
|
|
|
194
|
/**
|
193
|
/**
|
|
@@ -230,7 +229,7 @@ class FileController |
|
@@ -230,7 +229,7 @@ class FileController |
|
230
|
$hash = hash_file('md5', $file->getPathname());
|
229
|
$hash = hash_file('md5', $file->getPathname());
|
|
231
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
230
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
|
232
|
if($file_hash !== false){
|
231
|
if($file_hash !== false){
|
|
233
|
- $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
|
232
|
+ $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
|
|
234
|
continue;
|
233
|
continue;
|
|
235
|
}
|
234
|
}
|
|
236
|
$url = $this->config['root'].'/'.$this->path;
|
235
|
$url = $this->config['root'].'/'.$this->path;
|
|
@@ -238,10 +237,8 @@ class FileController |
|
@@ -238,10 +237,8 @@ class FileController |
|
238
|
//同步数据到cos
|
237
|
//同步数据到cos
|
|
239
|
if($this->upload_location == 1){
|
238
|
if($this->upload_location == 1){
|
|
240
|
$cosService = new CosService();
|
239
|
$cosService = new CosService();
|
|
241
|
- $is_cos = 1;//上传到cos
|
|
|
|
242
|
$cosService->uploadFile($files,$this->path,$fileName);
|
240
|
$cosService->uploadFile($files,$this->path,$fileName);
|
|
243
|
}else{
|
241
|
}else{
|
|
244
|
- $is_cos = 0;
|
|
|
|
245
|
$res = $files->move($url,$fileName);
|
242
|
$res = $files->move($url,$fileName);
|
|
246
|
if ($res === false) {
|
243
|
if ($res === false) {
|
|
247
|
return $this->response($files->getError(), Code::USER_ERROR);
|
244
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
@@ -249,8 +246,8 @@ class FileController |
|
@@ -249,8 +246,8 @@ class FileController |
|
249
|
}
|
246
|
}
|
|
250
|
$size = $file->getSize();
|
247
|
$size = $file->getSize();
|
|
251
|
$file_type = $file->getClientOriginalExtension();
|
248
|
$file_type = $file->getClientOriginalExtension();
|
|
252
|
- $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
|
|
|
|
253
|
- $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
|
249
|
+ $this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
|
|
|
|
250
|
+ $data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
|
|
254
|
}
|
251
|
}
|
|
255
|
return $this->response('资源',Code::SUCCESS,$data);
|
252
|
return $this->response('资源',Code::SUCCESS,$data);
|
|
256
|
}
|
253
|
}
|
|
@@ -288,23 +285,26 @@ class FileController |
|
@@ -288,23 +285,26 @@ class FileController |
|
288
|
* @method :post
|
285
|
* @method :post
|
|
289
|
* @time :2023/6/26 16:28
|
286
|
* @time :2023/6/26 16:28
|
|
290
|
*/
|
287
|
*/
|
|
291
|
- public function downLoad($hash){
|
288
|
+ public function downLoad(){
|
|
292
|
$file_model = new File();
|
289
|
$file_model = new File();
|
|
293
|
- $info = $file_model->read(['hash' => $hash]);
|
290
|
+ $info = $file_model->read(['hash' => $this->param['hash']]);
|
|
294
|
if ($info === false) {
|
291
|
if ($info === false) {
|
|
295
|
$this->response('指定文件不存在!', Code::USER_ERROR);
|
292
|
$this->response('指定文件不存在!', Code::USER_ERROR);
|
|
296
|
}
|
293
|
}
|
|
297
|
- $path = $this->config['url'].'/'.$info['path'];
|
|
|
|
298
|
- if (!is_file($path)) {
|
|
|
|
299
|
- $this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
294
|
+ if($info['is_cos'] == 1){
|
|
|
|
295
|
+
|
|
|
|
296
|
+ }else{
|
|
|
|
297
|
+ $fileUrl = $this->config['root'].'/'.$info['path'];
|
|
|
|
298
|
+ if (!is_file($fileUrl)) {
|
|
|
|
299
|
+ $this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
|
|
|
300
|
+ }
|
|
|
|
301
|
+ $fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
|
|
|
|
302
|
+ // 设置响应头
|
|
|
|
303
|
+ header('Content-Type: application/octet-stream');
|
|
|
|
304
|
+ header('Content-Disposition: attachment; filename="' . $fileName . '"');
|
|
|
|
305
|
+ // 下载文件
|
|
|
|
306
|
+ readfile($fileUrl);
|
|
300
|
}
|
307
|
}
|
|
301
|
- $fileUrl = url('upload'.$path); // 文件的 URL
|
|
|
|
302
|
- $fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
|
|
|
|
303
|
- // 设置响应头
|
|
|
|
304
|
- header('Content-Type: application/octet-stream');
|
|
|
|
305
|
- header('Content-Disposition: attachment; filename="' . $fileName . '"');
|
|
|
|
306
|
- // 下载文件
|
|
|
|
307
|
- readfile($fileUrl);
|
|
|
|
308
|
}
|
308
|
}
|
|
309
|
|
309
|
|
|
310
|
/**
|
310
|
/**
|
|
@@ -340,8 +340,8 @@ class FileController |
|
@@ -340,8 +340,8 @@ class FileController |
|
340
|
* @method :post
|
340
|
* @method :post
|
|
341
|
* @time :2023/7/20 16:46
|
341
|
* @time :2023/7/20 16:46
|
|
342
|
*/
|
342
|
*/
|
|
343
|
- public function getFileUrl($hash){
|
|
|
|
344
|
- $fileModel = new File();
|
343
|
+ public function getFileUrl(&$fileModel,$hash){
|
|
|
|
344
|
+
|
|
345
|
$info = $fileModel->read(['hash'=>$hash]);
|
345
|
$info = $fileModel->read(['hash'=>$hash]);
|
|
346
|
if($info['is_cos'] == 1){
|
346
|
if($info['is_cos'] == 1){
|
|
347
|
$cos = new CosService();
|
347
|
$cos = new CosService();
|
|
@@ -351,4 +351,21 @@ class FileController |
|
@@ -351,4 +351,21 @@ class FileController |
|
351
|
}
|
351
|
}
|
|
352
|
return $url;
|
352
|
return $url;
|
|
353
|
}
|
353
|
}
|
|
|
|
354
|
+
|
|
|
|
355
|
+ /**
|
|
|
|
356
|
+ * @remark :获取所有视频
|
|
|
|
357
|
+ * @name :getImageList
|
|
|
|
358
|
+ * @author :lyh
|
|
|
|
359
|
+ * @method :post
|
|
|
|
360
|
+ * @time :2023/6/29 11:48
|
|
|
|
361
|
+ */
|
|
|
|
362
|
+ public function getFileList(){
|
|
|
|
363
|
+ $fileModel = new File();
|
|
|
|
364
|
+ $lists = $fileModel->list(['refer'=>1],$order = 'id',['id','hash','type','path','created_at']);
|
|
|
|
365
|
+ foreach ($lists as $k => $v){
|
|
|
|
366
|
+ $v['file_link'] = $this->getFileUrl($fileModel,$v['hash']);
|
|
|
|
367
|
+ $lists[$k] = $v;
|
|
|
|
368
|
+ }
|
|
|
|
369
|
+ $this->response('success',Code::SUCCESS,$lists);
|
|
|
|
370
|
+ }
|
|
354
|
} |
371
|
} |