Skip to content

Commit

Permalink
fix: should alway remove tmp files on try finally (eggjs#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Nov 11, 2018
1 parent 68a2402 commit b17d146
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
12 changes: 8 additions & 4 deletions multipart-file-mode/app/controller/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ module.exports = class extends Controller {
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
const source = fs.createReadStream(file.filepath);
const target = fs.createWriteStream(targetPath);
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
// delete tmp file
await fs.unlink(file.filepath);

try {
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
} finally {
// delete those request tmp files
await ctx.cleanupRequestFiles();
}

ctx.body = { url: '/public/' + filename };
}
Expand Down
12 changes: 8 additions & 4 deletions multipart-file-mode/app/controller/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ module.exports = class extends Controller {
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
const source = fs.createReadStream(file.filepath);
const target = fs.createWriteStream(targetPath);
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
// delete tmp file
await fs.unlink(file.filepath);

try {
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
} finally {
// delete those request tmp files
await ctx.cleanupRequestFiles();
}

ctx.redirect('/public/' + filename);
}
Expand Down
22 changes: 13 additions & 9 deletions multipart-file-mode/app/controller/multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ module.exports = class extends Controller {
const { ctx } = this;
const files = ctx.request.files;
ctx.logger.warn('files: %j', files);
for (const file of files) {
const filename = file.filename.toLowerCase();
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
const source = fs.createReadStream(file.filepath);
const target = fs.createWriteStream(targetPath);
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
// delete tmp file
await fs.unlink(file.filepath);

try {
for (const file of files) {
const filename = file.filename.toLowerCase();
const targetPath = path.join(this.config.baseDir, 'app/public', filename);
const source = fs.createReadStream(file.filepath);
const target = fs.createWriteStream(targetPath);
await pump(source, target);
ctx.logger.warn('save %s to %s', file.filepath, targetPath);
}
} finally {
// delete those request tmp files
await ctx.cleanupRequestFiles();
}

const fields = [];
Expand Down

0 comments on commit b17d146

Please sign in to comment.