perf: 优化pdf在线预览

This commit is contained in:
kuaifan 2024-02-28 15:04:56 +08:00
parent cc862741dc
commit 5a4700753a
2 changed files with 97 additions and 2 deletions

View File

@ -7,6 +7,7 @@ use Cache;
use Request;
use Redirect;
use Response;
use App\Module\Doo;
use App\Models\File;
use App\Module\Base;
use App\Tasks\LoopTask;
@ -355,15 +356,31 @@ class IndexController extends InvokeController
$ext = strtolower(Arr::get($query, 'ext'));
$userAgent = strtolower(Request::server('HTTP_USER_AGENT'));
if ($ext === 'pdf') {
// 文件超过 10m 不支持在线预览,提示下载
if (filesize($file) > 10 * 1024 * 1024) {
return view('download', [
'name' => $name,
'size' => Base::readableBytes(filesize($file)),
'url' => Base::fillUrl($path),
'button' => Doo::translate('点击下载'),
]);
}
// 浏览器类型
$browser = 'none';
if (str_contains($userAgent, 'chrome')) {
$browser = str_contains($userAgent, 'android') || str_contains($userAgent, 'harmonyos') ? 'android-mobile' : 'chrome-desktop';
} elseif (str_contains($userAgent, 'safari')) {
$browser = str_contains($userAgent, 'iphone') || str_contains($userAgent, 'ipad') ? 'safari-mobile' : 'safari-desktop';
}
// electron 直接在线预览查看
if (str_contains($userAgent, 'electron')) {
if (str_contains($userAgent, 'electron') || str_contains($browser, 'desktop')) {
return Response::download($file, $name, [
'Content-Type' => 'application/pdf'
], 'inline');
}
// EEUI App 直接在线预览查看
if (str_contains($userAgent, 'eeui') && Base::judgeClientVersion("0.34.47")) {
if (str_contains($userAgent, 'iphone') || str_contains($userAgent, 'ipad')) {
if ($browser === 'safari-mobile') {
$message = Base::array2json([
'type' => 'currentOpen',
'url' => Base::fillUrl($path),

View File

@ -0,0 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta name="renderer" content="webkit">
<meta name="format-detection" content="telephone=no"/>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ $name }} - {{ config('app.name', 'WebPage') }}</title>
<link rel="shortcut icon" href="{{ asset_main('favicon.ico') }}">
<style>
* {
margin: 0;
padding: 0;
}
body {
font-size: 14px;
}
.down {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #000;
color: #fff;
padding: 16px 18px;
border-radius: 12px;;
}
.title {
font-size: 18px;
}
.link {
margin-top: 10px;
display: block;
height: 30px;
line-height: 30px;
padding: 0 12px;
text-align: center;
background-color: #f60;
color: #fff;
text-decoration: none;
border-radius: 5px;
}
.dark {
.down {
background-color: #ffffff;
color: #555;
}
}
</style>
</head>
<body>
<div class="down">
<div class="title">{{ $name }}</div>
<a class="link" href="{{ $url }}" target="_blank">{{$button}} ({{ $size }})</a>
</div>
<script>
let themeConf = window.localStorage.getItem("__system:themeConf__");
if (themeConf === 'auto') {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
themeConf = 'dark';
}
}
if (themeConf === 'dark') {
document.body.classList.add("dark");
}
</script>
</body>
</html>