From 870276fa48b5bce8798a2386870df59ede0bf270 Mon Sep 17 00:00:00 2001 From: kuaifan Date: Mon, 24 Jan 2022 14:28:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/FileController.php | 6 +++- app/Models/FileContent.php | 33 ++++++++++++------- resources/assets/js/pages/manage/file.vue | 35 +++++++++++++++++++-- resources/assets/sass/pages/page-file.scss | 1 + 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/Api/FileController.php b/app/Http/Controllers/Api/FileController.php index e9ee4f21..a184265c 100755 --- a/app/Http/Controllers/Api/FileController.php +++ b/app/Http/Controllers/Api/FileController.php @@ -382,6 +382,9 @@ class FileController extends AbstractController * @apiParam {Number|String} id * - Number 文件ID(需要登录) * - String 链接码(不需要登录,用于预览) + * @apiParam {String} down 直接下载 + * - no: 浏览(默认) + * - yes: 下载 * * @apiSuccess {Number} ret 返回状态码(1正确、0错误) * @apiSuccess {String} msg 返回信息(错误描述) @@ -390,6 +393,7 @@ class FileController extends AbstractController public function content() { $id = Request::input('id'); + $down = Request::input('down', 'no'); // if (Base::isNumber($id)) { User::auth(); @@ -405,7 +409,7 @@ class FileController extends AbstractController } // $content = FileContent::whereFid($file->id)->orderByDesc('id')->first(); - return FileContent::formatContent($file->type, $content ? $content->content : []); + return FileContent::formatContent($file, $content?->content, $down == 'yes'); } /** diff --git a/app/Models/FileContent.php b/app/Models/FileContent.php index e40d4e7d..a1970a78 100644 --- a/app/Models/FileContent.php +++ b/app/Models/FileContent.php @@ -41,22 +41,24 @@ class FileContent extends AbstractModel use SoftDeletes; /** - * 获取格式内容 - * @param $type + * 获取格式内容(或下载) + * @param File $file * @param $content + * @param $download * @return array|\Symfony\Component\HttpFoundation\BinaryFileResponse */ - public static function formatContent($type, $content) + public static function formatContent($file, $content, $download = false) { - $content = Base::json2array($content); - if (in_array($type, ['word', 'excel', 'ppt'])) { + $name = $file->ext ? "{$file->name}.{$file->ext}" : null; + $content = Base::json2array($content ?: []); + if (in_array($file->type, ['word', 'excel', 'ppt'])) { if (empty($content)) { - return Response::download(resource_path('assets/statics/office/empty.' . str_replace(['word', 'excel', 'ppt'], ['docx', 'xlsx', 'pptx'], $type))); + return Response::download(resource_path('assets/statics/office/empty.' . str_replace(['word', 'excel', 'ppt'], ['docx', 'xlsx', 'pptx'], $file->type)), $name); } - return Response::download(public_path($content['url'])); + return Response::download(public_path($content['url']), $name); } if (empty($content)) { - $content = match ($type) { + $content = match ($file->type) { 'document' => [ "type" => "md", "content" => "", @@ -69,15 +71,24 @@ class FileContent extends AbstractModel ], default => json_decode('{}'), }; + if ($download) { + abort(403, "This file is empty."); + } } else { $content['preview'] = false; - if ($content['ext'] && !in_array($content['ext'], ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'])) { - $url = 'http://' . env('APP_IPPR') . '.3/' . $content['url']; - if (in_array($type, ['picture', 'image', 'tif', 'media'])) { + if ($file->ext && !in_array($file->ext, ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'])) { + if ($download) { + return Response::download(public_path($content['url']), $name); + } + if (in_array($file->type, ['picture', 'image', 'tif', 'media'])) { $url = Base::fillUrl($content['url']); + } else { + $url = 'http://' . env('APP_IPPR') . '.3/' . $content['url']; } $content['url'] = base64_encode($url); $content['preview'] = true; + } elseif ($download) { + abort(403, "This file not support download."); } } return Base::retSuccess('success', [ 'content' => $content ]); diff --git a/resources/assets/js/pages/manage/file.vue b/resources/assets/js/pages/manage/file.vue index fb091790..ad641cb0 100644 --- a/resources/assets/js/pages/manage/file.vue +++ b/resources/assets/js/pages/manage/file.vue @@ -104,8 +104,14 @@
- - + +