This commit is contained in:
扶桑花间 2025-08-27 20:50:53 +08:00
parent 5d194115c9
commit f2c336f043
3 changed files with 40 additions and 1 deletions

View File

@ -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); return $this->writeSuccess('ok', $paginate);
} }

View File

@ -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']);
}
} }

View File

@ -6,6 +6,9 @@ use app\entity\file\UploadResult;
use app\entity\SysFileRecord; use app\entity\SysFileRecord;
use app\entity\SysFileRule; use app\entity\SysFileRule;
use app\entity\SysUserFile; 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\Event;
use think\facade\Request; use think\facade\Request;
use think\App; use think\App;
@ -26,12 +29,39 @@ class FilesystemService
$this->app = $app; $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 UploadedFile $file 上传的文件
* @param int $directory_id 存储的目录位置 * @param int $directory_id 存储的目录位置
* @param int $user_id 用户ID * @param int $user_id 用户ID
* @param array $vars * @param array $vars
* @return UploadResult * @return UploadResult
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/ */
public function upload(UploadedFile $file, int $directory_id, int $user_id, array $vars = []): UploadResult public function upload(UploadedFile $file, int $directory_id, int $user_id, array $vars = []): UploadResult
{ {