From c5ca3ea2a46708744bb884f67fafee9bc1606df1 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Sun, 19 May 2019 23:42:19 +0800 Subject: [PATCH] chore: remove tmp files and don't block the request's response (#33) --- README.md | 7 +++++-- test/file-mode.test.js | 19 +++++++++++++++++++ .../apps/file-mode/app/controller/upload.js | 3 +++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 73993b5..35e83f8 100644 --- a/README.md +++ b/README.md @@ -178,8 +178,11 @@ module.exports = class extends Controller { // process file or upload to cloud storage result = await ctx.oss.put(name, file.filepath); } finally { - // need to remove the tmp files - await ctx.cleanupRequestFiles(); + // remove tmp files and don't block the request's response + // cleanupRequestFiles won't throw error even remove file io error happen + ctx.cleanupRequestFiles(); + // remove tmp files before send response + // await ctx.cleanupRequestFiles(); } ctx.body = { diff --git a/test/file-mode.test.js b/test/file-mode.test.js index 68dc8c0..315bf74 100644 --- a/test/file-mode.test.js +++ b/test/file-mode.test.js @@ -311,6 +311,25 @@ describe('test/file-mode.test.js', () => { assert(data.files.length === 1); }); + it('should use cleanupRequestFiles in async way', async () => { + const form = formstream(); + form.field('foo', 'fengmk2').field('love', 'egg'); + form.file('file2', __filename); + // other form fields + form.field('work', 'with Node.js'); + + const headers = form.headers(); + const res = await urllib.request(host + '/upload?async_cleanup=true', { + method: 'POST', + headers, + stream: form, + }); + + assert(res.status === 200); + const data = JSON.parse(res.data); + assert(data.files.length === 1); + }); + describe('schedule/clean_tmpdir', () => { it('should register clean_tmpdir schedule', () => { // [egg-schedule]: register schedule /hello/egg-multipart/app/schedule/clean_tmpdir.js diff --git a/test/fixtures/apps/file-mode/app/controller/upload.js b/test/fixtures/apps/file-mode/app/controller/upload.js index 9d2935d..4f10cbf 100644 --- a/test/fixtures/apps/file-mode/app/controller/upload.js +++ b/test/fixtures/apps/file-mode/app/controller/upload.js @@ -9,6 +9,9 @@ module.exports = async ctx => { if (ctx.query.cleanup === 'true') { await ctx.cleanupRequestFiles(); } + if (ctx.query.async_cleanup === 'true') { + ctx.cleanupRequestFiles(); + } if (ctx.query.call_multipart_twice) { ctx.multipart();