multer: file is not removed if upload is canceled
Consider this minimal working example (Multer v1.1.0 on Windows 7, OS X 10.11 and Ubuntu 14.04 with node v4.2.1):
var express = require('express')
var multer = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();
app.post("/upload", upload.single("file"), function(req, res, next){
res.end("uploaded to: " + req.file.filename);
});
app.listen(80);
and the following test script
# create large test file
$ dd if=/dev/urandom bs=2G count=1 of=upload
# upload
$ curl -F "file=@upload" http://localhost/upload
# upload again, but cancel before upload is finished
timeout -s SIGKILL 2s curl -F "file=@upload" http://localhost/upload
Expected behavior: Only the first file is persisted, the second is removed after the upload got canceled.
Actual behaviour: Both files are stored in the uploads folder
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 6
- Comments: 40 (1 by maintainers)
Commits related to this issue
- Resolution to issue #259 — committed to kyleerik/multer by kyleerik 4 years ago
- https://github.com/expressjs/multer/issues/259 Fixing https://github.com/expressjs/multer/issues/259 — committed to samsung-cnct/multer by maratoid 4 years ago
- Resolution to https://github.com/expressjs/multer/issues/259 — committed to samsung-cnct/multer by maratoid 4 years ago
Here is my approach worked for me
I thought perhaps the error handling code from the README was the solution, but it seems the callback is not called if the request is aborted.
This post might help.
EDIT : This is what I came up with :
We know when if the upload gets canceled. However the partially uploaded file still can’t be deleted, since only multer knows its name ; deleting the most recent file could be a temporary solution.
@Ealhad You can get its name by storage. And req has Event aborted:
@adskjohn you were close, really close!. What I did is only modify the “disk.js” file adding these lines after the “finish” stream event:
This should solve the issue 😄
Came across this issue today, nice to see that a 4 year old issue like this is unresolved. I think it’s poor form to have ‘fixed’ it in the next version when the next version is many years in the making.
//saveTo is the path where the file is going to be saved to //writeStream=fs.createWriteStream(saveTo); req.on(“aborted”,()=>{ writeStream.close(); fs.unlinkSync(saveTo); }); This worked for me using busboy
@jonchurch I see you’ve assigned this to yourself. Have you had a chance to address it? I, for one, am anxiously awaiting the resolution. Shall I create a PR?
@yuwanlin had the right approach, I believe, to causing busboy to close the file handles. Just adding his first code block is sufficient to close this issue.
Insert at line 97 of make-middleware.js
req.on('close', function() { busboy.emit('finish'); })
Once that is done one can use a solution such as @ifree92 suggests to delete the file. That part doesn’t need to be part of the fix, although I do hope that version 2 will address the whole problem internally. It is a reasonable assumption that a partial file from an aborted upload should be deleted.
In the meantime, anyone else needing this patch can use:
"multer": "kyleerik/multer#kyleerik-patch-1"
… in their package.json file.Well, got the same problem. Another solution would be adding a secondary array to the Request object: