fix: upload: temp file on Windows won't be deleted until hfs is quit #751
Massimo Melina committed
Sep 28, 2024 at 13:42 UTC
72bd618ca371bebc669d056135cc7d8ab36f791f
1 file changed
+6
-5
src/upload.ts
+6
-5
@@ -6,7 +6,7 @@ import { basename, dirname, extname, join } from 'path'
6
import fs from 'fs'
7
import {
8
Callback, dirTraversal, loadFileAttr, pendingPromise, storeFileAttr, try_,
9
- createStreamLimiter, isWindowsDrive,
9
+ createStreamLimiter, isWindowsDrive, _log,
10
} from './misc'
11
import { notifyClient } from './frontEndApis'
12
import { defineConfig } from './config'
@@ -105,7 +105,7 @@ export function uploadWriter(base: VfsNode, path: string, ctx: Koa.Context) {
105
const size = resumable && try_(() => fs.statSync(resumable).size)
106
if (size === undefined) // stat failed
107
return fail(HTTP_SERVER_ERROR)
108
- if (resume > size)
108
+ if (_.isNumber(size) && resume > size)
109
return fail(HTTP_RANGE_NOT_SATISFIABLE)
110
// warn frontend about resume possibility
111
let resumableLost = false
@@ -144,9 +144,10 @@ export function uploadWriter(base: VfsNode, path: string, ctx: Koa.Context) {
144
const lockMiddleware = pendingPromise() // outside we need to know when all operations stopped
145
writeStream.once('close', async () => {
146
try {
147
+ await new Promise(res => fileStream.close(res)) // this only seem to be necessary on Windows
148
if (ctx.req.aborted) {
149
if (resumable && !resumableLost && !resuming) // we don't want to be left with 2 temp files
149
- return delayedDelete(tempName, 0)
150
+ return rm(tempName)
151
const sec = deleteUnfinishedUploadsAfter.get()
152
return _.isNumber(sec) && delayedDelete(tempName, sec)
153
}
@@ -170,8 +171,8 @@ export function uploadWriter(base: VfsNode, path: string, ctx: Koa.Context) {
171
setUploadMeta(dest, ctx)
172
if (ctx.query.comment)
173
void setCommentFor(dest, String(ctx.query.comment))
173
- if (resumable)
174
- delayedDelete(resumable, 0)
174
+ if (resumable && !resuming) // this happens if user decided to not resume and the new upload finished before delayedDelete
175
+ rm(resumable).catch(console.warn)
176
events.emit('uploadFinished', obj)
177
if (resEvent) for (const cb of resEvent)
178
if (_.isFunction(cb))