From f2c336f043849d3fed02a7e803fef4a5feed0553 Mon Sep 17 00:00:00 2001 From: v Date: Wed, 27 Aug 2025 20:50:53 +0800 Subject: [PATCH] up. --- .../admin/system/FileController.php | 2 +- app/model/SysFileRecord.php | 9 ++++++ app/service/file/FilesystemService.php | 30 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/app/controller/admin/system/FileController.php b/app/controller/admin/system/FileController.php index c0d73b8..1dda544 100644 --- a/app/controller/admin/system/FileController.php +++ b/app/controller/admin/system/FileController.php @@ -30,7 +30,7 @@ class FileController extends BaseController ], ]); - $paginate = CurdService::getPaginate($this->request, $model); + $paginate = CurdService::getPaginate($this->request, $model->append(['previewUrl','downloadUrl'])); return $this->writeSuccess('ok', $paginate); } diff --git a/app/model/SysFileRecord.php b/app/model/SysFileRecord.php index 6c43a00..ca2ff33 100644 --- a/app/model/SysFileRecord.php +++ b/app/model/SysFileRecord.php @@ -44,4 +44,13 @@ class SysFileRecord extends BaseModel } + public function getPreviewUrlAttr($previewUrl, $data) + { + return filesystem()->getFileUrl($data['rule_id'], $data['path'].'/'.$data['name']); + } + + public function getDownloadUrlAttr($downloadUrl, $data) + { + return filesystem()->getDownloadUrl($data['id']); + } } \ No newline at end of file diff --git a/app/service/file/FilesystemService.php b/app/service/file/FilesystemService.php index 419dd0b..2005a00 100644 --- a/app/service/file/FilesystemService.php +++ b/app/service/file/FilesystemService.php @@ -6,6 +6,9 @@ use app\entity\file\UploadResult; use app\entity\SysFileRecord; use app\entity\SysFileRule; use app\entity\SysUserFile; +use think\db\exception\DataNotFoundException; +use think\db\exception\DbException; +use think\db\exception\ModelNotFoundException; use think\facade\Event; use think\facade\Request; use think\App; @@ -26,12 +29,39 @@ class FilesystemService $this->app = $app; } + public function getDownloadUrl($fileId) + { + return $this->getFilesystemApiUrl()."/i/down/".hashids(4)->encode($fileId)."?token=".md5(uniqid()); + } + public function getFilesystemApiUrl() + { + $filesystem_apiUrl = $this->app->config->get('filesystem.api_url'); + if ($filesystem_apiUrl == "#") { + $filesystem_apiUrl = Request::domain(); + } else if (!Validate::url($filesystem_apiUrl)) { + throw new FileException('请正确配置filesystem.api_url'); + } + return $filesystem_apiUrl; + } + public function getFileUrl($rule_id, $file_path) + { + $rule = SysFileRule::find($rule_id); + if ($rule) { + $putUrl = $rule->url ? $rule->url : $this->getFilesystemApiUrl(); + return $putUrl.$file_path; + } + return ""; + } + /** * @param UploadedFile $file 上传的文件 * @param int $directory_id 存储的目录位置 * @param int $user_id 用户ID * @param array $vars * @return UploadResult + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function upload(UploadedFile $file, int $directory_id, int $user_id, array $vars = []): UploadResult {