作者 lyh

gx

... ... @@ -293,22 +293,24 @@ class FileController
if ($info === false) {
$this->response('指定文件不存在!', Code::USER_ERROR);
}
if($info['is_cos'] == 1){
$cos = new CosService();
return $cos->downloadFile($info['path']);
$fileUrl = $cos->getImageUrl($info['path']);
}else{
$fileUrl = $this->config['root'].$info['path'];
if (!is_file($fileUrl)) {
$this->response('指定文件已被系统删除!', Code::USER_ERROR);
}
}
$fileName = basename($info['path']); // 要保存的文件名
// 设置响应头
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
// 下载文件
readfile($fileUrl);
}
}
/**
* @remark :保存路径
... ...
... ... @@ -61,23 +61,4 @@ class CosService
return $imageUrl;
}
/**
* @remark :下载文件
* @name :downloadFile
* @author :lyh
* @method :post
* @time :2023/7/21 14:56
*/
public function downloadFile($file_name){
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
'credentials' => [
'secretId' => $cos['credentials']['secretId'],
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$saveAs = basename($file_name);
return $cosClient->getObject(['Bucket' => $cos['bucket'], 'Key' => trim($file_name,'/'), 'SaveAs' => $saveAs]);
}
}
... ...