Files
crawler-plugin/frontend-vue/src/shared/utils/download-progress.ts
T
huangzd1997 a3b01f1f50 fix(安全/健壮性): 全工作区审查修复——鉴权兜底扩展+路径穿越+忙等+泄漏
- AdminApiGuardFilter 兜底扩展到 /api/collect-data、/api/price-track:无需鉴权的
  工具接口纳入 JWT/内部令牌校验(原匿名可达即越权读写他人数据)
- pricetrack asinFiles 改为仅允许上传临时目录内文件(canonical 前缀校验),
  修复请求路径直接 new File 可读服务器任意 csv/xlsx 的穿越
- dedupe 删除导入逐行 REQUIRES_NEW 事务改 500 条一批 IN 删除,50 万行导入
  由 50 万个事务收敛为千级
- 前端记住密码 XOR 硬编码密钥改 WebCrypto AES-GCM(密钥随机生成独立存储),
  登录流程接口改 async 并保证自动登录恢复时序
- 任务进度轮询失败按指数退避(原固定 5s 无限撞);下载进度终态条目 2 分钟
  自动清理(原永久堆积);AmazonConsolePage statusTimer 卸载清理
2026-09-11 17:11:43 +08:00

203 lines
5.8 KiB
TypeScript

import { computed, reactive } from 'vue'
import { getPywebviewApi } from '@/shared/bridges/pywebview'
export type DownloadProgressStatus = 'selecting' | 'running' | 'success' | 'failed' | 'cancelled'
export type DownloadProgressItem = {
id: string
filename: string
status: DownloadProgressStatus
path?: string
downloaded: number
total: number
percent: number
error?: string
createdAt: number
updatedAt: number
}
type PywebviewDownloadProgressEvent = {
id: string
status: 'running' | 'success' | 'failed'
path?: string
downloaded: number
total: number
percent: number
error?: string
}
const progressItems = reactive<Record<string, DownloadProgressItem>>({})
let progressListenerBound = false
/** 终态条目自动清理延迟:success/failed 未手动关闭也只保留 2 分钟,防长驻累积 */
const TERMINAL_RETENTION_MS = 2 * 60 * 1000
let sweepTimer: ReturnType<typeof setInterval> | null = null
function sweepTerminalItems() {
const cutoff = now() - TERMINAL_RETENTION_MS
for (const id of Object.keys(progressItems)) {
const item = progressItems[id]
if ((item.status === 'success' || item.status === 'failed') && item.updatedAt <= cutoff) {
delete progressItems[id]
}
}
if (Object.keys(progressItems).length === 0 && sweepTimer) {
clearInterval(sweepTimer)
sweepTimer = null
}
}
function ensureSweepTimer() {
if (sweepTimer || typeof window === 'undefined') return
sweepTimer = setInterval(sweepTerminalItems, 30 * 1000)
}
function normalizePercent(value: number) {
if (!Number.isFinite(value)) return 0
return Math.max(0, Math.min(100, Math.round(value)))
}
function now() {
return Date.now()
}
function upsertProgress(partial: Omit<Partial<DownloadProgressItem>, 'id'> & { id: string }) {
ensureSweepTimer()
const existing = progressItems[partial.id]
const timestamp = now()
progressItems[partial.id] = {
id: partial.id,
filename: partial.filename || existing?.filename || '下载文件',
status: partial.status || existing?.status || 'running',
path: partial.path ?? existing?.path,
downloaded: Number(partial.downloaded ?? existing?.downloaded ?? 0),
total: Number(partial.total ?? existing?.total ?? 0),
percent: normalizePercent(Number(partial.percent ?? existing?.percent ?? 0)),
error: partial.error ?? existing?.error,
createdAt: existing?.createdAt || timestamp,
updatedAt: timestamp,
}
}
function handlePywebviewProgress(event: Event) {
const detail = (event as CustomEvent<PywebviewDownloadProgressEvent>).detail
if (!detail?.id) return
upsertProgress({
id: detail.id,
status: detail.status,
path: detail.path,
downloaded: detail.downloaded,
total: detail.total,
percent: detail.percent,
error: detail.error,
})
}
export function ensureDownloadProgressListener() {
if (progressListenerBound || typeof window === 'undefined') return
progressListenerBound = true
window.addEventListener('pywebview-download-progress', handlePywebviewProgress)
}export function useDownloadProgress() {
ensureDownloadProgressListener()
const items = computed(() =>
Object.values(progressItems)
.filter((item) => item.status !== 'cancelled')
.sort((a, b) => b.createdAt - a.createdAt),
)
return {
items,
clearDownloadProgress,
}
}
export function clearDownloadProgress(id: string) {
delete progressItems[id]
}
function buildDownloadId(filename: string) {
const safeName = filename.replace(/[^a-zA-Z0-9._-]+/g, '-').slice(0, 60) || 'download'
return `download:${Date.now()}:${Math.random().toString(16).slice(2)}:${safeName}`
}
export async function saveUrlWithProgress(url: string, filename: string, id = buildDownloadId(filename)) {
ensureDownloadProgressListener()
const api = getPywebviewApi()
if (!api?.save_file_from_url_new) {
upsertProgress({
id,
filename,
status: 'failed',
downloaded: 0,
total: 0,
percent: 0,
error: '当前客户端未提供下载能力',
})
return { success: false, error: '当前客户端未提供下载能力' }
}
upsertProgress({
id,
filename,
status: 'selecting',
downloaded: 0,
total: 0,
percent: 0,
})
const result = api.save_file_from_url_with_progress
? await api.save_file_from_url_with_progress(url, filename, id)
: await api.save_file_from_url_new(url, filename)
if (result.success) {
const existing = progressItems[id]
upsertProgress({
id,
filename,
status: 'success',
path: result.path,
downloaded: existing?.downloaded || existing?.total || 0,
total: existing?.total || existing?.downloaded || 0,
percent: 100,
})
} else if (result.error === '用户取消') {
upsertProgress({
id,
filename,
status: 'cancelled',
downloaded: 0,
total: 0,
percent: 0,
})
clearDownloadProgress(id)
} else {
upsertProgress({
id,
filename,
status: 'failed',
downloaded: 0,
total: 0,
percent: 0,
error: result.error || '下载失败',
})
}
return result
}
export function formatDownloadBytes(value: number) {
const size = Number(value || 0)
if (!Number.isFinite(size) || size <= 0) return '未知大小'
if (size < 1024) return `${size} B`
if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`
if (size < 1024 * 1024 * 1024) return `${(size / 1024 / 1024).toFixed(1)} MB`
return `${(size / 1024 / 1024 / 1024).toFixed(2)} GB`
}
export function downloadProgressText(item: DownloadProgressItem) {
if (item.status === 'selecting') return '等待选择保存位置'
if (item.status === 'success') return '下载完成'
if (item.status === 'failed') return item.error || '下载失败'
return item.total > 0 ? '正在下载,请等待完成后再打开' : '正在下载,正在获取文件大小'
}