Skip to content

Commit

Permalink
Delete file when user cancel request (#567)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
IqbalLx and mcollina committed Jan 23, 2025
1 parent 55b4dd3 commit 73f6d8f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,13 +452,14 @@ function fastifyMultipart (fastify, options, done) {
this.savedRequestFiles = []
const tmpdir = (options && options.tmpdir) || os.tmpdir()
this.tmpUploads = []
let i = 0
for await (const file of files) {
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename))
const filepath = path.join(tmpdir, generateId() + path.extname(file.filename || ('file' + i++)))
const target = createWriteStream(filepath)
try {
this.tmpUploads.push(filepath)
await pump(file.file, target)
this.savedRequestFiles.push({ ...file, filepath })
this.tmpUploads.push(filepath)
} catch (err) {
this.log.error({ err }, 'save request file')
throw err
Expand Down
4 changes: 2 additions & 2 deletions test/big.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ test('should upload a big file in constant memory', { skip: process.env.CI }, fu
}

const memory = process.memoryUsage()
t.ok(memory.rss < 400 * 1024 * 1024) // 200MB
t.ok(memory.heapTotal < 400 * 1024 * 1024) // 200MB
t.ok(memory.rss < 500 * 1024 * 1024)
t.ok(memory.heapTotal < 500 * 1024 * 1024)

reply.send()
})
Expand Down
53 changes: 51 additions & 2 deletions test/multipart-incomplete-upload.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
'use strict'

const util = require('node:util')
const test = require('tap').test
const FormData = require('form-data')
const Fastify = require('fastify')
const multipart = require('..')
const http = require('node:http')
const util = require('node:util')
const sleep = util.promisify(setTimeout)
const { writableNoopStream } = require('noop-stream')
const stream = require('node:stream')
const pipeline = util.promisify(stream.pipeline)
const { once } = require('node:events')
const fs = require('node:fs/promises')

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

const fastify = Fastify()
Expand Down Expand Up @@ -60,3 +62,50 @@ test('should finish with error on partial upload', async function (t) {
await sleep(100)
t.end()
})

test('should finish with error on partial upload - saveRequestFiles', async function (t) {
t.plan(3)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

await fastify.register(multipart)

let tmpUploads
fastify.post('/', async function (req) {
t.ok(req.isMultipart())
try {
await req.saveRequestFiles()
} finally {
tmpUploads = req.tmpUploads
}
})

await fastify.listen({ port: 0 })
const dataSize = 1024 * 1024 * 1024

// request
const form = new FormData()
form.append('upload', Buffer.alloc(dataSize))
const opts = {
protocol: 'http:',
hostname: 'localhost',
port: fastify.server.address().port,
path: '/',
headers: form.getHeaders(),
method: 'POST'
}

const req = http.request(opts)
const data = form.getBuffer()
req.write(data.slice(0, dataSize / 4))
req.write(data.slice(dataSize / 4, dataSize / 2))
req.end()

const [res] = await once(req, 'response')
t.equal(res.statusCode, 500)

for (const tmpUpload of tmpUploads) {
await t.rejects(fs.access(tmpUpload))
}
})

0 comments on commit 73f6d8f

Please sign in to comment.