|
...
|
...
|
@@ -38,8 +38,9 @@ class FileController |
|
|
|
public $cache = '';//缓存数据
|
|
|
|
public $upload_location = 1;
|
|
|
|
public $file_type = [
|
|
|
|
1 => 'file_other',
|
|
|
|
0 => 'file'
|
|
|
|
2 => 'other',//其他
|
|
|
|
1 => 'video',//视频
|
|
|
|
0 => 'file'//文件
|
|
|
|
];
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
...
|
...
|
@@ -169,7 +170,7 @@ class FileController |
|
|
|
$hash = hash_file('md5', $files->getPathname());
|
|
|
|
//查看文件是否存在
|
|
|
|
$fileModel = new File();
|
|
|
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
|
|
|
$file_hash = $fileModel->read(['hash'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
|
|
|
|
if($file_hash !== false){
|
|
|
|
return $this->response('资源',Code::SUCCESS,['file'=>$hash]);
|
|
|
|
}
|
|
...
|
...
|
@@ -178,17 +179,15 @@ class FileController |
|
|
|
//同步数据到cos
|
|
|
|
if($this->upload_location == 1){
|
|
|
|
$cosService = new CosService();
|
|
|
|
$is_cos = 1;//上传到cos
|
|
|
|
$cosService->uploadFile($files,$this->path,$fileName);
|
|
|
|
}else{
|
|
|
|
$is_cos = 0;
|
|
|
|
$res = $files->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
|
|
|
|
return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($hash)]);
|
|
|
|
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
|
|
|
|
return $this->response('资源',Code::SUCCESS,['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -230,7 +229,7 @@ class FileController |
|
|
|
$hash = hash_file('md5', $file->getPathname());
|
|
|
|
$file_hash = $fileModel->read(['hash'=>$hash]);
|
|
|
|
if($file_hash !== false){
|
|
|
|
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
|
|
|
|
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$url = $this->config['root'].'/'.$this->path;
|
|
...
|
...
|
@@ -238,10 +237,8 @@ class FileController |
|
|
|
//同步数据到cos
|
|
|
|
if($this->upload_location == 1){
|
|
|
|
$cosService = new CosService();
|
|
|
|
$is_cos = 1;//上传到cos
|
|
|
|
$cosService->uploadFile($files,$this->path,$fileName);
|
|
|
|
}else{
|
|
|
|
$is_cos = 0;
|
|
|
|
$res = $files->move($url,$fileName);
|
|
|
|
if ($res === false) {
|
|
|
|
return $this->response($files->getError(), Code::USER_ERROR);
|
|
...
|
...
|
@@ -249,8 +246,8 @@ class FileController |
|
|
|
}
|
|
|
|
$size = $file->getSize();
|
|
|
|
$file_type = $file->getClientOriginalExtension();
|
|
|
|
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$is_cos);
|
|
|
|
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($hash)];
|
|
|
|
$this->saveMysql($fileModel,$size,$file_type,$fileName,$hash,$this->upload_location);
|
|
|
|
$data[] = ['file'=>$hash,'file_link'=>$this->getFileUrl($fileModel,$hash)];
|
|
|
|
}
|
|
|
|
return $this->response('资源',Code::SUCCESS,$data);
|
|
|
|
}
|
|
...
|
...
|
@@ -288,23 +285,26 @@ class FileController |
|
|
|
* @method :post
|
|
|
|
* @time :2023/6/26 16:28
|
|
|
|
*/
|
|
|
|
public function downLoad($hash){
|
|
|
|
public function downLoad(){
|
|
|
|
$file_model = new File();
|
|
|
|
$info = $file_model->read(['hash' => $hash]);
|
|
|
|
$info = $file_model->read(['hash' => $this->param['hash']]);
|
|
|
|
if ($info === false) {
|
|
|
|
$this->response('指定文件不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$path = $this->config['url'].'/'.$info['path'];
|
|
|
|
if (!is_file($path)) {
|
|
|
|
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
|
|
|
if($info['is_cos'] == 1){
|
|
|
|
|
|
|
|
}else{
|
|
|
|
$fileUrl = $this->config['root'].'/'.$info['path'];
|
|
|
|
if (!is_file($fileUrl)) {
|
|
|
|
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
|
|
|
|
// 设置响应头
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
|
|
header('Content-Disposition: attachment; filename="' . $fileName . '"');
|
|
|
|
// 下载文件
|
|
|
|
readfile($fileUrl);
|
|
|
|
}
|
|
|
|
$fileUrl = url('upload'.$path); // 文件的 URL
|
|
|
|
$fileName = 'downloaded_file'.'.'.$info['type']; // 要保存的文件名
|
|
|
|
// 设置响应头
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
|
|
header('Content-Disposition: attachment; filename="' . $fileName . '"');
|
|
|
|
// 下载文件
|
|
|
|
readfile($fileUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
...
|
...
|
@@ -340,8 +340,8 @@ class FileController |
|
|
|
* @method :post
|
|
|
|
* @time :2023/7/20 16:46
|
|
|
|
*/
|
|
|
|
public function getFileUrl($hash){
|
|
|
|
$fileModel = new File();
|
|
|
|
public function getFileUrl(&$fileModel,$hash){
|
|
|
|
|
|
|
|
$info = $fileModel->read(['hash'=>$hash]);
|
|
|
|
if($info['is_cos'] == 1){
|
|
|
|
$cos = new CosService();
|
|
...
|
...
|
@@ -351,4 +351,21 @@ class FileController |
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :获取所有视频
|
|
|
|
* @name :getImageList
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2023/6/29 11:48
|
|
|
|
*/
|
|
|
|
public function getFileList(){
|
|
|
|
$fileModel = new File();
|
|
|
|
$lists = $fileModel->list(['refer'=>1],$order = 'id',['id','hash','type','path','created_at']);
|
|
|
|
foreach ($lists as $k => $v){
|
|
|
|
$v['file_link'] = $this->getFileUrl($fileModel,$v['hash']);
|
|
|
|
$lists[$k] = $v;
|
|
|
|
}
|
|
|
|
$this->response('success',Code::SUCCESS,$lists);
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|