Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.
TypeScript albumsexif-extractiongeocodinglive-photomotion-photophoto-galleryphoto-sharingwebgl-viewer
feat(upload): add concurrent chunks and provider-aware direct uploads
Timothy Yin87131b9a4935
diff --git a/app/components/ui/UploadQueueItem.vue b/app/components/ui/UploadQueueItem.vueindex d2b2693b..a8002dc9 100644--- a/app/components/ui/UploadQueueItem.vue+++ b/app/components/ui/UploadQueueItem.vue@@ -10,6 +10,7 @@ interface UploadFile {| 'waiting'| 'preparing'| 'uploading'+ | 'finalizing'| 'processing'| 'completed'| 'error'@@ -59,6 +60,7 @@ const statusColor = computed(() => {case 'preparing':return 'primary'case 'uploading':+ case 'finalizing':return 'primary'case 'processing':return 'info'@@ -75,33 +77,6 @@ const statusColor = computed(() => {}})-const statusText = computed(() => {- switch (props.uploadingFile.status) {- case 'waiting':- return $t('dashboard.photos.uploadQueueItem.status.waiting')- case 'preparing':- return $t('dashboard.photos.uploadQueueItem.status.preparing')- case 'uploading':- return $t('dashboard.photos.uploadQueueItem.status.uploading', [- props.uploadingFile.progress || 0,- ])- case 'processing':- return props.uploadingFile.stage- ? getStageText(props.uploadingFile.stage)- : $t('dashboard.photos.uploadQueueItem.status.pendingProcessing')- case 'completed':- return $t('dashboard.photos.uploadQueueItem.status.completed')- case 'error':- return $t('dashboard.photos.uploadQueueItem.status.error')- case 'skipped':- return $t('dashboard.photos.uploadQueueItem.status.skipped')- case 'blocked':- return $t('dashboard.photos.uploadQueueItem.status.blocked')- default:- return $t('dashboard.photos.uploadQueueItem.status.unknown')- }-})-// 获取处理阶段文本const getStageText = (stage: string) => {const stageMap: Record<string, string> = {@@ -316,13 +291,6 @@ const generateParticleStyle = (index: number) => {>{{ uploadingFile.fileName }}</p>- <UBadge- :color="statusColor"- variant="soft"- size="sm"- >- {{ statusText }}- </UBadge></div><div@@ -331,12 +299,11 @@ const generateParticleStyle = (index: number) => {<span>{{ formatBytes(uploadingFile.file.size) }}</span><spanv-if="- uploadingFile.uploadProgress?.speedText &&uploadingFile.status === 'uploading'"- class="hidden sm:inline"+ class="inline tabular-nums">- • {{ uploadingFile.uploadProgress.speedText }}+ • {{ uploadingFile.uploadProgress?.speedText || '0 B/s' }}</span></div></div>@@ -346,7 +313,10 @@ const generateParticleStyle = (index: number) => {<div class="flex items-center gap-2 ml-2"><!-- 中止上传按钮 --><motion.div- v-if="uploadingFile.status === 'uploading' && uploadingFile.canAbort"+ v-if="+ (uploadingFile.status === 'uploading' || uploadingFile.status === 'finalizing') &&+ uploadingFile.canAbort+ ":initial="{ opacity: 0, scale: 0.8 }":animate="{ opacity: 1, scale: 1 }":exit="{ opacity: 0, scale: 0.8 }"@@ -392,6 +362,7 @@ const generateParticleStyle = (index: number) => {<motion.divv-if="uploadingFile.status === 'uploading' ||+ uploadingFile.status === 'finalizing' ||uploadingFile.status === 'processing'":initial="{ opacity: 0, height: 0 }"@@ -429,14 +400,19 @@ const generateParticleStyle = (index: number) => {</motion.div><div- v-if="uploadingFile.uploadProgress?.timeRemainingText"- class="text-xs text-neutral-500 dark:text-neutral-400"+ class="text-xs text-neutral-500 dark:text-neutral-400 tabular-nums">- {{ $t('dashboard.photos.uploadQueueItem.progress.remainingTime', [uploadingFile.uploadProgress.timeRemainingText]) }}+ {{ $t('dashboard.photos.uploadQueueItem.progress.remainingTime', [uploadingFile.uploadProgress?.timeRemainingText || '—']) }}</div></div><!-- 处理进度 -->+ <div v-if="uploadingFile.status === 'finalizing'" class="space-y-1">+ <span class="text-xs text-neutral-600 dark:text-neutral-400">+ {{ $t('dashboard.photos.uploadQueueItem.status.finalizing') }}+ </span>+ <UProgress :model-value="null" animation="swing" color="primary" />+ </div><divv-if="uploadingFile.status === 'processing'"class="space-y-1"diff --git a/app/components/ui/UploadQueuePanel.vue b/app/components/ui/UploadQueuePanel.vueindex 39f5d466..82bcb65b 100644--- a/app/components/ui/UploadQueuePanel.vue+++ b/app/components/ui/UploadQueuePanel.vue@@ -9,6 +9,7 @@ interface UploadFile {| 'waiting'| 'preparing'| 'uploading'+ | 'finalizing'| 'processing'| 'completed'| 'error'@@ -52,14 +53,19 @@ const stats = computed(() => {return {total: files.length,waiting: files.filter((f) => f.status === 'waiting').length,- uploading: files.filter((f) => f.status === 'uploading').length,+ uploading: files.filter(+ (f) => f.status === 'uploading' || f.status === 'finalizing',+ ).length,processing: files.filter((f) => f.status === 'processing').length,completed: files.filter((f) => f.status === 'completed').length,error: files.filter((f) => f.status === 'error').length,skipped: files.filter((f) => f.status === 'skipped').length,blocked: files.filter((f) => f.status === 'blocked').length,active: files.filter(- (f) => f.status === 'uploading' || f.status === 'processing',+ (f) =>+ f.status === 'uploading' ||+ f.status === 'finalizing' ||+ f.status === 'processing',).length,pending: files.filter((f) => f.status === 'waiting' || f.status === 'preparing',@@ -80,7 +86,7 @@ const overallProgress = computed(() => {} else if (file.status === 'uploading' && file.progress !== undefined) {// 上传中:上传进度 * 0.7(上传占总进度的70%)totalProgress += file.progress * 0.7- } else if (file.status === 'processing') {+ } else if (file.status === 'processing' || file.status === 'finalizing') {// 处理中:上传完成(70%)totalProgress += 70} else if (file.status === 'preparing') {diff --git a/app/composables/useUpload.ts b/app/composables/useUpload.tsindex 580e50c7..af2bb366 100644--- a/app/composables/useUpload.ts+++ b/app/composables/useUpload.ts@@ -1,3 +1,6 @@+import { UPLOAD_CHUNK_SIZE } from '~~/shared/utils/upload'+import type { UploadPlan, UploadPart } from '~~/shared/types/upload'+export interface UploadProgress {loaded: numbertotal: number@@ -7,7 +10,7 @@ export interface UploadProgress {}export interface UploadStatus {- status: 'idle' | 'uploading' | 'success' | 'error' | 'aborted'+ status: 'idle' | 'uploading' | 'finalizing' | 'success' | 'error' | 'aborted'progress: UploadProgresserror?: stringstartTime?: number@@ -25,9 +28,11 @@ export interface UploadCallbacks {export interface UseUploadOptions {timeout?: number // 超时时间(毫秒)+ finalizeTimeout?: number+ chunkConcurrency?: number // 每个文件同时上传的分片数量(1–6)withCredentials?: booleanheaders?: Record<string, string>- speedSampleSize?: number // 用于计算速度的样本数量+ speedSampleSize?: number // 速度平均窗口(秒)maxRetries?: number // 最大重试次数retryDelay?: number // 重试延迟(毫秒)}@@ -40,7 +45,9 @@ export function useUpload(options: UseUploadOptions = {}) {const { $i18n } = useNuxtApp()const t = $i18n.tconst {- timeout = 0,+ timeout = 60_000,+ finalizeTimeout = 10 * 60_000,+ chunkConcurrency = 3,withCredentials = false,headers = {},speedSampleSize = 5,@@ -60,19 +67,41 @@ export function useUpload(options: UseUploadOptions = {}) {// 当前的 XMLHttpRequest 实例let currentXHR: XMLHttpRequest | null = null+ const activeXHRs = new Set<XMLHttpRequest>()+ const retryWaits = new Map<ReturnType<typeof setTimeout>, () => void>()+ const stopRequests = () => {+ for (const xhr of activeXHRs) xhr.abort()+ for (const [timer, resolve] of retryWaits) {+ clearTimeout(timer)+ resolve()+ }+ retryWaits.clear()+ }// 用于计算速度的数据点- const speedSamples: Array<{ timestamp: number; loaded: number }> = []+ const speedSamples: Array<{+ timestamp: number+ loaded: number+ useful: number+ }> = []+ let transferred = 0// 计算上传速度和剩余时间const calculateSpeed = (loaded: number,): { speed: number; timeRemaining?: number } => {- const now = Date.now()- speedSamples.push({ timestamp: now, loaded })+ const now = performance.now()+ const previous = speedSamples.at(-1)+ if (!previous || now - previous.timestamp >= 200) {+ speedSamples.push({ timestamp: now, loaded: transferred, useful: loaded })+ }- // 保持样本数量在限制内- if (speedSamples.length > speedSampleSize) {+ // Use a time window so short pauses between parts do not erase the estimate.+ const windowStart = now - Math.max(1, speedSampleSize) * 1000+ while (+ speedSamples.length > 2 &&+ speedSamples[1]!.timestamp <= windowStart+ ) {speedSamples.shift()}@@ -91,12 +120,19 @@ export function useUpload(options: UseUploadOptions = {}) {const timeDiff = (lastSample.timestamp - firstSample.timestamp) / 1000 // 转换为秒const bytesDiff = lastSample.loaded - firstSample.loaded- const speed = timeDiff > 0 ? bytesDiff / timeDiff : 0+ const speed = timeDiff > 0 ? Math.max(0, bytesDiff / timeDiff) : 0// 计算剩余时间const total = uploadStatus.value.progress.totalconst remaining = total - loaded- const timeRemaining = speed > 0 ? remaining / speed : undefined+ // Retransmitted bytes consume bandwidth but do not reduce the remaining+ // file size. Estimate from useful progress rather than wire throughput.+ const usefulSpeed =+ timeDiff > 0+ ? Math.max(0, (lastSample.useful - firstSample.useful) / timeDiff)+ : 0+ const timeRemaining =+ usefulSpeed > 0 && remaining > 0 ? remaining / usefulSpeed : undefinedreturn { speed, timeRemaining }}@@ -108,15 +144,26 @@ export function useUpload(options: UseUploadOptions = {}) {// 更新进度const updateProgress = (loaded: number, total: number) => {- const percentage = total > 0 ? Math.round((loaded / total) * 100) : 0- const { speed, timeRemaining } = calculateSpeed(loaded)+ loaded = Math.min(+ total,+ Math.max(uploadStatus.value.progress.loaded, loaded),+ )+ const percentage =+ total > 0+ ? Math.min(+ uploadStatus.value.status === 'uploading' ? 99 : 100,+ Math.floor((loaded / total) * 100),+ )+ : 0+ const finalizing = uploadStatus.value.status === 'finalizing'+ const metrics = calculateSpeed(loaded)const progress: UploadProgress = {loaded,total,percentage,- speed,- timeRemaining,+ speed: finalizing ? 0 : metrics.speed,+ timeRemaining: finalizing ? undefined : metrics.timeRemaining,}updateStatus({ progress })@@ -125,6 +172,7 @@ export function useUpload(options: UseUploadOptions = {}) {// 重置状态const resetStatus = () => {speedSamples.length = 0+ transferred = 0updateStatus({status: 'idle',progress: {@@ -138,244 +186,284 @@ export function useUpload(options: UseUploadOptions = {}) {})}- // 主要的上传函数+ let cancelled = false+ let active = false+const uploadFile = async (file: File,- signedUrl: string,+ target: string | UploadPlan,callbacks: UploadCallbacks = {},- attempt: number = 1,): Promise<XMLHttpRequest> => {- // 重置状态(仅在第一次尝试时)- if (attempt === 1) {- resetStatus()- }-- return new Promise((resolve, reject) => {- // 创建新的 XHR 实例- currentXHR = new XMLHttpRequest()- const xhr = currentXHR-- // 设置超时- if (timeout > 0) {- xhr.timeout = timeout+ if (active) throw new Error('An upload is already active')+ resetStatus()+ cancelled = false+ active = true+ updateStatus({ status: 'uploading', startTime: Date.now() })+ callbacks.onStatusChange?.('uploading')+ const plan: UploadPlan =+ typeof target === 'string'+ ? {+ mode: target.startsWith('/api/photos/chunks/')+ ? 'chunks'+ : 'single',+ url: target,+ }+ : target+ const signedUrl = plan.url+ const chunked = plan.mode !== 'single'+ const managedSession = plan.mode === 'chunks' || plan.mode === 'multipart'+ let stopped = false+ let failureXHR: XMLHttpRequest | null = null+ let loadedTotal = 0+ const partProgress = new Map<number, number>()+ const recordProgress = (index: number, loaded: number) => {+ const previous = partProgress.get(index) ?? 0+ const next = Math.max(previous, loaded)+ partProgress.set(index, next)+ loadedTotal += next - previous+ if (+ loadedTotal >= file.size &&+ uploadStatus.value.status === 'uploading'+ ) {+ updateStatus({ status: 'finalizing' })+ callbacks.onStatusChange?.('finalizing')}-- // 设置是否携带凭证- xhr.withCredentials = withCredentials-- // 记录开始时间- const startTime = Date.now()- updateStatus({ status: 'uploading', startTime })- callbacks.onStatusChange?.('uploading')-- // 进度事件处理- xhr.upload.addEventListener('progress', (event) => {- if (event.lengthComputable) {- updateProgress(event.loaded, event.total)- callbacks.onProgress?.(uploadStatus.value.progress)- }- })-- // 成功处理- xhr.addEventListener('load', () => {- const endTime = Date.now()- updateStatus({ status: 'success', endTime })- callbacks.onStatusChange?.('success')- callbacks.onSuccess?.(xhr)- resolve(xhr)- })-- // 错误处理- xhr.addEventListener('error', () => {- const endTime = Date.now()- let errorMessage = ''-- if (xhr.status === 0) {- // 状态码 0 通常表示网络连接失败、CORS 问题或服务器不可达- errorMessage = t('upload.runtimeError.networkFailed')- } else if (xhr.status >= 400 && xhr.status < 500) {- errorMessage = t('upload.runtimeError.clientError', { status: xhr.status })- } else if (xhr.status >= 500) {- errorMessage = t('upload.runtimeError.serverError', { status: xhr.status })- } else {- errorMessage = t('upload.runtimeError.networkError', { status: xhr.status })- }-- // 检查是否可以重试- const canRetry =- attempt < maxRetries &&- (xhr.status === 0 || // 网络错误- xhr.status >= 500 || // 服务器错误- xhr.status === 429) // 频率限制-- if (canRetry) {- updateStatus({- status: 'error',- error: t('upload.runtimeError.retrying', {- message: errorMessage,- attempt,- max: maxRetries,- }),- endTime,+ updateProgress(loadedTotal, file.size)+ }+ updateProgress(0, file.size)+ callbacks.onProgress?.(uploadStatus.value.progress)+ // Update even when the connection stalls, so stale speed/ETA do not linger.+ const progressTimer = setInterval(() => {+ updateProgress(uploadStatus.value.progress.loaded, file.size)+ callbacks.onProgress?.(uploadStatus.value.progress)+ }, 250)+ const request = async (+ method: string,+ url: string,+ body?: Blob,+ partIndex = 0,+ requestHeaders: Record<string, string> = {},+ filePayload = true,+ ): Promise<XMLHttpRequest> => {+ for (let attempt = 1; ; attempt++) {+ if (cancelled || stopped)+ throw new Error(t('upload.runtimeError.aborted'))+ let xhr: XMLHttpRequest | undefined+ try {+ return await new Promise<XMLHttpRequest>((resolve, reject) => {+ const requestXHR = new XMLHttpRequest()+ xhr = requestXHR+ let sent = 0+ currentXHR = requestXHR+ activeXHRs.add(requestXHR)+ requestXHR.open(method, url)+ requestXHR.timeout =+ !filePayload || plan.mode === 'single' ? finalizeTimeout : timeout+ requestXHR.withCredentials = withCredentials+ if (body)+ requestXHR.setRequestHeader(+ 'Content-Type',+ filePayload+ ? file.type || 'application/octet-stream'+ : 'application/json',+ )+ Object.entries({ ...headers, ...requestHeaders }).forEach(+ ([key, value]) => requestXHR.setRequestHeader(key, value),+ )+ requestXHR.upload.addEventListener('progress', (event) => {+ if (event.lengthComputable && filePayload) {+ const loaded = Math.min(body?.size ?? 0, event.loaded)+ transferred += Math.max(0, loaded - sent)+ sent = Math.max(sent, loaded)+ recordProgress(partIndex, loaded)+ }+ })+ requestXHR.addEventListener('load', () => {+ if (requestXHR.status >= 200 && requestXHR.status < 300) {+ if (filePayload) {+ transferred += Math.max(0, (body?.size ?? 0) - sent)+ if (body) recordProgress(partIndex, body.size)+ }+ resolve(requestXHR)+ } else+ reject(+ new Error(+ t('upload.runtimeError.httpError', {+ status: requestXHR.status,+ }),+ ),+ )+ })+ requestXHR.addEventListener('error', () =>+ reject(new Error(t('upload.runtimeError.networkFailed'))),+ )+ requestXHR.addEventListener('timeout', () =>+ reject(+ new Error(+ t('upload.runtimeError.timeout', {+ timeout: requestXHR.timeout,+ }),+ ),+ ),+ )+ requestXHR.addEventListener('abort', () =>+ reject(new Error(t('upload.runtimeError.aborted'))),+ )+ requestXHR.send(body)})+ } catch (error) {+ const status = xhr?.status ?? 0+ if (+ cancelled ||+ stopped ||+ attempt >= Math.max(1, maxRetries) ||+ (status > 0 && status < 500 && status !== 429)+ ) {+ failureXHR = xhr ?? null+ throw error+ }callbacks.onRetry?.(attempt, maxRetries)-- // 延迟后重试- setTimeout(() => {- uploadFile(file, signedUrl, callbacks, attempt + 1)- .then(resolve)- .catch(reject)- }, retryDelay * attempt) // 指数退避- } else {- updateStatus({ status: 'error', error: errorMessage, endTime })- callbacks.onStatusChange?.('error')- callbacks.onError?.(errorMessage, xhr)- reject(new Error(errorMessage))- }- })-- // 超时处理- xhr.addEventListener('timeout', () => {- const endTime = Date.now()- const errorMessage = t('upload.runtimeError.timeout', { timeout })-- // 检查是否可以重试(超时也可以重试)- const canRetry = attempt < maxRetries-- if (canRetry) {- updateStatus({- status: 'error',- error: t('upload.runtimeError.retrying', {- message: errorMessage,- attempt,- max: maxRetries,- }),- endTime,+ if (cancelled || stopped) throw error+ await new Promise<void>((resolve) => {+ const timer = setTimeout(() => {+ retryWaits.delete(timer)+ resolve()+ }, retryDelay * attempt)+ retryWaits.set(timer, resolve)})- callbacks.onRetry?.(attempt, maxRetries)-- // 延迟后重试- setTimeout(() => {- uploadFile(file, signedUrl, callbacks, attempt + 1)- .then(resolve)- .catch(reject)- }, retryDelay * attempt) // 指数退避- } else {- updateStatus({ status: 'error', error: errorMessage, endTime })- callbacks.onStatusChange?.('error')- callbacks.onError?.(errorMessage, xhr)- reject(new Error(errorMessage))+ } finally {+ if (xhr) activeXHRs.delete(xhr)}- })-- // 中止处理- xhr.addEventListener('abort', () => {- const endTime = Date.now()- updateStatus({ status: 'aborted', endTime })- callbacks.onStatusChange?.('aborted')- callbacks.onAbort?.()- reject(new Error(t('upload.runtimeError.aborted')))- })-- // 状态变化处理- xhr.addEventListener('readystatechange', () => {- if (xhr.readyState === XMLHttpRequest.DONE) {- if (xhr.status >= 200 && xhr.status < 300) {- // 成功情况在 load 事件中处理- return- } else if (xhr.status >= 400) {- const endTime = Date.now()- let errorMessage = ''-- // 根据状态码提供更友好的错误信息- switch (xhr.status) {- case 400:- errorMessage = t('upload.runtimeError.badRequest')- break- case 401:- errorMessage = t('upload.runtimeError.unauthorized')- break- case 403:- errorMessage = t('upload.runtimeError.forbidden')- break- case 404:- errorMessage = t('upload.runtimeError.notFound')- break- case 409:- errorMessage = t('upload.runtimeError.conflict')- break- case 413:- errorMessage = t('upload.runtimeError.fileTooLarge')- break- case 415:- errorMessage = t('upload.runtimeError.unsupportedType')- break- case 429:- errorMessage = t('upload.runtimeError.rateLimited')- break- case 500:- errorMessage = t('upload.runtimeError.internalServerError')- break- case 502:- case 503:- case 504:- errorMessage = t('upload.runtimeError.serviceUnavailable')- break- default:- errorMessage = t('upload.runtimeError.httpError', { status: xhr.status })- }-- // 尝试获取服务器返回的详细错误信息- try {- const responseText = xhr.responseText- if (responseText) {- try {- const responseData = JSON.parse(responseText)- if (responseData.data?.message) {- errorMessage = responseData.data.title || errorMessage- }- } catch {- // 如果不是 JSON,使用原始文本- errorMessage += ` - ${responseText}`- }+ }+ }+ try {+ let response: XMLHttpRequest+ if (plan.mode === 'range') {+ if (!Number.isSafeInteger(plan.partSize) || plan.partSize <= 0)+ throw new Error('Invalid range upload plan')+ let lastResponse: XMLHttpRequest | undefined+ for (+ let index = 0, offset = 0;+ offset < file.size;+ index++, offset += plan.partSize+ ) {+ const end = Math.min(offset + plan.partSize, file.size)+ lastResponse = await request(+ plan.method || 'PUT',+ plan.url,+ file.slice(offset, end),+ index,+ {+ ...plan.headers,+ 'Content-Range': `bytes ${offset}-${end - 1}/${file.size}`,+ },+ )+ }+ if (!lastResponse) throw new Error('Empty upload')+ response = lastResponse+ } else if (chunked) {+ const partSize =+ plan.mode === 'multipart' ? plan.partSize : UPLOAD_CHUNK_SIZE+ const count = Math.ceil(file.size / partSize)+ if (+ plan.mode === 'multipart' &&+ (partSize <= 0 || plan.partUrls.length !== count)+ )+ throw new Error('Invalid multipart upload plan')+ const parts: UploadPart[] = []+ const concurrency = Number.isFinite(chunkConcurrency)+ ? Math.max(1, Math.min(6, Math.floor(chunkConcurrency)))+ : 3+ let nextIndex = 0+ let failure: unknown+ const worker = async () => {+ try {+ while (!cancelled && !stopped && nextIndex < count) {+ const index = nextIndex+++ const offset = index * partSize+ const partResponse = await request(+ 'PUT',+ plan.mode === 'multipart'+ ? plan.partUrls[index]!+ : `${signedUrl}?index=${index}`,+ file.slice(offset, offset + partSize),+ index,+ )+ if (plan.mode === 'multipart') {+ const etag = partResponse.getResponseHeader('ETag')+ if (!etag) throw new Error(t('upload.runtimeError.missingEtag'))+ parts[index] = { partNumber: index + 1, etag }}- } catch {- // 忽略解析错误}-- updateStatus({ status: 'error', error: errorMessage, endTime })- callbacks.onStatusChange?.('error')- callbacks.onError?.(errorMessage, xhr)+ } catch (error) {+ if (!stopped) {+ failure = error+ stopped = true+ stopRequests()+ }}}- })-- // 准备请求- xhr.open('PUT', signedUrl)-- // 设置请求头- xhr.setRequestHeader(- 'Content-Type',- file.type || 'application/octet-stream',- )-- // 设置自定义请求头- Object.entries(headers).forEach(([key, value]) => {- xhr.setRequestHeader(key, value)- })-- // 开始上传- xhr.send(file)- })+ await Promise.all(+ Array.from({ length: Math.min(count, concurrency) }, worker),+ )+ if (failure) throw failure+ if (cancelled) throw new Error(t('upload.runtimeError.aborted'))+ updateStatus({ status: 'finalizing' })+ updateProgress(file.size, file.size)+ callbacks.onProgress?.(uploadStatus.value.progress)+ callbacks.onStatusChange?.('finalizing')+ response = await request(+ 'POST',+ signedUrl,+ plan.mode === 'multipart'+ ? new Blob([JSON.stringify({ parts })], {+ type: 'application/json',+ })+ : undefined,+ 0,+ {},+ false,+ )+ } else {+ response = await request(+ plan.mode === 'single' ? plan.method || 'PUT' : 'PUT',+ signedUrl,+ file,+ 0,+ plan.mode === 'single' ? plan.headers : {},+ )+ }+ if (cancelled) throw new Error(t('upload.runtimeError.aborted'))+ updateStatus({ status: 'success', endTime: Date.now() })+ updateProgress(file.size, file.size)+ callbacks.onProgress?.(uploadStatus.value.progress)+ callbacks.onStatusChange?.('success')+ callbacks.onSuccess?.(response)+ return response+ } catch (error) {+ const message = error instanceof Error ? error.message : String(error)+ const status = cancelled ? 'aborted' : 'error'+ updateStatus({ status, error: message, endTime: Date.now() })+ callbacks.onStatusChange?.(status)+ if (cancelled) callbacks.onAbort?.()+ else if (failureXHR || currentXHR)+ callbacks.onError?.(message, (failureXHR || currentXHR)!)+ if (managedSession)+ void $fetch(signedUrl, { method: 'DELETE' }).catch(() => {})+ throw error+ } finally {+ clearInterval(progressTimer)+ stopRequests()+ active = false+ currentXHR = null+ }}- // 中止上传const abortUpload = () => {- if (currentXHR && uploadStatus.value.status === 'uploading') {- currentXHR.abort()- }+ if (!active) return+ cancelled = true+ stopRequests()}-// 格式化字节大小const formatBytes = (bytes: number): string => {if (bytes === 0) return '0 B'@@ -387,11 +475,13 @@ export function useUpload(options: UseUploadOptions = {}) {// 格式化时间const formatTime = (seconds: number): string => {- if (!isFinite(seconds) || seconds < 0) return t('upload.progress.calculating')+ if (!isFinite(seconds) || seconds < 0)+ return t('upload.progress.calculating')- const hours = Math.floor(seconds / 3600)- const minutes = Math.floor((seconds % 3600) / 60)- const secs = Math.floor(seconds % 60)+ const rounded = Math.ceil(seconds)+ const hours = Math.floor(rounded / 3600)+ const minutes = Math.floor((rounded % 3600) / 60)+ const secs = rounded % 60if (hours > 0) {return `${hours}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`@@ -403,7 +493,9 @@ export function useUpload(options: UseUploadOptions = {}) {}// 计算属性- const isUploading = computed(() => uploadStatus.value.status === 'uploading')+ const isUploading = computed(() =>+ ['uploading', 'finalizing'].includes(uploadStatus.value.status),+ )const isIdle = computed(() => uploadStatus.value.status === 'idle')const isSuccess = computed(() => uploadStatus.value.status === 'success')const isError = computed(() => uploadStatus.value.status === 'error')@@ -419,7 +511,8 @@ export function useUpload(options: UseUploadOptions = {}) {loadedText: formatBytes(loaded),totalText: formatBytes(total),speedText: speed ? `${formatBytes(speed)}/s` : '',- timeRemainingText: timeRemaining ? formatTime(timeRemaining) : '',+ timeRemainingText:+ timeRemaining !== undefined ? formatTime(timeRemaining) : '',progressText: `${formatBytes(loaded)} / ${formatBytes(total)} (${percentage}%)`,}})diff --git a/app/pages/dashboard/photos.vue b/app/pages/dashboard/photos.vueindex 25ce26f6..46744c54 100644--- a/app/pages/dashboard/photos.vue+++ b/app/pages/dashboard/photos.vue@@ -112,6 +112,7 @@ interface UploadingFile {| 'waiting'| 'preparing'| 'uploading'+ | 'finalizing'| 'processing'| 'completed'| 'error'@@ -272,7 +273,7 @@ const uploadImage = async (const fileId = existingFileId || `${Date.now()}-${fileName}`const uploadManager = useUpload({- timeout: 10 * 60 * 1000, // 10分钟超时+ timeout: 60 * 1000, // 单个分片一分钟超时,避免请求长时间卡住})// 获取或创建 uploadingFile@@ -304,6 +305,8 @@ const uploadImage = async (body: {fileName: file.name,contentType: file.type,+ fileSize: file.size,+ chunked: true,},})@@ -345,7 +348,7 @@ const uploadImage = async (uploadingFiles.value = new Map(uploadingFiles.value)// 第二步:使用 composable 上传文件到存储- await uploadManager.uploadFile(file, signedUrlResponse.signedUrl, {+ await uploadManager.uploadFile(file, signedUrlResponse.upload || signedUrlResponse.signedUrl, {onProgress: (progress: UploadProgress) => {uploadingFile.progress = progress.percentageuploadingFile.uploadProgress = {@@ -355,14 +358,15 @@ const uploadImage = async (speed: progress.speed,timeRemaining: progress.timeRemaining,speedText: progress.speed ? `${formatBytes(progress.speed)}/s` : '',- timeRemainingText: progress.timeRemaining- ? dayjs.duration(progress.timeRemaining, 'seconds').humanize()+ timeRemainingText: progress.timeRemaining !== undefined+ ? uploadManager.formatTime(progress.timeRemaining): '',}uploadingFiles.value = new Map(uploadingFiles.value)},onStatusChange: (status: string) => {- uploadingFile.canAbort = status === 'uploading'+ uploadingFile.canAbort = status === 'uploading' || status === 'finalizing'+ if (status === 'finalizing') uploadingFile.status = 'finalizing'uploadingFiles.value = new Map(uploadingFiles.value)},onSuccess: async (_xhr: XMLHttpRequest) => {@@ -758,7 +762,10 @@ const clearAllUploads = () => {toRemove.push(fileId)// 如果是正在上传的任务,先中止- if (uploadingFile.status === 'uploading' && uploadingFile.abortUpload) {+ if (+ (uploadingFile.status === 'uploading' || uploadingFile.status === 'finalizing') &&+ uploadingFile.abortUpload+ ) {uploadingFile.abortUpload()}@@ -1114,7 +1121,8 @@ const validateFile = (}const handleUpload = async () => {- const fileList = selectedFiles.value+ const fileList = [...selectedFiles.value]+ const eraseLocation = uploadEraseLocationEnabled.valueif (fileList.length === 0) {return@@ -1186,6 +1194,7 @@ const handleUpload = async () => {}// 立即为所有有效文件创建队列条目,状态为 waiting+ const nextUploadingFiles = new Map(uploadingFiles.value)for (const file of validFiles) {const fileId = fileIdMapping.get(file)!const uploadingFile: UploadingFile = {@@ -1195,11 +1204,18 @@ const handleUpload = async () => {status: 'waiting',canAbort: false,}- uploadingFiles.value.set(fileId, uploadingFile)+ nextUploadingFiles.set(fileId, uploadingFile)}// 触发队列更新- uploadingFiles.value = new Map(uploadingFiles.value)+ uploadingFiles.value = nextUploadingFiles++ // 本批次已经交给队列,立即释放选择器并关闭抽屉。+ selectedFiles.value = []+ isUploadSlideoverOpen.value = false+ await nextTick()+ // 让浏览器先处理界面更新,再启动文件上传。+ await new Promise<void>((resolve) => setTimeout(resolve, 0))// 动态并发上传,始终保持 CONCURRENT_LIMIT 个文件在上传const CONCURRENT_LIMIT = 3 // 限制同时上传的文件数量@@ -1212,7 +1228,7 @@ const handleUpload = async () => {const startUpload = async (file: File): Promise<void> => {const fileId = fileIdMapping.get(file)!try {- await uploadImage(file, fileId, uploadEraseLocationEnabled.value)+ await uploadImage(file, fileId, eraseLocation)} catch (error: any) {errors.push(`${file.name}: ${error.message || '上传失败'}`)console.error('上传错误:', error)@@ -1248,10 +1264,6 @@ const handleUpload = async () => {if (errors.length > 0) {console.error('批量上传错误详情:', errors)}-- // 清空选中的文件- selectedFiles.value = []- isUploadSlideoverOpen.value = false}const openMetadataEditor = (photo: Photo) => {diff --git a/i18n/locales/en.json b/i18n/locales/en.jsonindex eb867c69..ecee387b 100644--- a/i18n/locales/en.json+++ b/i18n/locales/en.json@@ -550,6 +550,7 @@}},"runtimeError": {+ "missingEtag": "The storage response does not expose ETag. Add ETag to the bucket CORS ExposeHeaders setting.","networkFailed": "Network connection failed","clientError": "Client error ({status})","serverError": "Server error ({status})",@@ -809,6 +810,7 @@"waiting": "Waiting","preparing": "Preparing","uploading": "Uploading {0}%",+ "finalizing": "Saving to storage","pendingProcessing": "Waiting to process","completed": "Completed","error": "Error",diff --git a/i18n/locales/ja.json b/i18n/locales/ja.jsonindex c8492c3f..856d6447 100644--- a/i18n/locales/ja.json+++ b/i18n/locales/ja.json@@ -735,6 +735,7 @@"waiting": "アップロード待機","preparing": "準備中","uploading": "アップロード中 {0}%",+ "finalizing": "ファイルを保存中","pendingProcessing": "処理待ち","completed": "完了","error": "エラー",@@ -1586,6 +1587,7 @@}},"runtimeError": {+ "missingEtag": "ストレージの応答から ETag を読み取れません。バケットの CORS ExposeHeaders に ETag を追加してください。","networkFailed": "ネットワーク接続に失敗しました","clientError": "クライアントエラー ({status})","serverError": "サーバーエラー ({status})",diff --git a/i18n/locales/ru.json b/i18n/locales/ru.jsonindex 5085adb6..e0467492 100644--- a/i18n/locales/ru.json+++ b/i18n/locales/ru.json@@ -549,6 +549,7 @@}},"runtimeError": {+ "missingEtag": "Ответ хранилища не раскрывает ETag. Добавьте ETag в ExposeHeaders настроек CORS корзины.","networkFailed": "Не удалось подключиться к сети","clientError": "Ошибка клиента ({status})","serverError": "Ошибка сервера ({status})",@@ -808,6 +809,7 @@"waiting": "Ожидание","preparing": "Подготовка","uploading": "Загрузка: {0}%",+ "finalizing": "Сохранение файла","pendingProcessing": "Ожидание обработки","completed": "Завершено","error": "Ошибка",diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.jsonindex 0692095f..a729e7c2 100644--- a/i18n/locales/zh-Hans.json+++ b/i18n/locales/zh-Hans.json@@ -550,6 +550,7 @@}},"runtimeError": {+ "missingEtag": "存储响应未公开 ETag,请在存储桶 CORS 的 ExposeHeaders 中添加 ETag。","networkFailed": "网络连接失败","clientError": "客户端错误 ({status})","serverError": "服务器错误 ({status})",@@ -809,6 +810,7 @@"waiting": "等待上传","preparing": "准备中","uploading": "上传中 {0}%",+ "finalizing": "正在保存文件","pendingProcessing": "等待处理","completed": "完成","error": "错误",diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.jsonindex 12624ea7..ef11f5fa 100644--- a/i18n/locales/zh-Hant-HK.json+++ b/i18n/locales/zh-Hant-HK.json@@ -735,6 +735,7 @@"waiting": "等待上傳","preparing": "準備中","uploading": "上傳中 {0}%",+ "finalizing": "正在儲存檔案","pendingProcessing": "等待處理","completed": "完成","error": "錯誤",@@ -1586,6 +1587,7 @@}},"runtimeError": {+ "missingEtag": "儲存回應未公開 ETag,請在儲存桶 CORS 的 ExposeHeaders 中加入 ETag。","networkFailed": "網絡連線失敗","clientError": "客戶端錯誤 ({status})","serverError": "伺服器錯誤 ({status})",diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.jsonindex a1423ef6..d6ed0ca1 100644--- a/i18n/locales/zh-Hant-TW.json+++ b/i18n/locales/zh-Hant-TW.json@@ -735,6 +735,7 @@"waiting": "等待上傳","preparing": "準備中","uploading": "上傳中 {0}%",+ "finalizing": "正在儲存檔案","pendingProcessing": "等待處理","completed": "完成","error": "錯誤",@@ -1586,6 +1587,7 @@}},"runtimeError": {+ "missingEtag": "儲存回應未公開 ETag,請在儲存桶 CORS 的 ExposeHeaders 中加入 ETag。","networkFailed": "網路連線失敗","clientError": "用戶端錯誤 ({status})","serverError": "伺服器錯誤 ({status})",diff --git a/server/api/photos/chunks/[id].ts b/server/api/photos/chunks/[id].tsnew file mode 100644index 00000000..8bbefa6d--- /dev/null+++ b/server/api/photos/chunks/[id].ts@@ -0,0 +1,141 @@+import { open, stat, rm } from 'node:fs/promises'+import {+ chunkUploads,+ chunkHash,+ removeChunkUpload,+ UPLOAD_CHUNK_SIZE,+} from '~~/server/utils/chunk-upload'++export default eventHandler(async (event) => {+ const session = await requireUserSession(event)+ const id = getRouterParam(event, 'id') || ''+ const upload = chunkUploads.get(id)+ if (!upload || upload.owner !== String(session.user.id)) {+ throw createError({+ statusCode: 404,+ statusMessage: 'Upload session not found',+ })+ }+ if (event.method === 'DELETE') {+ await removeChunkUpload(id)+ return { ok: true }+ }+ if (upload.cancelled)+ throw createError({ statusCode: 410, statusMessage: 'Upload cancelled' })+ if (upload.busy || (event.method === 'POST' && upload.activeParts.size))+ throw createError({ statusCode: 409, statusMessage: 'Upload is busy' })+ upload.busy = event.method === 'POST'+ let activeIndex: number | undefined+ upload.touched = Date.now()+ try {+ if (event.method === 'POST') {+ if (upload.completed) return { ok: true, key: upload.key }+ if (+ upload.hashes.size !== Math.ceil(upload.size / UPLOAD_CHUNK_SIZE) ||+ (await stat(upload.filePath)).size !== upload.size+ ) {+ throw createError({+ statusCode: 400,+ statusMessage: 'Upload is incomplete',+ })+ }+ await upload.provider.createFromFile(+ upload.key,+ upload.filePath,+ upload.contentType,+ )+ upload.completed = true+ await rm(upload.filePath, { force: true }).catch(() => {})+ return { ok: true, key: upload.key }+ }+ if (event.method !== 'PUT') throw createError({ statusCode: 405 })+ if (upload.completed)+ throw createError({+ statusCode: 409,+ statusMessage: 'Upload already completed',+ })+ const index = Number(getQuery(event).index)+ const count = Math.ceil(upload.size / UPLOAD_CHUNK_SIZE)+ if (!Number.isInteger(index) || index < 0 || index >= count) {+ throw createError({+ statusCode: 400,+ statusMessage: 'Invalid chunk index',+ })+ }+ if (upload.activeParts.has(index))+ throw createError({ statusCode: 409, statusMessage: 'Chunk is busy' })+ upload.activeParts.add(index)+ activeIndex = index+ const expected = Math.min(+ UPLOAD_CHUNK_SIZE,+ upload.size - index * UPLOAD_CHUNK_SIZE,+ )+ const chunks: Buffer[] = []+ let size = 0+ // Nitro's dev proxy can provide a cached/web body rather than a live Node+ // IncomingMessage. Reading node.req directly can wait forever for its end.+ const stream = getRequestWebStream(event)+ const reader = stream?.getReader()+ if (!reader)+ throw createError({+ statusCode: 400,+ statusMessage: 'Missing chunk body',+ })+ try {+ while (true) {+ const { done, value } = await reader.read()+ if (done) break+ size += value.byteLength+ if (size > expected) {+ await reader.cancel()+ throw createError({+ statusCode: 413,+ statusMessage: 'Chunk too large',+ })+ }+ chunks.push(Buffer.from(value))+ }+ } finally {+ reader.releaseLock()+ }+ if (size !== expected)+ throw createError({+ statusCode: 400,+ statusMessage: 'Invalid chunk size',+ })+ const data = Buffer.concat(chunks)+ const hash = chunkHash(data)+ if (upload.cancelled)+ throw createError({ statusCode: 410, statusMessage: 'Upload cancelled' })+ if (upload.hashes.has(index)) {+ if (upload.hashes.get(index) !== hash)+ throw createError({+ statusCode: 409,+ statusMessage: 'Chunk content mismatch',+ })+ return { ok: true }+ }+ const file = await open(upload.filePath, 'r+')+ try {+ let written = 0+ while (written < data.length) {+ const result = await file.write(+ data,+ written,+ data.length - written,+ index * UPLOAD_CHUNK_SIZE + written,+ )+ written += result.bytesWritten+ }+ } finally {+ await file.close()+ }+ upload.hashes.set(index, hash)+ return { ok: true }+ } finally {+ if (event.method === 'POST') upload.busy = false+ if (activeIndex !== undefined) upload.activeParts.delete(activeIndex)+ upload.touched = Date.now()+ if (upload.cancelled) await removeChunkUpload(id)+ }+})diff --git a/server/api/photos/index.post.ts b/server/api/photos/index.post.tsindex 7f6c7217..71d8db3f 100644--- a/server/api/photos/index.post.ts+++ b/server/api/photos/index.post.ts@@ -3,6 +3,7 @@ import { useStorageProvider } from '~~/server/utils/useStorageProvider'import { eq } from 'drizzle-orm'import { generateSafePhotoId } from '~~/server/utils/file-utils'import { settingsManager } from '~~/server/services/settings/settingsManager'+import { prepareUpload } from '~~/server/utils/direct-upload'const VIDEO_EXTENSIONS = new Set(['.mov', '.mp4'])@@ -42,12 +43,12 @@ const isLikelyImageKey = (storageKey?: string | null): boolean => {}export default eventHandler(async (event) => {- await requireUserSession(event)+ const session = await requireUserSession(event)const { storageProvider } = useStorageProvider(event)const t = await useTranslation(event)const body = await readBody(event)- const { fileName, contentType, skipDuplicateCheck } = body+ const { fileName, contentType, skipDuplicateCheck, fileSize, chunked } = bodyconst isVideoUpload = fileName ? isVideoFile(fileName, contentType) : falseif (!fileName) {@@ -57,15 +58,55 @@ export default eventHandler(async (event) => {})}+ if (chunked || fileSize !== undefined) {+ const maxMB =+ (await settingsManager.get<number>('system', 'upload.maxFileSize')) ?? 256+ if (!Number.isSafeInteger(fileSize) || fileSize <= 0)+ throw createError({ statusCode: 400, statusMessage: 'Invalid file size' })+ if (fileSize > maxMB * 1024 * 1024)+ throw createError({+ statusCode: 413,+ statusMessage: t('upload.error.tooLarge.title'),+ })+ const mime = useRuntimeConfig(event).upload.mime+ const allowed = mime.whitelist+ .split(',')+ .map((type: string) => type.trim())+ .filter(Boolean)+ if (+ mime.whitelistEnabled &&+ allowed.length &&+ !allowed.includes(contentType || 'application/octet-stream')+ ) {+ throw createError({+ statusCode: 415,+ statusMessage: t('upload.error.invalidType.title'),+ })+ }+ if (+ typeof fileName !== 'string' ||+ /[\\/]/.test(fileName) ||+ fileName === '.' ||+ fileName === '..'+ ) {+ throw createError({ statusCode: 400, statusMessage: 'Invalid file name' })+ }+ }+try {- const objectKey = `${(storageProvider.config?.prefix || '').replace(/\/+$/, '')}/${fileName}`+ const providerConfig = storageProvider.config+ const prefix =+ providerConfig && 'prefix' in providerConfig ? providerConfig.prefix : ''+ const objectKey = `${(prefix || '').replace(/\/+$/, '')}/${fileName}`// 重复文件检测const duplicateCheckEnabled =((await settingsManager.get<boolean>('system','upload.duplicateCheck.enabled',- )) ?? true) && !skipDuplicateCheck+ )) ??+ true) &&+ !skipDuplicateChecklet existingPhoto = nullif (duplicateCheckEnabled) {@@ -131,14 +172,20 @@ export default eventHandler(async (event) => {}}- // 若存储提供商支持预签名 URL,返回外部直传地址- if (storageProvider.getSignedUrl) {- const signedUrl = await storageProvider.getSignedUrl(objectKey, 3600, {- contentType: contentType || 'application/octet-stream',- })+ // Let the provider choose direct upload before considering a local receiver.+ {+ const upload = await prepareUpload(+ String(session.user.id),+ objectKey,+ fileSize,+ contentType || 'application/octet-stream',+ storageProvider,+ Boolean(chunked),+ )const response: any = {- signedUrl,+ signedUrl: upload.url,+ upload,fileKey: objectKey,expiresIn: 3600,}@@ -160,31 +207,6 @@ export default eventHandler(async (event) => {return response}-- // 否则回退到内部直传端点(需会话)- const internalUploadUrl = `/api/photos/upload?key=${encodeURIComponent(objectKey)}`- const response: any = {- signedUrl: internalUploadUrl,- fileKey: objectKey,- expiresIn: 3600,- }-- if (existingPhoto) {- response.duplicate = true- response.existingPhoto = existingPhoto- response.warningInfo = {- title: t('upload.duplicate.warn.title'),- message: t('upload.duplicate.warn.message', { fileName }),- warning: t('upload.duplicate.warn.warning'),- info: t('upload.duplicate.warn.info', {- title: existingPhoto.title || fileName,- dateTaken:- existingPhoto.dateTaken || t('common.unknown', 'unknown date'),- }),- }- }-- return response} catch (error) {if ((error as any).statusCode) {throw errordiff --git a/server/api/photos/multipart/[id].ts b/server/api/photos/multipart/[id].tsnew file mode 100644index 00000000..e898711e--- /dev/null+++ b/server/api/photos/multipart/[id].ts@@ -0,0 +1,54 @@+import { directUploads, abortDirectUpload } from '~~/server/utils/direct-upload'+import type { UploadPart } from '~~/shared/types/upload'++export default eventHandler(async (event) => {+ const user = await requireUserSession(event)+ const id = getRouterParam(event, 'id') || ''+ const session = directUploads.get(id)+ if (!session || session.owner !== String(user.user.id))+ throw createError({+ statusCode: 404,+ statusMessage: 'Upload session not found',+ })+ if (event.method === 'DELETE') {+ await abortDirectUpload(id)+ return { ok: true }+ }+ if (event.method !== 'POST') throw createError({ statusCode: 405 })+ if (session.cancelled)+ throw createError({ statusCode: 410, statusMessage: 'Upload cancelled' })+ if (session.busy)+ throw createError({ statusCode: 409, statusMessage: 'Upload is busy' })+ if (session.completed) return { ok: true, key: session.key }+ session.busy = true+ session.touched = Date.now()+ try {+ const body = (await readBody(event)) as { parts?: UploadPart[] }+ const parts = body?.parts+ if (+ !Array.isArray(parts) ||+ parts.length !== session.setup.partUrls.length ||+ parts.some(+ (part, index) =>+ !part ||+ part.partNumber !== index + 1 ||+ typeof part.etag !== 'string' ||+ !part.etag ||+ part.etag.length > 256,+ )+ )+ throw createError({+ statusCode: 400,+ statusMessage: 'Invalid multipart completion data',+ })+ if (session.cancelled)+ throw createError({ statusCode: 410, statusMessage: 'Upload cancelled' })+ await session.setup.complete(parts)+ session.completed = true+ return { ok: true, key: session.key }+ } finally {+ session.busy = false+ session.touched = Date.now()+ if (session.cancelled) await abortDirectUpload(id)+ }+})diff --git a/server/api/photos/upload.put.ts b/server/api/photos/upload.put.tsindex 7e7356bf..f7829252 100644--- a/server/api/photos/upload.put.ts+++ b/server/api/photos/upload.put.ts@@ -1,3 +1,6 @@+import { mkdtemp, open, rm } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import path from 'node:path'import { useStorageProvider } from '~~/server/utils/useStorageProvider'import { logger } from '~~/server/utils/logger'import { settingsManager } from '~~/server/services/settings/settingsManager'@@ -51,51 +54,64 @@ export default eventHandler(async (event) => {}}- // 使用流式处理而不是一次性读取整个文件到内存- const raw = await readRawBody(event, false)- if (!raw || !(raw instanceof Buffer)) {- throw createError({- statusCode: 400,- statusMessage: t('upload.error.uploadFailed.title'),- data: {- title: t('upload.error.uploadFailed.title'),- message: t('upload.error.uploadFailed.message'),- },- })- }-- // 简单大小限制(从设置中读取,默认 256MB)const maxFileSizeMB =(await settingsManager.get<number>('system', 'upload.maxFileSize')) ?? 256const maxBytes = maxFileSizeMB * 1024 * 1024- if (raw.byteLength > maxBytes) {- const sizeInMB = (raw.byteLength / 1024 / 1024).toFixed(2)+ const stream = getRequestWebStream(event)+ if (!stream)throw createError({- statusCode: 413,- statusMessage: t('upload.error.tooLarge.title'),- data: {- title: t('upload.error.tooLarge.title'),- message: t('upload.error.tooLarge.message', { size: sizeInMB }),- suggestion: t('upload.error.tooLarge.suggestion', {- maxSize: maxFileSizeMB,- }),- },+ statusCode: 400,+ statusMessage: t('upload.error.uploadFailed.title'),})- }-+ const directory = await mkdtemp(path.join(tmpdir(), 'chronoframe-upload-'))+ const filePath = path.join(directory, 'file')+ const reader = stream.getReader()+ let size = 0try {- await storageProvider.create(key.replace(/^\/+/, ''), raw, contentType)+ const file = await open(filePath, 'w')+ try {+ while (true) {+ const { done, value } = await reader.read()+ if (done) break+ size += value.byteLength+ if (size > maxBytes) {+ await reader.cancel()+ throw createError({+ statusCode: 413,+ statusMessage: t('upload.error.tooLarge.title'),+ })+ }+ let written = 0+ while (written < value.byteLength) {+ const result = await file.write(+ value,+ written,+ value.byteLength - written,+ )+ if (!result.bytesWritten)+ throw new Error('Failed to write upload data')+ written += result.bytesWritten+ }+ }+ } finally {+ await file.close()+ }+ if (!size)+ throw createError({+ statusCode: 400,+ statusMessage: t('upload.error.uploadFailed.title'),+ })+ await storageProvider.createFromFile(key, filePath, contentType)} catch (error) {+ if ((error as { statusCode?: number }).statusCode) throw errorlogger.chrono.error('Storage provider create error:', error)throw createError({statusCode: 500,statusMessage: t('upload.error.uploadFailed.title'),- data: {- title: t('upload.error.uploadFailed.title'),- message: t('upload.error.uploadFailed.message'),- },})+ } finally {+ reader.releaseLock()+ await rm(directory, { recursive: true, force: true })}-return { ok: true, key }})diff --git a/server/services/storage/interfaces.ts b/server/services/storage/interfaces.tsindex 2d61ce11..ad1d023b 100644--- a/server/services/storage/interfaces.ts+++ b/server/services/storage/interfaces.ts@@ -1,4 +1,15 @@import type { StorageConfig } from '.'+import type { UploadPart, UploadPlan } from '../../../shared/types/upload'++export type DirectUploadSetup =+ | Extract<UploadPlan, { mode: 'single' | 'range' }>+ | {+ mode: 'multipart'+ partSize: number+ partUrls: string[]+ complete(parts: UploadPart[]): Promise<void>+ abort(): Promise<void>+ }export interface StorageObject {key: string@@ -16,6 +27,16 @@ export interface UploadOptions {export interface StorageProvider {config?: StorageConfig+ prepareDirectUpload?(+ key: string,+ size: number,+ contentType: string,+ ): Promise<DirectUploadSetup | null>+ createFromFile(+ key: string,+ filePath: string,+ contentType: string,+ ): Promise<StorageObject>create(key: string,fileBuffer: Buffer,diff --git a/server/services/storage/providers/local.ts b/server/services/storage/providers/local.tsindex db69618b..4d0f5167 100644--- a/server/services/storage/providers/local.ts+++ b/server/services/storage/providers/local.ts@@ -1,10 +1,6 @@import { promises as fs } from 'node:fs'import path from 'node:path'-import type {- LocalStorageConfig,- StorageObject,- StorageProvider,-} from '../interfaces'+import type { StorageObject, StorageProvider } from '../interfaces'const ensureDir = async (dirPath: string) => {await fs.mkdir(dirPath, { recursive: true })@@ -14,7 +10,7 @@ const sanitizeKey = (key: string) =>key.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/^\/+/, '')const combinePrefixAndKey = (prefix: string | undefined, key: string) => {- const cleanPrefix = (prefix || '').replace(/\/+$/, '')+ const cleanPrefix = (prefix || '').replace(/^\/+|\/+$/g, '')const cleanKey = key.replace(/^\/+/, '')if (!cleanPrefix) return cleanKeyreturn cleanKey.startsWith(cleanPrefix + '/')@@ -53,6 +49,20 @@ export class LocalStorageProvider implements StorageProvider {}}+ async createFromFile(key: string, filePath: string): Promise<StorageObject> {+ const { absFile, relKey } = this.resolveAbsoluteKey(key)+ await ensureDir(path.dirname(absFile))+ const tempFile = `${absFile}.tmp-${crypto.randomUUID()}`+ try {+ await fs.copyFile(filePath, tempFile)+ await fs.rename(tempFile, absFile)+ const stat = await fs.stat(absFile)+ return { key: relKey, size: stat.size, lastModified: stat.mtime }+ } finally {+ await fs.rm(tempFile, { force: true })+ }+ }+async delete(key: string): Promise<void> {const { absFile } = this.resolveAbsoluteKey(key)try {diff --git a/server/services/storage/providers/openlist.ts b/server/services/storage/providers/openlist.tsindex 8680a72b..aaae2459 100644--- a/server/services/storage/providers/openlist.ts+++ b/server/services/storage/providers/openlist.ts@@ -1,5 +1,12 @@import type { Logger } from '../../../utils/logger'-import type { StorageProvider, StorageObject } from '../interfaces'+import { createReadStream } from 'node:fs'+import { stat } from 'node:fs/promises'+import type {+ StorageProvider,+ StorageObject,+ DirectUploadSetup,+} from '../interfaces'+import path from 'node:path'/*** OpenListStorageProvider implements StorageProvider for OpenList API.@@ -145,6 +152,113 @@ export class OpenListStorageProvider implements StorageProvider {this.logger?.success(`Deleted object: ${key}`)}+ async createFromFile(+ key: string,+ filePath: string,+ contentType: string,+ ): Promise<StorageObject> {+ const { size } = await stat(filePath)+ const body = createReadStream(filePath)+ try {+ const init = {+ method: 'PUT',+ duplex: 'half',+ headers: {+ 'Content-Type': contentType,+ 'Content-Length': String(size),+ 'File-Path': encodeURIComponent(+ this.toAbsolutePath(this.withRoot(key)),+ ),+ },+ body,+ }+ const response = await this.request(+ this.config.uploadEndpoint || '/api/fs/put',+ init as unknown as RequestInit,+ )+ if (!response.ok)+ throw new Error(`OpenList upload failed: ${response.status}`)+ const result = (await response.json()) as {+ code?: number+ message?: string+ }+ if (result.code !== undefined && result.code !== 200)+ throw new Error(result.message || 'OpenList upload failed')+ return { key: this.withRoot(key), size }+ } finally {+ body.destroy()+ }+ }++ async prepareDirectUpload(+ key: string,+ size: number,+ ): Promise<DirectUploadSetup | null> {+ const absoluteKey = this.toAbsolutePath(this.withRoot(key))+ const response = await this.request('/api/fs/get_direct_upload_info', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ path: path.posix.dirname(absoluteKey),+ file_name: path.posix.basename(absoluteKey),+ file_size: size,+ tool: 'HttpDirect',+ }),+ })+ if ([404, 405, 501].includes(response.status)) return null+ if (!response.ok)+ throw new Error(+ `OpenList direct upload preparation failed: ${response.status}`,+ )+ const result = (await response.json()) as {+ code: number+ message?: string+ data?: {+ upload_url?: string+ chunk_size?: number+ method?: string+ headers?: Record<string, string>+ } | null+ }+ if (result.code !== 200) {+ if (+ [404, 405, 501].includes(result.code) ||+ /not supported|unsupported|not implement/i.test(result.message || '')+ )+ return null+ throw new Error(+ result.message || 'OpenList direct upload preparation failed',+ )+ }+ const info = result.data+ if (!info?.upload_url) return null+ if (info.method && !['PUT', 'POST'].includes(info.method.toUpperCase()))+ return null+ const url = new URL(info.upload_url)+ if (!['http:', 'https:'].includes(url.protocol))+ throw new Error('Invalid OpenList upload URL')+ const token = await this.ensureAuthToken()+ if (+ info.upload_url.includes(token) ||+ Object.values(info.headers || {}).some((value) => value.includes(token))+ ) {+ throw new Error('OpenList direct upload returned the server credential')+ }+ const common = {+ url: info.upload_url,+ method: (info.method?.toUpperCase() || 'PUT') as 'PUT' | 'POST',+ headers: info.headers,+ }+ if ((info.chunk_size ?? 0) > 0) {+ if (!Number.isSafeInteger(info.chunk_size))+ throw new Error('Invalid OpenList upload chunk size')+ // OpenList's HttpDirect protocol uses sequential Content-Range requests.+ // It does not advertise random-order/concurrent writes to this URL.+ return { mode: 'range', partSize: info.chunk_size!, ...common }+ }+ return { mode: 'single', ...common }+ }+async get(key: string): Promise<Buffer | null> {// If download endpoint is not provided, try to resolve raw_url via meta and fetch itconst downloadPath = this.config.downloadEndpointdiff --git a/server/services/storage/providers/s3.ts b/server/services/storage/providers/s3.tsindex 28c16d50..7a373d82 100644--- a/server/services/storage/providers/s3.ts+++ b/server/services/storage/providers/s3.ts@@ -6,13 +6,22 @@ import {ListObjectsCommand,PutObjectCommand,S3Client,+ CreateMultipartUploadCommand,+ UploadPartCommand,+ CompleteMultipartUploadCommand,+ AbortMultipartUploadCommand,+ ListPartsCommand,} from '@aws-sdk/client-s3'import { getSignedUrl } from '@aws-sdk/s3-request-presigner'+import { createReadStream } from 'node:fs'+import { stat } from 'node:fs/promises'import type {StorageObject,StorageProvider,UploadOptions,+ DirectUploadSetup,} from '../interfaces'+import { UPLOAD_CHUNK_SIZE } from '../../../../shared/utils/upload'const createClient = (config: S3StorageConfig): S3Client => {if (config.provider !== 's3') {@@ -108,6 +117,29 @@ export class S3StorageProvider implements StorageProvider {}}+ async createFromFile(+ key: string,+ filePath: string,+ contentType: string,+ ): Promise<StorageObject> {+ const { size } = await stat(filePath)+ const body = createReadStream(filePath)+ try {+ const result = await this.client.send(+ new PutObjectCommand({+ Bucket: this.config.bucket,+ Key: key,+ Body: body,+ ContentLength: size,+ ContentType: contentType,+ }),+ )+ return { key, size, etag: result.ETag }+ } finally {+ body.destroy()+ }+ }+async get(key: string): Promise<Buffer | null> {try {const cmd = new GetObjectCommand({@@ -195,6 +227,127 @@ export class S3StorageProvider implements StorageProvider {return url}+ async prepareDirectUpload(+ key: string,+ size: number,+ contentType: string,+ ): Promise<DirectUploadSetup> {+ const single = async (): Promise<DirectUploadSetup> => ({+ mode: 'single',+ url: await this.getSignedUrl(key, 3600, { contentType }),+ })+ if (size <= UPLOAD_CHUNK_SIZE) return single()+ const base = { Bucket: this.config.bucket, Key: key }+ let uploadId: string | undefined+ try {+ uploadId = (+ await this.client.send(+ new CreateMultipartUploadCommand({+ ...base,+ ContentType: contentType,+ }),+ )+ ).UploadId+ } catch (error) {+ const unsupported = error as {+ name?: string+ Code?: string+ $metadata?: { httpStatusCode?: number }+ }+ if (+ [405, 501].includes(unsupported.$metadata?.httpStatusCode ?? 0) ||+ [+ 'NotImplemented',+ 'NotSupported',+ 'UnsupportedOperation',+ 'AccessDenied',+ ].includes(unsupported.name || unsupported.Code || '')+ ) {+ // A provider can allow ordinary PUT uploads while denying multipart+ // initiation. Keep the file on the direct path in that case.+ this.logger?.warn(+ `Multipart initialization rejected (${unsupported.name || unsupported.Code || unsupported.$metadata?.httpStatusCode}); falling back to direct PUT`,+ )+ return single()+ }+ throw error+ }+ if (!uploadId) throw new Error('S3 did not return a multipart upload ID')+ const params = { ...base, UploadId: uploadId }+ const abort = async () => {+ await this.client.send(new AbortMultipartUploadCommand(params))+ }+ const partSize = Math.max(+ UPLOAD_CHUNK_SIZE,+ Math.ceil(size / 10_000 / 1024 / 1024) * 1024 * 1024,+ )+ const count = Math.ceil(size / partSize)+ try {+ const partUrls = await Promise.all(+ Array.from({ length: count }, (_, index) =>+ getSignedUrl(+ this.client,+ new UploadPartCommand({ ...params, PartNumber: index + 1 }),+ { expiresIn: 3600 },+ ),+ ),+ )+ return {+ mode: 'multipart',+ partSize,+ partUrls,+ abort,+ complete: async (parts) => {+ // Verify remote part sizes before publishing the object. Presigned+ // upload URLs alone do not enforce the declared total file size.+ const remote = new Map<number, { size?: number; etag?: string }>()+ let marker: string | undefined+ do {+ const result = await this.client.send(+ new ListPartsCommand({ ...params, PartNumberMarker: marker }),+ )+ for (const part of result.Parts || []) {+ if (part.PartNumber !== undefined)+ remote.set(part.PartNumber, {+ size: part.Size,+ etag: part.ETag,+ })+ }+ marker = result.IsTruncated+ ? result.NextPartNumberMarker+ : undefined+ } while (marker)+ if (+ remote.size !== count ||+ parts.some((part) => {+ const uploaded = remote.get(part.partNumber)+ return (+ uploaded?.etag !== part.etag ||+ uploaded?.size !==+ Math.min(partSize, size - (part.partNumber - 1) * partSize)+ )+ })+ )+ throw new Error('S3 multipart upload size or ETag mismatch')+ await this.client.send(+ new CompleteMultipartUploadCommand({+ ...params,+ MultipartUpload: {+ Parts: parts.map((part) => ({+ PartNumber: part.partNumber,+ ETag: part.etag,+ })),+ },+ }),+ )+ },+ }+ } catch (error) {+ await abort().catch(() => {})+ throw error+ }+ }+async getFileMeta(key: string): Promise<StorageObject | null> {try {const cmd = new HeadObjectCommand({diff --git a/server/utils/chunk-upload.ts b/server/utils/chunk-upload.tsnew file mode 100644index 00000000..7e22df06--- /dev/null+++ b/server/utils/chunk-upload.ts@@ -0,0 +1,80 @@+import { randomUUID, createHash } from 'node:crypto'+import { mkdtemp, rm, writeFile } from 'node:fs/promises'+import { tmpdir } from 'node:os'+import path from 'node:path'+import type { StorageProvider } from '../services/storage/interfaces'++export { UPLOAD_CHUNK_SIZE } from '../../shared/utils/upload'+const TTL = 60 * 60 * 1000+export const chunkUploads = new Map<string, ChunkUpload>()++interface ChunkUpload {+ owner: string+ key: string+ size: number+ contentType: string+ provider: StorageProvider+ directory: string+ filePath: string+ hashes: Map<number, string>+ activeParts: Set<number>+ cancelled: boolean+ busy: boolean+ completed: boolean+ touched: number+}++export const chunkHash = (data: Buffer) =>+ createHash('sha256').update(data).digest('hex')++export async function removeChunkUpload(id: string) {+ const upload = chunkUploads.get(id)+ if (!upload) return+ upload.cancelled = true+ if (upload.busy || upload.activeParts.size) return+ await rm(upload.directory, { recursive: true, force: true })+ chunkUploads.delete(id)+}++export async function createChunkUpload(+ owner: string,+ key: string,+ size: number,+ contentType: string,+ provider: StorageProvider,+) {+ const directory = await mkdtemp(path.join(tmpdir(), 'chronoframe-upload-'))+ const id = randomUUID()+ await writeFile(path.join(directory, 'file'), Buffer.alloc(0))+ chunkUploads.set(id, {+ owner,+ key,+ size,+ contentType,+ provider,+ directory,+ filePath: path.join(directory, 'file'),+ hashes: new Map(),+ activeParts: new Set(),+ cancelled: false,+ busy: false,+ completed: false,+ touched: Date.now(),+ })+ return `/api/photos/chunks/${id}`+}++const cleanup = setInterval(() => {+ for (const [id, upload] of chunkUploads) {+ if (+ !upload.busy &&+ !upload.activeParts.size &&+ Date.now() - upload.touched > TTL+ ) {+ void removeChunkUpload(id).catch((error) =>+ console.error('Upload cleanup failed', error),+ )+ }+ }+}, 60_000)+cleanup.unref()diff --git a/server/utils/direct-upload.ts b/server/utils/direct-upload.tsnew file mode 100644index 00000000..600730b7--- /dev/null+++ b/server/utils/direct-upload.ts@@ -0,0 +1,88 @@+import { randomUUID } from 'node:crypto'+import type {+ DirectUploadSetup,+ StorageProvider,+} from '../services/storage/interfaces'+import type { UploadPlan } from '../../shared/types/upload'+import { createChunkUpload } from './chunk-upload'++export const directUploads = new Map<+ string,+ {+ owner: string+ key: string+ setup: Extract<DirectUploadSetup, { mode: 'multipart' }>+ busy: boolean+ cancelled: boolean+ completed: boolean+ touched: number+ }+>()++export async function abortDirectUpload(id: string) {+ const session = directUploads.get(id)+ if (!session) return+ session.cancelled = true+ if (session.busy) return+ if (!session.completed) await session.setup.abort()+ directUploads.delete(id)+}++export async function prepareUpload(+ owner: string,+ key: string,+ size: number,+ contentType: string,+ provider: StorageProvider,+ chunked: boolean,+): Promise<UploadPlan> {+ if (provider.prepareDirectUpload && Number.isSafeInteger(size) && size > 0) {+ const setup = await provider.prepareDirectUpload(key, size, contentType)+ if (setup?.mode === 'single' || setup?.mode === 'range') return setup+ if (setup?.mode === 'multipart') {+ const id = randomUUID()+ directUploads.set(id, {+ owner,+ key,+ setup,+ busy: false,+ cancelled: false,+ completed: false,+ touched: Date.now(),+ })+ return {+ mode: 'multipart',+ url: `/api/photos/multipart/${id}`,+ partSize: setup.partSize,+ partUrls: setup.partUrls,+ }+ }+ }+ if (provider.getSignedUrl) {+ return {+ mode: 'single',+ url: await provider.getSignedUrl(key, 3600, { contentType }),+ }+ }+ if (chunked && provider.config?.provider === 'local') {+ return {+ mode: 'chunks',+ url: await createChunkUpload(owner, key, size, contentType, provider),+ }+ }+ return {+ mode: 'single',+ url: `/api/photos/upload?key=${encodeURIComponent(key)}`,+ }+}++const cleanup = setInterval(() => {+ for (const [id, session] of directUploads) {+ if (!session.busy && Date.now() - session.touched > 60 * 60_000) {+ void abortDirectUpload(id).catch((error) =>+ console.error('Multipart cleanup failed', error),+ )+ }+ }+}, 60_000)+cleanup.unref()diff --git a/shared/types/upload.ts b/shared/types/upload.tsnew file mode 100644index 00000000..2a1a061b--- /dev/null+++ b/shared/types/upload.ts@@ -0,0 +1,29 @@+export interface UploadPart {+ partNumber: number+ etag: string+}++export type UploadPlan =+ | {+ mode: 'single'+ url: string+ method?: 'PUT' | 'POST'+ headers?: Record<string, string>+ }+ | {+ mode: 'range'+ url: string+ partSize: number+ method?: 'PUT' | 'POST'+ headers?: Record<string, string>+ }+ | {+ mode: 'chunks'+ url: string+ }+ | {+ mode: 'multipart'+ url: string+ partSize: number+ partUrls: string[]+ }diff --git a/shared/utils/upload.ts b/shared/utils/upload.tsnew file mode 100644index 00000000..f4e535d1--- /dev/null+++ b/shared/utils/upload.ts@@ -0,0 +1 @@+export const UPLOAD_CHUNK_SIZE = 8 * 1024 * 1024