Skip to content

Commit 73f6d8f

Browse files
IqbalLxmcollina
andcommitted
Delete file when user cancel request (#567)
* adding new test * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> * fixup Signed-off-by: Matteo Collina <hello@matteocollina.com> --------- Signed-off-by: Matteo Collina <hello@matteocollina.com> Co-authored-by: Matteo Collina <hello@matteocollina.com>
1 parent 55b4dd3 commit 73f6d8f

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,14 @@ function fastifyMultipart (fastify, options, done) {
452452
this.savedRequestFiles = []
453453
const tmpdir = (options && options.tmpdir) || os.tmpdir()
454454
this.tmpUploads = []
455+
let i = 0
455456
for await (const file of files) {
456-
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
457+
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename || ('file' + i++)))
457458
const target = createWriteStream(filepath)
458459
try {
460+
this.tmpUploads.push(filepath)
459461
await pump(file.file, target)
460462
this.savedRequestFiles.push({ ...file, filepath })
461-
this.tmpUploads.push(filepath)
462463
} catch (err) {
463464
this.log.error({ err }, 'save request file')
464465
throw err

test/big.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
4343
}
4444

4545
const memory = process.memoryUsage()
46-
t.ok(memory.rss < 400 * 1024 * 1024) // 200MB
47-
t.ok(memory.heapTotal < 400 * 1024 * 1024) // 200MB
46+
t.ok(memory.rss < 500 * 1024 * 1024)
47+
t.ok(memory.heapTotal < 500 * 1024 * 1024)
4848

4949
reply.send()
5050
})

test/multipart-incomplete-upload.test.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict'
22

3-
const util = require('node:util')
43
const test = require('tap').test
54
const FormData = require('form-data')
65
const Fastify = require('fastify')
76
const multipart = require('..')
87
const http = require('node:http')
8+
const util = require('node:util')
99
const sleep = util.promisify(setTimeout)
1010
const { writableNoopStream } = require('noop-stream')
1111
const stream = require('node:stream')
1212
const pipeline = util.promisify(stream.pipeline)
13+
const { once } = require('node:events')
14+
const fs = require('node:fs/promises')
1315

14-
test('should finish with error on partial upload', async function (t) {
16+
test('should finish with error on partial upload - files api', async function (t) {
1517
t.plan(4)
1618

1719
const fastify = Fastify()
@@ -60,3 +62,50 @@ test('should finish with error on partial upload', async function (t) {
6062
await sleep(100)
6163
t.end()
6264
})
65+
66+
test('should finish with error on partial upload - saveRequestFiles', async function (t) {
67+
t.plan(3)
68+
69+
const fastify = Fastify()
70+
t.teardown(fastify.close.bind(fastify))
71+
72+
await fastify.register(multipart)
73+
74+
let tmpUploads
75+
fastify.post('/', async function (req) {
76+
t.ok(req.isMultipart())
77+
try {
78+
await req.saveRequestFiles()
79+
} finally {
80+
tmpUploads = req.tmpUploads
81+
}
82+
})
83+
84+
await fastify.listen({ port: 0 })
85+
const dataSize = 1024 * 1024 * 1024
86+
87+
// request
88+
const form = new FormData()
89+
form.append('upload', Buffer.alloc(dataSize))
90+
const opts = {
91+
protocol: 'http:',
92+
hostname: 'localhost',
93+
port: fastify.server.address().port,
94+
path: '/',
95+
headers: form.getHeaders(),
96+
method: 'POST'
97+
}
98+
99+
const req = http.request(opts)
100+
const data = form.getBuffer()
101+
req.write(data.slice(0, dataSize / 4))
102+
req.write(data.slice(dataSize / 4, dataSize / 2))
103+
req.end()
104+
105+
const [res] = await once(req, 'response')
106+
t.equal(res.statusCode, 500)
107+
108+
for (const tmpUpload of tmpUploads) {
109+
await t.rejects(fs.access(tmpUpload))
110+
}
111+
})

0 commit comments

Comments
 (0)