From 456346d5eeb6ea131056431b520df29f6d00011d Mon Sep 17 00:00:00 2001 From: Sucukdeluxe <259325684+Sucukdeluxe@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:27:15 +0100 Subject: [PATCH] Report CloudDrop direct upload progress and stop when the server ends an upload The lease client reports progress every five seconds, stops with a clear message when the upload was closed on the server and falls back to the server path when no direct target is available before anything was sent. Co-Authored-By: Claude Fable 5.2 --- lib/clouddrop-lease.js | 43 +++++++++++++++++++++++++++++++++++++++-- lib/clouddrop-upload.js | 1 + renderer/i18n.js | 2 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/lib/clouddrop-lease.js b/lib/clouddrop-lease.js index af7caf4..1389c40 100644 --- a/lib/clouddrop-lease.js +++ b/lib/clouddrop-lease.js @@ -23,6 +23,7 @@ const DEFAULTS = Object.freeze({ warmupMs: 5000, windowMs: 4000, requestTimeoutMs: 120000, + heartbeatMs: 5000, apiTimeoutMs: 180000, maxInflightChunks: 64, }); @@ -175,6 +176,7 @@ function leaseError(prefix, reply) { const error = new Error(`Clouddrop: ${prefix}${payload && payload.error ? `: ${payload.error}` : ` (HTTP ${reply.status})`}`); error.status = reply.status; if (payload && typeof payload.code === 'string') error.code = payload.code; + if (payload && typeof payload.reason === 'string') error.reason = payload.reason; return error; } @@ -256,6 +258,8 @@ async function uploadViaLease({ apiKey, baseUrl, dispatcher, userAgent, filePath client: { tool: 'multi-hoster-uploader', version: options.appVersion || null, host: os.hostname(), settings: { connections: settings.connections, chunkSize: settings.chunkSize } }, }, { signal, retries: 2 }); const leaseId = lease.leaseId; + const control = new AbortController(); + signal = signal ? AbortSignal.any([signal, control.signal]) : control.signal; const events = []; const event = (type, data) => events.push({ kind: 'event', type, t: Date.now(), ms: Math.round(performance.now() - started), ...data }); const handle = await fs.promises.open(filePath, 'r'); @@ -267,7 +271,34 @@ async function uploadViaLease({ apiKey, baseUrl, dispatcher, userAgent, filePath let doneBytes = 0; let slowRestarts = 0; let retries = 0; - const report = (partAcked) => { if (progressCb) progressCb(Math.min(fileSize, doneBytes + partAcked), fileSize); }; + let reportedBytes = 0; + const report = (partAcked) => { + reportedBytes = Math.min(fileSize, doneBytes + partAcked); + if (progressCb) progressCb(reportedBytes, fileSize); + }; + let heartbeatBusy = false; + let heartbeatPrevious = { at: performance.now(), bytes: 0 }; + const heartbeat = setInterval(async () => { + if (heartbeatBusy) return; + heartbeatBusy = true; + const at = performance.now(); + const bytes = reportedBytes; + const seconds = (at - heartbeatPrevious.at) / 1000; + const bytesPerSecond = seconds > 0 ? Math.max(0, Math.round((bytes - heartbeatPrevious.bytes) / seconds)) : 0; + heartbeatPrevious = { at, bytes }; + try { + await api.call('POST', `/upload/${leaseId}/progress`, { bytesUploaded: bytes, bytesPerSecond }, { retries: 0, timeoutMs: 15000 }); + } catch (error) { + if (error && error.status === 410) { + control.abort(new Error(error.reason === 'cancelled_by_admin' + ? 'Clouddrop: Upload wurde vom Administrator abgebrochen' + : 'Clouddrop: Upload wurde auf dem Server beendet')); + } + } finally { + heartbeatBusy = false; + } + }, settings.heartbeatMs); + heartbeat.unref?.(); async function sendPart(part, target) { const key = d64(target.fileKey); @@ -412,7 +443,14 @@ async function uploadViaLease({ apiKey, baseUrl, dispatcher, userAgent, filePath for (let attempt = 1; ; attempt++) { const body = { avoidHosts: [...avoidHosts] }; if (reason) body.reason = reason; - const target = await api.call('POST', `/upload/${leaseId}/parts/${part.index}/target`, body, { signal, retries: 2 }); + let target; + try { + target = await api.call('POST', `/upload/${leaseId}/parts/${part.index}/target`, body, { signal, retries: 2 }); + } catch (error) { + const nothingSent = doneBytes === 0 && attempt === 1; + if (nothingSent && !signal.aborted && error && (error.status === 507 || error.status === 503)) error.leaseFallback = true; + throw error; + } target.host = new URL(target.uploadUrl).host; event('target', { part: part.index, attempt: target.attempt, host: target.host, server: target.timings }); try { @@ -465,6 +503,7 @@ async function uploadViaLease({ apiKey, baseUrl, dispatcher, userAgent, filePath api.call('POST', `/upload/${leaseId}/abort`, { reason: String(error.message || error).slice(0, 180) }, { retries: 1 }).catch(() => {}); throw error; } finally { + clearInterval(heartbeat); await handle.close().catch(() => {}); } } diff --git a/lib/clouddrop-upload.js b/lib/clouddrop-upload.js index 10eefb5..cfcf43e 100644 --- a/lib/clouddrop-upload.js +++ b/lib/clouddrop-upload.js @@ -157,6 +157,7 @@ class ClouddropUploader { leaseAvailability.set(this._leaseCacheKey(), { enabled: false, until: this.now() + LEASE_DISABLED_CACHE_MS }); return null; } + if (error && error.leaseFallback) return null; throw error; } } diff --git a/renderer/i18n.js b/renderer/i18n.js index 4daf051..ec0a087 100644 --- a/renderer/i18n.js +++ b/renderer/i18n.js @@ -829,6 +829,8 @@ ['Kein Ordnerpfad angegeben', 'No folder path was provided'], ['Clouddrop: Datei ist leer', 'Clouddrop: The file is empty'], ['Clouddrop: MEGA hat keine Abschluss-Kennung geliefert', 'Clouddrop: MEGA did not return a completion handle'], + ['Clouddrop: Upload wurde vom Administrator abgebrochen', 'Clouddrop: The upload was cancelled by the administrator'], + ['Clouddrop: Upload wurde auf dem Server beendet', 'Clouddrop: The upload was ended on the server'], ['Clouddrop: Keine fileId in Upload-Antwort', 'Clouddrop: No fileId in the upload response'], ['Clouddrop: Keine sessionId von /upload/init', 'Clouddrop: No sessionId from /upload/init'], ['Clouddrop: Keine fileId im Job-Ergebnis', 'Clouddrop: No fileId in the job result'],