@samitouri / QOSami-HFS / commits / f5c10c3b

fix: admin/monitoring: wrong upload progress when split-uploads is enabled

Massimo Melina committed Feb 9, 2025 at 21:32 UTC f5c10c3bafca0382a98ed7893c963dbd552949cd
4 files changed +15 -6
frontend/src/uploadQueue.ts
+2 -2
@@ -10,8 +10,8 @@ import { proxy, ref, snapshot, subscribe } from 'valtio'
10 import { createElement as h } from 'react'
11 import _ from 'lodash'
12 import { UploadStatus } from './upload'
13 -import i18n from './i18n'
13 import { hfsEvent, onHfsEvent } from './misc'
14 +import i18n from './i18n'
15 const { t } = i18n
16
17 export interface ToUpload { file: File, comment?: string, name?: string, to?: string, error?: string }
@@ -155,7 +155,7 @@ export async function startUpload(toUpload: ToUpload, to: string, resume=0) {
155 req.open('PUT', to + pathEncode(uploadPath) + buildUrlQueryString({
156 notificationChannel,
157 giveBack: toUpload.file.lastModified,
158 - ...partial && { partial: 'y' },
158 + ...partial && { partial: fullSize - offset },
159 ...offset && { resume: offset, preserveTempFile },
160 ...toUpload.comment && { comment: toUpload.comment },
161 ...with_(state.uploadOnExisting, x => x !== 'rename' && { existing: x }), // rename is the default
src/connections.ts
+1 -1
@@ -60,7 +60,7 @@ export function getConnection(ctx: Context) {
60 return ctx.state.connection
61 }
62
63 -export function updateConnectionForCtx(ctx: Context ) {
63 +export function updateConnectionForCtx(ctx: Context) {
64 const conn = getConnection(ctx)
65 if (conn)
66 updateConnection(conn, { ctx })
src/serveFile.ts
+8
@@ -122,6 +122,14 @@ export function monitorAsDownload(ctx: Koa.Context, size?: number, offset?: numb
122 })
123 }
124
125 +declare module "koa" {
126 + interface DefaultState {
127 + opProgress?: number
128 + opTotal?: number
129 + opOffset?: number
130 + }
131 +}
132 +
133 export function applyRange(ctx: Koa.Context, totalSize=ctx.response.length) {
134 ctx.set('Accept-Ranges', 'bytes')
135 const { range } = ctx.request.header
src/upload.ts
+4 -3
@@ -91,7 +91,8 @@ export function uploadWriter(base: VfsNode, baseUri: string, path: string, ctx:
91 const dir = dirname(fullPath)
92 const min = minAvailableMb.get() * (1 << 20)
93 const reqSize = Number(ctx.headers["content-length"])
94 - if (isNaN(reqSize)) {
94 + const fullSize = Math.max(reqSize, Number(ctx.query.partial) || 0)
95 + if (isNaN(fullSize)) {
96 if (min)
97 return fail(HTTP_BAD_REQUEST, 'content-length mandatory')
98 }
@@ -107,7 +108,7 @@ export function uploadWriter(base: VfsNode, baseUri: string, path: string, ctx:
108 const { free } = res
109 if (typeof free !== 'number' || isNaN(free))
110 throw ''
110 - if (reqSize > free - (min || 0))
111 + if (fullSize > free - (min || 0))
112 return fail(HTTP_PAYLOAD_TOO_LARGE)
113 }
114 catch(e: any) { // warn, but let it through
@@ -243,7 +244,7 @@ export function uploadWriter(base: VfsNode, baseUri: string, path: string, ctx:
244 function trackProgress() {
245 let lastGot = 0
246 let lastGotTime = 0
246 - const opTotal = reqSize + resume
247 + const opTotal = fullSize + resume
248 Object.assign(ctx.state, { opTotal, opOffset: resume / opTotal, opProgress: 0 })
249 const conn = updateConnectionForCtx(ctx)
250 if (!conn) return