fix: 下载文件出现文件损坏的情况

This commit is contained in:
kuaifan 2022-12-18 16:02:57 +08:00
parent e18fcd5c43
commit f0d0ee69c8
5 changed files with 24 additions and 10 deletions

View File

@ -970,7 +970,10 @@ class DialogController extends AbstractController
return Redirect::to(FileContent::toPreviewUrl($array)); return Redirect::to(FileContent::toPreviewUrl($array));
} }
// //
return Response::download(public_path($array['path']), $array['name']); $filePath = public_path($array['path']);
return Response::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $array['name']);
} }
/** /**

View File

@ -1175,7 +1175,7 @@ class ProjectController extends AbstractController
if (empty($file) || !file_exists(storage_path($file))) { if (empty($file) || !file_exists(storage_path($file))) {
return Base::ajaxError("文件不存在!", [], 0, 502); return Base::ajaxError("文件不存在!", [], 0, 502);
} }
return response()->download(storage_path($file)); return Response::download(storage_path($file));
} }
/** /**
@ -1385,7 +1385,10 @@ class ProjectController extends AbstractController
])); ]));
} }
// //
return Response::download(public_path($file->getRawOriginal('path')), $file->name); $filePath = public_path($file->getRawOriginal('path'));
return Response::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $file->name);
} }
/** /**

View File

@ -14,6 +14,7 @@ use Guanguans\Notify\Factory;
use Guanguans\Notify\Messages\EmailMessage; use Guanguans\Notify\Messages\EmailMessage;
use Madzipper; use Madzipper;
use Request; use Request;
use Response;
use Session; use Session;
/** /**
@ -990,7 +991,7 @@ class SystemController extends AbstractController
if (empty($file) || !file_exists(storage_path($file))) { if (empty($file) || !file_exists(storage_path($file))) {
return Base::ajaxError("文件不存在!", [], 0, 502); return Base::ajaxError("文件不存在!", [], 0, 502);
} }
return response()->download(storage_path($file)); return Response::download(storage_path($file));
} }
/** /**

View File

@ -17,6 +17,7 @@ use Hhxsv5\LaravelS\Swoole\Task\Task;
use LasseRafn\InitialAvatarGenerator\InitialAvatar; use LasseRafn\InitialAvatarGenerator\InitialAvatar;
use Redirect; use Redirect;
use Request; use Request;
use Response;
/** /**
@ -269,7 +270,7 @@ class IndexController extends InvokeController
if (preg_match("/^\d+\.\d+\.\d+$/", $genericVersion)) { if (preg_match("/^\d+\.\d+\.\d+$/", $genericVersion)) {
$filePath = public_path("uploads/desktop/{$genericVersion}/{$name}"); $filePath = public_path("uploads/desktop/{$genericVersion}/{$name}");
if (file_exists($filePath)) { if (file_exists($filePath)) {
return response()->download($filePath); return Response::download($filePath);
} }
} }
} }
@ -317,7 +318,7 @@ class IndexController extends InvokeController
$userAgent = strtolower(Request::server('HTTP_USER_AGENT')); $userAgent = strtolower(Request::server('HTTP_USER_AGENT'));
if ($ext === 'pdf' if ($ext === 'pdf'
&& (str_contains($userAgent, 'electron') || str_contains($userAgent, 'chrome'))) { && (str_contains($userAgent, 'electron') || str_contains($userAgent, 'chrome'))) {
return response()->download($file, $name, [ return Response::download($file, $name, [
'Content-Type' => 'application/pdf' 'Content-Type' => 'application/pdf'
], 'inline'); ], 'inline');
} }

View File

@ -85,7 +85,7 @@ class FileContent extends AbstractModel
* @param File $file * @param File $file
* @param $content * @param $content
* @param $download * @param $download
* @return array|\Symfony\Component\HttpFoundation\BinaryFileResponse * @return array|\Symfony\Component\HttpFoundation\StreamedResponse
*/ */
public static function formatContent($file, $content, $download = false) public static function formatContent($file, $content, $download = false)
{ {
@ -93,9 +93,13 @@ class FileContent extends AbstractModel
$content = Base::json2array($content ?: []); $content = Base::json2array($content ?: []);
if (in_array($file->type, ['word', 'excel', 'ppt'])) { if (in_array($file->type, ['word', 'excel', 'ppt'])) {
if (empty($content)) { if (empty($content)) {
return Response::download(public_path('assets/office/empty.' . str_replace(['word', 'excel', 'ppt'], ['docx', 'xlsx', 'pptx'], $file->type)), $name); $filePath = public_path('assets/office/empty.' . str_replace(['word', 'excel', 'ppt'], ['docx', 'xlsx', 'pptx'], $file->type));
} else {
$filePath = public_path($content['url']);
} }
return Response::download(public_path($content['url']), $name); return Response::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $name);
} }
if (empty($content)) { if (empty($content)) {
$content = match ($file->type) { $content = match ($file->type) {
@ -124,7 +128,9 @@ class FileContent extends AbstractModel
if ($download) { if ($download) {
$filePath = public_path($path); $filePath = public_path($path);
if (isset($filePath)) { if (isset($filePath)) {
return Response::download($filePath, $name); return Response::streamDownload(function() use ($filePath) {
echo file_get_contents($filePath);
}, $name);
} else { } else {
abort(403, "This file not support download."); abort(403, "This file not support download.");
} }