Skip to content

Commit b17d146

Browse files
authored
fix: should alway remove tmp files on try finally (#89)
1 parent 68a2402 commit b17d146

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

multipart-file-mode/app/controller/ajax.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ module.exports = class extends Controller {
1919
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
2020
const source = fs.createReadStream(file.filepath);
2121
const target = fs.createWriteStream(targetPath);
22-
await pump(source, target);
23-
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
24-
// delete tmp file
25-
await fs.unlink(file.filepath);
22+
23+
try {
24+
await pump(source, target);
25+
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
26+
} finally {
27+
// delete those request tmp files
28+
await ctx.cleanupRequestFiles();
29+
}
2630

2731
ctx.body = { url: '/public/' + filename };
2832
}

multipart-file-mode/app/controller/form.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ module.exports = class extends Controller {
1919
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
2020
const source = fs.createReadStream(file.filepath);
2121
const target = fs.createWriteStream(targetPath);
22-
await pump(source, target);
23-
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
24-
// delete tmp file
25-
await fs.unlink(file.filepath);
22+
23+
try {
24+
await pump(source, target);
25+
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
26+
} finally {
27+
// delete those request tmp files
28+
await ctx.cleanupRequestFiles();
29+
}
2630

2731
ctx.redirect('/public/' + filename);
2832
}

multipart-file-mode/app/controller/multiple.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ module.exports = class extends Controller {
1414
const { ctx } = this;
1515
const files = ctx.request.files;
1616
ctx.logger.warn('files: %j', files);
17-
for (const file of files) {
18-
const filename = file.filename.toLowerCase();
19-
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
20-
const source = fs.createReadStream(file.filepath);
21-
const target = fs.createWriteStream(targetPath);
22-
await pump(source, target);
23-
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
24-
// delete tmp file
25-
await fs.unlink(file.filepath);
17+
18+
try {
19+
for (const file of files) {
20+
const filename = file.filename.toLowerCase();
21+
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
22+
const source = fs.createReadStream(file.filepath);
23+
const target = fs.createWriteStream(targetPath);
24+
await pump(source, target);
25+
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
26+
}
27+
} finally {
28+
// delete those request tmp files
29+
await ctx.cleanupRequestFiles();
2630
}
2731

2832
const fields = [];

0 commit comments

Comments
 (0)