diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/routes/admin/statsGET.js | 2 | ||||
| -rw-r--r-- | src/api/routes/uploads/uploadPOST.js | 6 | ||||
| -rw-r--r-- | src/api/utils/multerStorage.js | 50 |
3 files changed, 39 insertions, 19 deletions
diff --git a/src/api/routes/admin/statsGET.js b/src/api/routes/admin/statsGET.js index 8e53529..388d4fa 100644 --- a/src/api/routes/admin/statsGET.js +++ b/src/api/routes/admin/statsGET.js @@ -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 diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js index 8e26079..a0dba27 100644 --- a/src/api/routes/uploads/uploadPOST.js +++ b/src/api/routes/uploads/uploadPOST.js @@ -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 }; diff --git a/src/api/utils/multerStorage.js b/src/api/utils/multerStorage.js index 9c4d94f..1f1f0dd 100644 --- a/src/api/utils/multerStorage.js +++ b/src/api/utils/multerStorage.js @@ -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); } }); }); |