From 56b7b2a5d9f8970ba842c0a698884a8fe73a7180 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Sat, 12 Jul 2025 13:00:53 -0300 Subject: [PATCH] fix: prevent discarding completed upload size --- frontend/src/api/tus.ts | 16 ++++------------ frontend/src/stores/upload.ts | 23 +++++++---------------- frontend/src/types/upload.d.ts | 8 -------- 3 files changed, 11 insertions(+), 36 deletions(-) diff --git a/frontend/src/api/tus.ts b/frontend/src/api/tus.ts index 75b6414c..64efe69a 100644 --- a/frontend/src/api/tus.ts +++ b/frontend/src/api/tus.ts @@ -128,7 +128,8 @@ function isTusSupported() { return tus.isSupported === true; } -function computeETA(state: ETAState, speed?: number) { +function computeETA(speed?: number) { + const state = useUploadStore(); if (state.speedMbyte === 0) { return Infinity; } @@ -136,22 +137,13 @@ function computeETA(state: ETAState, speed?: number) { (acc: number, size: number) => acc + size, 0 ); - const uploadedSize = state.progress.reduce( - (acc: number, progress: Progress) => { - if (typeof progress === "number") { - return acc + progress; - } - return acc; - }, - 0 - ); + const uploadedSize = state.progress.reduce((a, b) => a + b, 0); const remainingSize = totalSize - uploadedSize; const speedBytesPerSecond = (speed ?? state.speedMbyte) * 1024 * 1024; return remainingSize / speedBytesPerSecond; } function computeGlobalSpeedAndETA() { - const uploadStore = useUploadStore(); let totalSpeed = 0; let totalCount = 0; @@ -163,7 +155,7 @@ function computeGlobalSpeedAndETA() { if (totalCount === 0) return { speed: 0, eta: Infinity }; const averageSpeed = totalSpeed / totalCount; - const averageETA = computeETA(uploadStore, averageSpeed); + const averageETA = computeETA(averageSpeed); return { speed: averageSpeed, eta: averageETA }; } diff --git a/frontend/src/stores/upload.ts b/frontend/src/stores/upload.ts index d9698255..8429146e 100644 --- a/frontend/src/stores/upload.ts +++ b/frontend/src/stores/upload.ts @@ -30,7 +30,7 @@ export const useUploadStore = defineStore("upload", { state: (): { id: number; sizes: number[]; - progress: Progress[]; + progress: number[]; queue: UploadItem[]; uploads: Uploads; speedMbyte: number; @@ -54,9 +54,7 @@ export const useUploadStore = defineStore("upload", { } const totalSize = state.sizes.reduce((a, b) => a + b, 0); - - // TODO: this looks ugly but it works with ts now - const sum = state.progress.reduce((acc, val) => +acc + +val) as number; + const sum = state.progress.reduce((a, b) => a + b, 0); return Math.ceil((sum / totalSize) * 100); }, getProgressDecimal: (state) => { @@ -65,21 +63,14 @@ export const useUploadStore = defineStore("upload", { } const totalSize = state.sizes.reduce((a, b) => a + b, 0); - - // TODO: this looks ugly but it works with ts now - const sum = state.progress.reduce((acc, val) => +acc + +val) as number; + const sum = state.progress.reduce((a, b) => a + b, 0); return ((sum / totalSize) * 100).toFixed(2); }, getTotalProgressBytes: (state) => { if (state.progress.length === 0 || state.sizes.length === 0) { return "0 Bytes"; } - const sum = state.progress.reduce( - (sum, p, i) => - (sum as number) + - (typeof p === "number" ? p : p ? state.sizes[i] : 0), - 0 - ) as number; + const sum = state.progress.reduce((a, b) => a + b, 0); return formatSize(sum); }, getTotalSize: (state) => { @@ -104,7 +95,7 @@ export const useUploadStore = defineStore("upload", { const isDir = upload.file.isDir; const progress = isDir ? 100 - : Math.ceil(((state.progress[id] as number) / size) * 100); + : Math.ceil((state.progress[id] / size) * 100); files.push({ id, @@ -124,7 +115,7 @@ export const useUploadStore = defineStore("upload", { }, actions: { // no context as first argument, use `this` instead - setProgress({ id, loaded }: { id: number; loaded: Progress }) { + setProgress({ id, loaded }: { id: number; loaded: number }) { this.progress[id] = loaded; }, setError(error: Error) { @@ -168,7 +159,7 @@ export const useUploadStore = defineStore("upload", { this.processUploads(); }, finishUpload(item: UploadItem) { - this.setProgress({ id: item.id, loaded: item.file.size > 0 }); + this.setProgress({ id: item.id, loaded: item.file.size }); this.removeJob(item.id); this.processUploads(); }, diff --git a/frontend/src/types/upload.d.ts b/frontend/src/types/upload.d.ts index 263b6bf5..131f4b2c 100644 --- a/frontend/src/types/upload.d.ts +++ b/frontend/src/types/upload.d.ts @@ -28,8 +28,6 @@ interface UploadEntry { type UploadList = UploadEntry[]; -type Progress = number | boolean; - type CurrentUploadList = { [key: string]: { upload: import("tus-js-client").Upload; @@ -43,9 +41,3 @@ type CurrentUploadList = { interval: number | undefined; }; }; - -interface ETAState { - sizes: number[]; - progress: Progress[]; - speedMbyte: number; -}