fix: use PassThrough from FileType to get the real mimetype of a file while it's being saved to the disk

This commit is contained in:
Zephyrrus 2021-01-06 23:31:10 +02:00
parent f151a8ac3a
commit e0801f0c19
4 changed files with 40 additions and 20 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "chibisafe",
"version": "4.0.0",
"version": "4.0.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

@ -1,6 +1,4 @@
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');
const StatsGenerator = require('../../utils/StatsGenerator');
// Thank you Bobby for the stats code https://github.com/BobbyWibowo/lolisafe/blob/safe.fiery.me/controllers/utilsController.js

View File

@ -160,7 +160,7 @@ const uploadFile = async (req, res) => {
const infoMap = req.files.map(file => ({
path: path.join(uploadDir, file.filename),
data: file
data: { ...file, mimetype: Util.getMimeFromType(file.fileType) || file.mimetype || '' }
}));
return infoMap[0];
@ -189,6 +189,8 @@ const finishChunks = async req => {
*/
file.extname = typeof file.original === 'string' ? Util.getExtension(file.original) : '';
file.fileType = chunksData[file.uuid].fileType;
file.mimetype = Util.getMimeFromType(chunksData[file.uuid].fileType) || file.mimetype || '';
if (Util.isExtensionBlocked(file.extname)) {
throw `${file.extname ? `${file.extname.substr(1).toUpperCase()} files` : 'Files with no extension'} are not permitted.`; // eslint-disable-line no-throw-literal
@ -218,7 +220,7 @@ const finishChunks = async req => {
filename: name,
originalname: file.original || '',
extname: file.extname,
mimetype: file.type || '',
mimetype: file.mimetype,
size: file.size,
hash
};

View File

@ -2,6 +2,7 @@ const fs = require('fs');
const path = require('path');
const blake3 = require('blake3');
const jetpack = require('fs-jetpack');
const FileType = require('file-type');
function DiskStorage(opts) {
this.getFilename = opts.filename;
@ -52,25 +53,44 @@ DiskStorage.prototype._handleFile = function _handleFile(req, file, cb) {
file.stream.on('data', d => hash.update(d));
if (file._isChunk) {
file.stream.on('end', () => {
cb(null, {
destination,
filename,
path: finalPath
if (file._chunksData.chunks === 0) {
FileType.stream(file.stream).then(ftStream => {
file._chunksData.fileType = ftStream.fileType;
file.stream.on('end', () => {
cb(null, {
destination,
filename,
path: finalPath,
fileType: file._chunksData.fileType
});
});
ftStream.pipe(outStream, { end: false });
});
});
file.stream.pipe(outStream, { end: false });
} else {
file.stream.on('end', () => {
cb(null, {
destination,
filename,
path: finalPath,
fileType: file._chunksData.fileType
});
});
file.stream.pipe(outStream, { end: false });
}
} else {
outStream.on('finish', () => {
cb(null, {
destination,
filename,
path: finalPath,
size: outStream.bytesWritten,
hash: hash.digest('hex')
FileType.stream(file.stream).then(ftStream => {
outStream.on('finish', () => {
cb(null, {
destination,
filename,
path: finalPath,
size: outStream.bytesWritten,
hash: hash.digest('hex'),
fileType: ftStream.fileType
});
});
ftStream.pipe(outStream);
});
file.stream.pipe(outStream);
}
});
});