fix: 手机下载文件名出现html的情况

This commit is contained in:
kuaifan 2023-11-01 00:13:24 +08:00
parent c587a50505
commit acddde8faa
4 changed files with 44 additions and 30 deletions

View File

@ -728,7 +728,7 @@ class DialogController extends AbstractController
$text_type = strtolower(trim(Request::input('text_type'))); $text_type = strtolower(trim(Request::input('text_type')));
$silence = in_array(strtolower(trim(Request::input('silence'))), ['yes', 'true', '1']); $silence = in_array(strtolower(trim(Request::input('silence'))), ['yes', 'true', '1']);
$markdown = in_array($text_type, ['md', 'markdown']); $markdown = in_array($text_type, ['md', 'markdown']);
// //
$result = []; $result = [];
$dialogIds = $dialog_ids ? explode(',', $dialog_ids) : [$dialog_id ?: 0]; $dialogIds = $dialog_ids ? explode(',', $dialog_ids) : [$dialog_id ?: 0];
foreach($dialogIds as $dialog_id) { foreach($dialogIds as $dialog_id) {
@ -1161,9 +1161,7 @@ class DialogController extends AbstractController
} }
// //
$filePath = public_path($array['path']); $filePath = public_path($array['path']);
return Base::streamDownload(function() use ($filePath) { return Base::streamDownload($filePath, $array['name']);
echo file_get_contents($filePath);
}, $array['name']);
} }
/** /**
@ -1576,7 +1574,7 @@ class DialogController extends AbstractController
// //
$dialogUser->color = $color; $dialogUser->color = $color;
$dialogUser->save(); $dialogUser->save();
// //
$data = [ $data = [
'id' => $dialogId, 'id' => $dialogId,
'color' => $color 'color' => $color
@ -1909,7 +1907,7 @@ class DialogController extends AbstractController
$user = User::auth(); $user = User::auth();
// //
$name = trim(Request::input('name')); $name = trim(Request::input('name'));
$link_id = intval(Request::input('link_id')); $link_id = intval(Request::input('link_id'));
$userids = Request::input('userids'); $userids = Request::input('userids');
// //
if (empty($name)) { if (empty($name)) {
@ -1944,9 +1942,9 @@ class DialogController extends AbstractController
*/ */
public function okr__push() public function okr__push()
{ {
User::auth(); User::auth();
$text = trim(Request::input('text')); $text = trim(Request::input('text'));
$userid = intval(Request::input('userid')); $userid = intval(Request::input('userid'));
// //
$botUser = User::botGetOrCreate('okr-alert'); $botUser = User::botGetOrCreate('okr-alert');
if (empty($botUser)) { if (empty($botUser)) {

View File

@ -910,7 +910,7 @@ class ProjectController extends AbstractController
$sorts = Request::input('sorts'); $sorts = Request::input('sorts');
$keys = is_array($keys) ? $keys : []; $keys = is_array($keys) ? $keys : [];
$sorts = is_array($sorts) ? $sorts : []; $sorts = is_array($sorts) ? $sorts : [];
$builder = ProjectTask::with(['taskUser', 'taskTag']); $builder = ProjectTask::with(['taskUser', 'taskTag']);
// //
if ($keys['name']) { if ($keys['name']) {
@ -988,7 +988,7 @@ class ProjectController extends AbstractController
$query->orWhere("project_task_users.userid", $userid); $query->orWhere("project_task_users.userid", $userid);
$query->orWhere("project_p_task_users.userid", $userid); $query->orWhere("project_p_task_users.userid", $userid);
}); });
// 优化子查询汇总 // 优化子查询汇总
$builder->leftJoinSub(function ($query) { $builder->leftJoinSub(function ($query) {
$query->select('task_id', DB::raw('count(*) as file_num')) $query->select('task_id', DB::raw('count(*) as file_num'))
->from('project_task_files') ->from('project_task_files')
@ -1011,10 +1011,10 @@ class ProjectController extends AbstractController
$builder->selectRaw("{$prefix}sub_task.sub_num as _sub_num"); $builder->selectRaw("{$prefix}sub_task.sub_num as _sub_num");
$builder->selectRaw("{$prefix}sub_task.sub_complete as _sub_complete"); $builder->selectRaw("{$prefix}sub_task.sub_complete as _sub_complete");
$builder->selectRaw(" $builder->selectRaw("
CAST(CASE CAST(CASE
WHEN {$prefix}project_tasks.complete_at IS NOT NULL THEN 100 WHEN {$prefix}project_tasks.complete_at IS NOT NULL THEN 100
WHEN {$prefix}sub_task.sub_complete = 0 OR {$prefix}sub_task.sub_complete IS NULL THEN 0 WHEN {$prefix}sub_task.sub_complete = 0 OR {$prefix}sub_task.sub_complete IS NULL THEN 0
ELSE ({$prefix}sub_task.sub_complete / {$prefix}sub_task.sub_num * 100) ELSE ({$prefix}sub_task.sub_complete / {$prefix}sub_task.sub_num * 100)
END AS SIGNED) as _percent END AS SIGNED) as _percent
"); ");
// //
@ -1024,7 +1024,7 @@ class ProjectController extends AbstractController
$customer->setAppends(["today","overdue"]); $customer->setAppends(["today","overdue"]);
return $customer; return $customer;
}); });
// //
$data = $list->toArray(); $data = $list->toArray();
// 还原字段 // 还原字段
foreach($data['data'] as &$item){ foreach($data['data'] as &$item){
@ -1070,7 +1070,7 @@ class ProjectController extends AbstractController
$taskid = trim(Request::input('taskid')); $taskid = trim(Request::input('taskid'));
$userid = Request::input('userid'); $userid = Request::input('userid');
$timerange = Request::input('timerange'); $timerange = Request::input('timerange');
// //
$list = ProjectTask::with(['taskUser']) $list = ProjectTask::with(['taskUser'])
->select('projects.name as project_name', 'project_tasks.id', 'project_tasks.name', 'project_tasks.start_at', 'project_tasks.end_at') ->select('projects.name as project_name', 'project_tasks.id', 'project_tasks.name', 'project_tasks.start_at', 'project_tasks.end_at')
->join('projects','project_tasks.project_id','=','projects.id') ->join('projects','project_tasks.project_id','=','projects.id')
@ -1094,7 +1094,7 @@ class ProjectController extends AbstractController
->distinct() ->distinct()
->orderByDesc('project_tasks.id') ->orderByDesc('project_tasks.id')
->paginate(Base::getPaginate(200, 100)); ->paginate(Base::getPaginate(200, 100));
// //
$list->transform(function ($customer) { $list->transform(function ($customer) {
$customer->setAppends([]); $customer->setAppends([]);
return $customer; return $customer;
@ -1659,9 +1659,7 @@ class ProjectController extends AbstractController
} }
// //
$filePath = public_path($file->getRawOriginal('path')); $filePath = public_path($file->getRawOriginal('path'));
return Base::streamDownload(function() use ($filePath) { return Base::streamDownload($filePath, $file->name);
echo file_get_contents($filePath);
}, $file->name);
} }
/** /**
@ -1786,7 +1784,7 @@ class ProjectController extends AbstractController
]); ]);
$data = ProjectTask::oneTask($task->id); $data = ProjectTask::oneTask($task->id);
$pushUserIds = ProjectTaskUser::whereTaskId($task->id)->pluck('userid')->toArray(); $pushUserIds = ProjectTaskUser::whereTaskId($task->id)->pluck('userid')->toArray();
$pushUserIds[] = ProjectUser::whereProjectId($task->project_id)->whereOwner(1)->value('userid'); $pushUserIds[] = ProjectUser::whereProjectId($task->project_id)->whereOwner(1)->value('userid');
foreach ($pushUserIds as $userId) { foreach ($pushUserIds as $userId) {
$task->pushMsg('add', $data, $userId); $task->pushMsg('add', $data, $userId);
} }

View File

@ -112,9 +112,7 @@ class FileContent extends AbstractModel
} else { } else {
$filePath = public_path($content['url']); $filePath = public_path($content['url']);
} }
return Base::streamDownload(function() use ($filePath) { return Base::streamDownload($filePath, $name);
echo file_get_contents($filePath);
}, $name);
} }
if (empty($content)) { if (empty($content)) {
$content = match ($file->type) { $content = match ($file->type) {
@ -143,9 +141,7 @@ class FileContent extends AbstractModel
if ($download) { if ($download) {
$filePath = public_path($path); $filePath = public_path($path);
if (isset($filePath)) { if (isset($filePath)) {
return Base::streamDownload(function() use ($filePath) { return Base::streamDownload($filePath, $name);
echo file_get_contents($filePath);
}, $name);
} else { } else {
abort(403, "This file not support download."); abort(403, "This file not support download.");
} }

View File

@ -11,6 +11,8 @@ use Redirect;
use Request; use Request;
use Response; use Response;
use Storage; use Storage;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\HttpFoundation\File\File;
use Validator; use Validator;
class Base class Base
@ -2870,15 +2872,35 @@ class Base
/** /**
* 流下载,解决没有后缀无法下载的问题 * 流下载,解决没有后缀无法下载的问题
* @param $callback * @param $file
* @param $name * @param $name
* @return mixed * @return mixed
*/ */
public static function streamDownload($callback, $name = null) { public static function streamDownload($file, $name = null)
{
$contentType = 'application/octet-stream';
if ($file instanceof \Closure) {
$callback = $file;
} else {
if (!$file instanceof File) {
if ($file instanceof \SplFileInfo) {
$file = new File($file->getPathname());
} else {
$file = new File((string)$file);
}
}
if (!$file->isReadable()) {
throw new FileException('File must be readable.');
}
$contentType = $file->getMimeType() ?: $contentType;
$callback = $file->getContent();
}
if ($name && !str_contains($name, '.')) { if ($name && !str_contains($name, '.')) {
$name .= "."; $name .= ".";
} }
return Response::streamDownload($callback, $name); return Response::streamDownload($callback, $name, [
'Content-Type' => $contentType,
]);
} }
/** /**