Is a way to check the file size first then decide if the file is saving into the disk?
See original GitHub issueHi,
I was assuming file.on(data, function () {...})
can handle this, for example:
var busParams = {headers: req.headers, limits : {fileSize:10000, files:1}};
var busboy = new Busboy(busParams);
var filePath = path.join(path.normalize(__dirname + '/..'), 'public', 'images');
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
file.on('data', function () {
if (data.size > busParams.limits.fileSize) {
return console.log('FILE TOO BIG');
} else {
var saveTo = filePath + '/' + Date.now() + filename;
var fStream = fs.createWriteStream(saveTo);
file.pipe(fStream);
file.on('end', function () {
console.log('FILE UPLOADED')
});
}
});
});
However, the uploaded file is empty. And I just realize that if file.pipe(fStream)
is running inside of any event listener, then it gives an empty file which is reasonable but making the 'data'
event not so useful. And the file have to save into the server memory first then being drained (by fs.unlink(saveTo)
inside file.on('limit', function () {})
), which is not so efficient.
So I just wonder if there is a way to check the file size first then decide if the file is saving into the disk?
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
What is the difference between “Size” and “Size on disk?”
Size is the actual size of the file in bytes. Size on disk is the actual amount of space being taken up on...
Read more >Understanding file sizes | GreenNet
In Windows, right-clicking on any file, folder, or drive and choosing "Properties..." will show the size. In an Explorer window, you can select...
Read more >How to determine the appropriate page file size for 64-bit ...
Learn how to determine the appropriate page file size for 64-bit versions of Windows.
Read more >CS372: Homework 11 Solutions - UT Computer Science
The file system also stores the first 436 bytes of each file in the inode. ... Note: The first thing you need to...
Read more >Size vs Size on Disk - Why is there a difference?
The size on the disk is the indicator of how much space a file occupies on the hard disk drive. When a file...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ultimately there is no way around this, no matter how you accept files from a user. This is not an issue limited to multipart forms. For example, if someone sends an HTTP PUT request with
Transfer-Encoding: chunked
, you won’t know you’ve reached the end until the client tells you. Meanwhile you have to do something with the data because you don’t know when the request stream will end (even malicious clients could lie about file length fields you may explicitly send on the front end). So you either Buffer it in memory (not a good idea generally) or you save it to disk somewhere or stream it to some other network resource (e.g. S3). No matter what though, resources somewhere will be used at least temporarily until the request stream has finished.Thank you very much for those details! I learn a lot from it!