fix: (regression beta) upload not working if split_uploads is not set

Massimo Melina committed Sep 26, 2024 at 11:02 UTC ac84f6a703e932a0e7f34065495e33ad1ce30b4a
1 file changed +17 -8
frontend/src/upload.ts
+17 -8
@@ -310,7 +310,7 @@ async function startUpload(toUpload: ToUpload, to: string, resume=0) {
310 overrideStatus = 0
311 uploadState.uploading = toUpload
312 await subscribeNotifications()
313 - const splitSize = getHFS().splitUploads || Infinity
313 + const splitSize = getHFS().splitUploads
314 const fullSize = toUpload.file.size
315 let offset = resume
316 do { // at least one iteration, even for empty files
@@ -321,22 +321,28 @@ async function startUpload(toUpload: ToUpload, to: string, resume=0) {
321 if (req?.readyState !== 4) return
322 const status = overrideStatus || req.status
323 closeLast?.()
324 + if (resuming) { // resuming requested
325 + resuming = false // this behavior is only for once, for cancellation of the upload that is in the background while resume is confirmed
326 + stopLooping()
327 + return
328 + }
329 if (!status || status === HTTP_CONFLICT) // 0 = user-aborted, HTTP_CONFLICT = skipped because existing
330 uploadState.skipped.push(toUpload)
331 else if (status >= 400)
332 error(status)
333 else {
329 - offset += splitSize
330 - if (offset < fullSize) return // continue looping
334 + if (splitSize) {
335 + offset += splitSize
336 + if (offset < fullSize) return // continue looping
337 + }
338 done()
339 }
333 - if (!resuming)
334 - next()
335 - offset = fullSize // stop looping
340 + next()
341 }
342 req.onerror = () => {
343 error(0)
344 finished.resolve()
345 + stopLooping()
346 }
347 let lastProgress = 0
348 req.upload.onprogress = (e:any) => {
@@ -350,15 +356,17 @@ async function startUpload(toUpload: ToUpload, to: string, resume=0) {
356 uploadPath = prefix('', dirname(uploadPath), '/') + toUpload.name
357 req.open('PUT', to + pathEncode(uploadPath) + buildUrlQueryString({
358 notificationChannel,
353 - ...offset + splitSize < fullSize && { partial: 'y' },
359 + ...splitSize && offset + splitSize < fullSize && { partial: 'y' },
360 ...offset && { resume: String(offset) },
361 ...toUpload.comment && { comment: toUpload.comment },
362 ...with_(state.uploadOnExisting, x => x !== 'rename' && { existing: x }), // rename is the default
363 }), true)
358 - req.send(toUpload.file.slice(offset, offset + splitSize))
364 + req.send(toUpload.file.slice(offset, splitSize ? offset + splitSize : undefined))
365 await finished
366 } while (offset < fullSize)
367
368 + function stopLooping() { offset = fullSize }
369 +
370 async function subscribeNotifications() {
371 if (notificationChannel) return
372 notificationChannel = 'upload-' + randomId()
@@ -414,6 +422,7 @@ async function startUpload(toUpload: ToUpload, to: string, resume=0) {
422 }
423
424 function next() {
425 + stopLooping()
426 uploadState.uploading = undefined
427 uploadState.partial = 0
428 const { qs } = uploadState