From 9f7cdc34b4a4a170ade9165cd8d7a35813068c3c Mon Sep 17 00:00:00 2001 From: kuaifan Date: Wed, 22 Feb 2023 12:41:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B2=A1=E6=9C=89=E5=90=8E=E7=BC=80?= =?UTF-8?q?=E5=90=8D=E6=97=A0=E6=B3=95=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/Api/DialogController.php | 3 +-- app/Http/Controllers/Api/ProjectController.php | 2 +- app/Models/FileContent.php | 5 ++--- app/Module/Base.php | 14 ++++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index b93cb4886..dbe84543a 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -18,7 +18,6 @@ use Carbon\Carbon; use DB; use Redirect; use Request; -use Response; /** * @apiDefine dialog @@ -1022,7 +1021,7 @@ class DialogController extends AbstractController } // $filePath = public_path($array['path']); - return Response::streamDownload(function() use ($filePath) { + return Base::streamDownload(function() use ($filePath) { echo file_get_contents($filePath); }, $array['name']); } diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index 79c621aab..5f7475709 100755 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -1507,7 +1507,7 @@ class ProjectController extends AbstractController } // $filePath = public_path($file->getRawOriginal('path')); - return Response::streamDownload(function() use ($filePath) { + return Base::streamDownload(function() use ($filePath) { echo file_get_contents($filePath); }, $file->name); } diff --git a/app/Models/FileContent.php b/app/Models/FileContent.php index 96b2c0467..0bddedf14 100644 --- a/app/Models/FileContent.php +++ b/app/Models/FileContent.php @@ -5,7 +5,6 @@ namespace App\Models; use App\Module\Base; use Illuminate\Database\Eloquent\SoftDeletes; -use Response; /** * App\Models\FileContent @@ -97,7 +96,7 @@ class FileContent extends AbstractModel } else { $filePath = public_path($content['url']); } - return Response::streamDownload(function() use ($filePath) { + return Base::streamDownload(function() use ($filePath) { echo file_get_contents($filePath); }, $name); } @@ -128,7 +127,7 @@ class FileContent extends AbstractModel if ($download) { $filePath = public_path($path); if (isset($filePath)) { - return Response::streamDownload(function() use ($filePath) { + return Base::streamDownload(function() use ($filePath) { echo file_get_contents($filePath); }, $name); } else { diff --git a/app/Module/Base.php b/app/Module/Base.php index 6be07b9a0..e6f1f01f6 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -11,6 +11,7 @@ use Illuminate\Support\Facades\Config; use Overtrue\Pinyin\Pinyin; use Redirect; use Request; +use Response; use Storage; use Validator; @@ -3182,4 +3183,17 @@ class Base throw new ApiException($validator->errors()->first()); } } + + /** + * 流下载,解决没有后缀无法下载的问题 + * @param $callback + * @param $name + * @return mixed + */ + public static function streamDownload($callback, $name = null) { + if ($name && !str_contains($name, '.')) { + $name .= "."; + } + return Response::streamDownload($callback, $name); + } }