up. 图片的预览和下载,先行版
This commit is contained in:
parent
f2c336f043
commit
fa3bfda53b
@ -46,7 +46,11 @@ class SysFileRecord extends BaseModel
|
|||||||
|
|
||||||
public function getPreviewUrlAttr($previewUrl, $data)
|
public function getPreviewUrlAttr($previewUrl, $data)
|
||||||
{
|
{
|
||||||
return filesystem()->getFileUrl($data['rule_id'], $data['path'].'/'.$data['name']);
|
if(!in_array($data['extension'],['jpg','jpeg','png','gif','txt'])){
|
||||||
|
// 仅部分文件支持 preview
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return filesystem()->getFileUrl($data['rule_id'], $data['id'], $data['path'].'/'.$data['name']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDownloadUrlAttr($downloadUrl, $data)
|
public function getDownloadUrlAttr($downloadUrl, $data)
|
||||||
|
|||||||
@ -31,7 +31,7 @@ class FilesystemService
|
|||||||
|
|
||||||
public function getDownloadUrl($fileId)
|
public function getDownloadUrl($fileId)
|
||||||
{
|
{
|
||||||
return $this->getFilesystemApiUrl()."/i/down/".hashids(4)->encode($fileId)."?token=".md5(uniqid());
|
return $this->getFilesystemApiUrl()."/i/down/".hashids(12)->encode($fileId)."?token=".md5(uniqid());
|
||||||
}
|
}
|
||||||
public function getFilesystemApiUrl()
|
public function getFilesystemApiUrl()
|
||||||
{
|
{
|
||||||
@ -43,12 +43,21 @@ class FilesystemService
|
|||||||
}
|
}
|
||||||
return $filesystem_apiUrl;
|
return $filesystem_apiUrl;
|
||||||
}
|
}
|
||||||
public function getFileUrl($rule_id, $file_path)
|
public function getFileUrl($rule_id, $fileId, $file_path, $zlink = false)
|
||||||
{
|
{
|
||||||
$rule = SysFileRule::find($rule_id);
|
$rule = SysFileRule::find($rule_id);
|
||||||
if ($rule) {
|
if ($rule) {
|
||||||
$putUrl = $rule->url ? $rule->url : $this->getFilesystemApiUrl();
|
$putUrl = $rule->url ? $rule->url : $this->getFilesystemApiUrl();
|
||||||
return $putUrl.$file_path;
|
if($zlink){
|
||||||
|
return $putUrl.$file_path;
|
||||||
|
}
|
||||||
|
// 是否需要安全访问
|
||||||
|
if ($rule->permissions) {
|
||||||
|
$uploadPath = "i/" . hashids(12)->encode($fileId);
|
||||||
|
return $putUrl . '/' . ltrim($uploadPath, '/');
|
||||||
|
}else{
|
||||||
|
return $putUrl.$file_path;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,47 @@ use app\http\middleware\AuthMiddleware;
|
|||||||
use think\facade\Route;
|
use think\facade\Route;
|
||||||
use think\middleware\AllowCrossDomain;
|
use think\middleware\AllowCrossDomain;
|
||||||
|
|
||||||
|
Route::get("/i/down/:fileId", function (){
|
||||||
|
// return json(['message'=>'暂不支持下载']);
|
||||||
|
$fileId = \request()->route('fileId');
|
||||||
|
$fileExt = hashids(12)->decode($fileId)[0] ?? '';
|
||||||
|
$data = \app\entity\SysFileRecord::find($fileExt);
|
||||||
|
|
||||||
|
$fileUrl = filesystem()->getFileUrl($data['rule_id'], $data['id'], $data['path'].'/'.$data['name'], true);
|
||||||
|
if(empty($fileUrl)){
|
||||||
|
return json(['message'=>'文件不存在']);
|
||||||
|
}
|
||||||
|
$mime = "image/jpeg";
|
||||||
|
header('Content-Type: ' . $mime);
|
||||||
|
header('Content-Disposition: attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"');
|
||||||
|
header('Content-Length: 928318');
|
||||||
|
|
||||||
|
echo file_get_contents($fileUrl);
|
||||||
|
exit;
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
Route::get("/i/:fileId", function (){
|
||||||
|
$fileId = \request()->route('fileId');
|
||||||
|
$fileExt = hashids(12)->decode($fileId)[0] ?? '';
|
||||||
|
$data = \app\entity\SysFileRecord::find($fileExt);
|
||||||
|
|
||||||
|
$fileUrl = filesystem()->getFileUrl($data['rule_id'], $data['id'], $data['path'].'/'.$data['name'], true);
|
||||||
|
if(empty($fileUrl)){
|
||||||
|
return json(['message'=>'文件不存在']);
|
||||||
|
}
|
||||||
|
$mime = "image/jpeg";
|
||||||
|
header('Content-Type: ' . $mime);
|
||||||
|
// header('Content-Disposition: attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"');
|
||||||
|
// header('Content-Length: 928318');
|
||||||
|
echo file_get_contents($fileUrl);
|
||||||
|
exit;
|
||||||
|
// $fileCo = file_get_contents($fileUrl);
|
||||||
|
// $base64Image = base64_encode($fileCo);
|
||||||
|
// echo '<img src="data:image/jpeg;base64,' . $base64Image . '" alt="My Image">';
|
||||||
|
// exit;
|
||||||
|
});
|
||||||
|
|
||||||
Route::group("adminapi", function () {
|
Route::group("adminapi", function () {
|
||||||
/*
|
/*
|
||||||
* 用户
|
* 用户
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user