perf: 优化下载工具

This commit is contained in:
kuaifan 2025-08-15 01:02:40 +08:00
parent d4e754d601
commit eecc6c9e53
5 changed files with 58 additions and 7 deletions

View File

@ -1404,7 +1404,12 @@ class Base
*/
public static function ajaxError($msg, $data = [], $ret = 0, $abortCode = 404)
{
abort_if(Request::header('Content-Type') !== 'application/json', $abortCode, Doo::translate($msg));
if (Request::header('Content-Type') !== 'application/json') {
$translateMsg = Doo::translate($msg);
abort($abortCode, $translateMsg, [
'X-Error-Message-Base64' => base64_encode($translateMsg),
]);
}
return Base::retError($msg, $data, $ret);
}

View File

@ -31,6 +31,12 @@ function initialize(onStarted= null) {
onInterrupted: (item) => {
downloadManager.refresh(item.getSavePath());
syncDownloadItems();
// 尝试更新下载项的错误信息
downloadManager.updateError(item).then(success => {
if (success) {
syncDownloadItems();
}
});
},
onProgress: (item) => {
downloadManager.refresh(item.path);

View File

@ -70,7 +70,7 @@
<span v-else class="download-time">{{ formatTime(item.startTime) }}</span>
<!-- 状态 -->
<span v-if="item.state !== 'progressing' || item.paused" class="state" :class="getStateClass(item)">
{{ getStateText(item) }}
{{ getStateText(item) }}{{item.error ? `: ${item.error}` : ''}}
</span>
</div>
</div>

View File

@ -498,11 +498,13 @@ body.win32 {
/* Toast 提示框样式 */
.toast {
position: fixed;
left: 0;
right: 0;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
z-index: 1000;
animation: toast-slide-up 0.3s ease-out;
display: flex;
justify-content: center;
}
.toast-content {
@ -516,7 +518,7 @@ body.win32 {
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
font-size: 13px;
min-width: 200px;
white-space: nowrap;
max-width: 90vw;
}
.toast.success .toast-content {
@ -529,15 +531,17 @@ body.win32 {
.toast-message {
flex: 1;
max-height: 200px;
overflow-y: auto;
}
@keyframes toast-slide-up {
from {
opacity: 0;
transform: translate(-50%, 20px);
transform: translateY(20px);
}
to {
opacity: 1;
transform: translate(-50%, 0);
transform: translateY(0);
}
}

View File

@ -60,6 +60,7 @@ class DownloadManager {
// 添加下载项
this.downloadHistory.unshift({
...this.convert(downloadItem),
error: null,
_source: downloadItem,
});
if (this.downloadHistory.length > 1000) {
@ -101,6 +102,41 @@ class DownloadManager {
store.set('downloadHistory', this.downloadHistory);
}
/**
* 尝试更新下载项的错误信息
* @param {Electron.DownloadItem} downloadItem
*/
async updateError(downloadItem) {
const urls = downloadItem.getURLChain()
const url = urls.length > 0 ? urls[0] : downloadItem.getURL()
const path = downloadItem.getSavePath()
const item = this.downloadHistory.find(d => d.path === path)
if (!item) {
return;
}
try {
const res = await fetch(url, {
method: 'HEAD',
})
let error = null
if (res.headers.get('X-Error-Message-Base64')) {
error = Buffer.from(res.headers.get('X-Error-Message-Base64'), 'base64').toString('utf-8')
} else if (res.headers.get('X-Error-Message')) {
error = res.headers.get('X-Error-Message')
}
if (error) {
Object.assign(item, {error});
store.set('downloadHistory', this.downloadHistory);
return true;
}
} catch {
// 忽略错误
}
return false
}
/**
* 暂停下载项
* @param {string} path