Skip to content

Commit

Permalink
chore: remove tmp files and don't block the request's response (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed May 19, 2019
1 parent c5a41a2 commit c5ca3ea
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
19 changes: 19 additions & 0 deletions test/file-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/apps/file-mode/app/controller/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit c5ca3ea

Please sign in to comment.