From 551631d8e2b7d91a3da6641c487ade9501b337b4 Mon Sep 17 00:00:00 2001 From: Ramires Viana <59319979+ramiresviana@users.noreply.github.com> Date: Sun, 3 Aug 2025 15:49:35 -0300 Subject: [PATCH] move eta logic to modal --- frontend/src/api/tus.ts | 108 +----------------- .../src/components/prompts/UploadFiles.vue | 51 ++++++++- frontend/src/stores/upload.ts | 22 +--- frontend/src/types/upload.d.ts | 14 --- 4 files changed, 56 insertions(+), 139 deletions(-) diff --git a/frontend/src/api/tus.ts b/frontend/src/api/tus.ts index 8bffa6a5..2609d258 100644 --- a/frontend/src/api/tus.ts +++ b/frontend/src/api/tus.ts @@ -1,17 +1,11 @@ import * as tus from "tus-js-client"; import { baseURL, tusEndpoint, tusSettings, origin } from "@/utils/constants"; import { useAuthStore } from "@/stores/auth"; -import { useUploadStore } from "@/stores/upload"; import { removePrefix } from "@/api/utils"; const RETRY_BASE_DELAY = 1000; const RETRY_MAX_DELAY = 20000; -const SPEED_UPDATE_INTERVAL = 1000; -const ALPHA = 0.2; -const ONE_MINUS_ALPHA = 1 - ALPHA; -const RECENT_SPEEDS_LIMIT = 5; -const MB_DIVISOR = 1024 * 1024; -const CURRENT_UPLOAD_LIST: CurrentUploadList = {}; +const CURRENT_UPLOAD_LIST: { [key: string]: tus.Upload } = {}; export async function upload( filePath: string, @@ -56,9 +50,6 @@ export async function upload( return true; }, onError: function (error: Error | tus.DetailedError) { - if (CURRENT_UPLOAD_LIST[filePath].interval) { - clearInterval(CURRENT_UPLOAD_LIST[filePath].interval); - } delete CURRENT_UPLOAD_LIST[filePath]; const message = @@ -73,40 +64,16 @@ export async function upload( reject(new Error(message)); }, onProgress: function (bytesUploaded) { - const fileData = CURRENT_UPLOAD_LIST[filePath]; - fileData.currentBytesUploaded = bytesUploaded; - - if (!fileData.hasStarted) { - fileData.hasStarted = true; - fileData.lastProgressTimestamp = Date.now(); - - fileData.interval = window.setInterval(() => { - calcProgress(filePath); - }, SPEED_UPDATE_INTERVAL); - } if (typeof onupload === "function") { onupload({ loaded: bytesUploaded }); } }, onSuccess: function () { - if (CURRENT_UPLOAD_LIST[filePath].interval) { - clearInterval(CURRENT_UPLOAD_LIST[filePath].interval); - } delete CURRENT_UPLOAD_LIST[filePath]; resolve(); }, }); - CURRENT_UPLOAD_LIST[filePath] = { - upload: upload, - recentSpeeds: [], - initialBytesUploaded: 0, - currentBytesUploaded: 0, - currentAverageSpeed: 0, - lastProgressTimestamp: null, - sumOfRecentSpeeds: 0, - hasStarted: false, - interval: undefined, - }; + CURRENT_UPLOAD_LIST[filePath] = upload; upload.start(); }); } @@ -138,76 +105,11 @@ function isTusSupported() { return tus.isSupported === true; } -function computeETA(speed?: number) { - const state = useUploadStore(); - if (state.speedMbyte === 0) { - return Infinity; - } - const totalSize = state.sizes.reduce( - (acc: number, size: number) => acc + size, - 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() { - let totalSpeed = 0; - let totalCount = 0; - - for (const filePath in CURRENT_UPLOAD_LIST) { - totalSpeed += CURRENT_UPLOAD_LIST[filePath].currentAverageSpeed; - totalCount++; - } - - if (totalCount === 0) return { speed: 0, eta: Infinity }; - - const averageSpeed = totalSpeed / totalCount; - const averageETA = computeETA(averageSpeed); - - return { speed: averageSpeed, eta: averageETA }; -} - -function calcProgress(filePath: string) { - const uploadStore = useUploadStore(); - const fileData = CURRENT_UPLOAD_LIST[filePath]; - - const elapsedTime = - (Date.now() - (fileData.lastProgressTimestamp ?? 0)) / 1000; - const bytesSinceLastUpdate = - fileData.currentBytesUploaded - fileData.initialBytesUploaded; - const currentSpeed = bytesSinceLastUpdate / MB_DIVISOR / elapsedTime; - - if (fileData.recentSpeeds.length >= RECENT_SPEEDS_LIMIT) { - fileData.sumOfRecentSpeeds -= fileData.recentSpeeds.shift() ?? 0; - } - - fileData.recentSpeeds.push(currentSpeed); - fileData.sumOfRecentSpeeds += currentSpeed; - - const avgRecentSpeed = - fileData.sumOfRecentSpeeds / fileData.recentSpeeds.length; - fileData.currentAverageSpeed = - ALPHA * avgRecentSpeed + ONE_MINUS_ALPHA * fileData.currentAverageSpeed; - - const { speed, eta } = computeGlobalSpeedAndETA(); - uploadStore.setUploadSpeed(speed); - uploadStore.setETA(eta); - - fileData.initialBytesUploaded = fileData.currentBytesUploaded; - fileData.lastProgressTimestamp = Date.now(); -} - export function abortAllUploads() { for (const filePath in CURRENT_UPLOAD_LIST) { - if (CURRENT_UPLOAD_LIST[filePath].interval) { - clearInterval(CURRENT_UPLOAD_LIST[filePath].interval); - } - if (CURRENT_UPLOAD_LIST[filePath].upload) { - CURRENT_UPLOAD_LIST[filePath].upload.abort(true); - CURRENT_UPLOAD_LIST[filePath].upload.options!.onError!( + if (CURRENT_UPLOAD_LIST[filePath]) { + CURRENT_UPLOAD_LIST[filePath].abort(true); + CURRENT_UPLOAD_LIST[filePath].options!.onError!( new Error("Upload aborted") ); } diff --git a/frontend/src/components/prompts/UploadFiles.vue b/frontend/src/components/prompts/UploadFiles.vue index 50bde8ab..48332135 100644 --- a/frontend/src/components/prompts/UploadFiles.vue +++ b/frontend/src/components/prompts/UploadFiles.vue @@ -8,7 +8,7 @@