fix: prevent discarding completed upload size

pull/5270/head
Ramires Viana 2025-07-12 13:00:53 -03:00
parent 9959cd1c9f
commit 56b7b2a5d9
3 changed files with 11 additions and 36 deletions

View File

@ -128,7 +128,8 @@ function isTusSupported() {
return tus.isSupported === true; return tus.isSupported === true;
} }
function computeETA(state: ETAState, speed?: number) { function computeETA(speed?: number) {
const state = useUploadStore();
if (state.speedMbyte === 0) { if (state.speedMbyte === 0) {
return Infinity; return Infinity;
} }
@ -136,22 +137,13 @@ function computeETA(state: ETAState, speed?: number) {
(acc: number, size: number) => acc + size, (acc: number, size: number) => acc + size,
0 0
); );
const uploadedSize = state.progress.reduce( const uploadedSize = state.progress.reduce((a, b) => a + b, 0);
(acc: number, progress: Progress) => {
if (typeof progress === "number") {
return acc + progress;
}
return acc;
},
0
);
const remainingSize = totalSize - uploadedSize; const remainingSize = totalSize - uploadedSize;
const speedBytesPerSecond = (speed ?? state.speedMbyte) * 1024 * 1024; const speedBytesPerSecond = (speed ?? state.speedMbyte) * 1024 * 1024;
return remainingSize / speedBytesPerSecond; return remainingSize / speedBytesPerSecond;
} }
function computeGlobalSpeedAndETA() { function computeGlobalSpeedAndETA() {
const uploadStore = useUploadStore();
let totalSpeed = 0; let totalSpeed = 0;
let totalCount = 0; let totalCount = 0;
@ -163,7 +155,7 @@ function computeGlobalSpeedAndETA() {
if (totalCount === 0) return { speed: 0, eta: Infinity }; if (totalCount === 0) return { speed: 0, eta: Infinity };
const averageSpeed = totalSpeed / totalCount; const averageSpeed = totalSpeed / totalCount;
const averageETA = computeETA(uploadStore, averageSpeed); const averageETA = computeETA(averageSpeed);
return { speed: averageSpeed, eta: averageETA }; return { speed: averageSpeed, eta: averageETA };
} }

View File

@ -30,7 +30,7 @@ export const useUploadStore = defineStore("upload", {
state: (): { state: (): {
id: number; id: number;
sizes: number[]; sizes: number[];
progress: Progress[]; progress: number[];
queue: UploadItem[]; queue: UploadItem[];
uploads: Uploads; uploads: Uploads;
speedMbyte: number; speedMbyte: number;
@ -54,9 +54,7 @@ export const useUploadStore = defineStore("upload", {
} }
const totalSize = state.sizes.reduce((a, b) => a + b, 0); const totalSize = state.sizes.reduce((a, b) => a + b, 0);
const sum = state.progress.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;
return Math.ceil((sum / totalSize) * 100); return Math.ceil((sum / totalSize) * 100);
}, },
getProgressDecimal: (state) => { getProgressDecimal: (state) => {
@ -65,21 +63,14 @@ export const useUploadStore = defineStore("upload", {
} }
const totalSize = state.sizes.reduce((a, b) => a + b, 0); const totalSize = state.sizes.reduce((a, b) => a + b, 0);
const sum = state.progress.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;
return ((sum / totalSize) * 100).toFixed(2); return ((sum / totalSize) * 100).toFixed(2);
}, },
getTotalProgressBytes: (state) => { getTotalProgressBytes: (state) => {
if (state.progress.length === 0 || state.sizes.length === 0) { if (state.progress.length === 0 || state.sizes.length === 0) {
return "0 Bytes"; return "0 Bytes";
} }
const sum = state.progress.reduce( const sum = state.progress.reduce((a, b) => a + b, 0);
(sum, p, i) =>
(sum as number) +
(typeof p === "number" ? p : p ? state.sizes[i] : 0),
0
) as number;
return formatSize(sum); return formatSize(sum);
}, },
getTotalSize: (state) => { getTotalSize: (state) => {
@ -104,7 +95,7 @@ export const useUploadStore = defineStore("upload", {
const isDir = upload.file.isDir; const isDir = upload.file.isDir;
const progress = isDir const progress = isDir
? 100 ? 100
: Math.ceil(((state.progress[id] as number) / size) * 100); : Math.ceil((state.progress[id] / size) * 100);
files.push({ files.push({
id, id,
@ -124,7 +115,7 @@ export const useUploadStore = defineStore("upload", {
}, },
actions: { actions: {
// no context as first argument, use `this` instead // 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; this.progress[id] = loaded;
}, },
setError(error: Error) { setError(error: Error) {
@ -168,7 +159,7 @@ export const useUploadStore = defineStore("upload", {
this.processUploads(); this.processUploads();
}, },
finishUpload(item: UploadItem) { 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.removeJob(item.id);
this.processUploads(); this.processUploads();
}, },

View File

@ -28,8 +28,6 @@ interface UploadEntry {
type UploadList = UploadEntry[]; type UploadList = UploadEntry[];
type Progress = number | boolean;
type CurrentUploadList = { type CurrentUploadList = {
[key: string]: { [key: string]: {
upload: import("tus-js-client").Upload; upload: import("tus-js-client").Upload;
@ -43,9 +41,3 @@ type CurrentUploadList = {
interval: number | undefined; interval: number | undefined;
}; };
}; };
interface ETAState {
sizes: number[];
progress: Progress[];
speedMbyte: number;
}