aboutsummaryrefslogtreecommitdiff
path: root/src/api/utils/multerStorage.js
diff options
context:
space:
mode:
authorKana <[email protected]>2021-01-08 19:48:25 +0900
committerGitHub <[email protected]>2021-01-08 19:48:25 +0900
commit3cfb2721ce64ab94fdbc9c97b54602acc3c654be (patch)
treeb427becf78c51081e58f1b2af98b2662f7626a39 /src/api/utils/multerStorage.js
parentMerge pull request #248 from WeebDev/feature/stats-dashboard (diff)
parentfix: pg driver doesn't return anything without .returning() (diff)
downloadhost.fuwn.me-3cfb2721ce64ab94fdbc9c97b54602acc3c654be.tar.xz
host.fuwn.me-3cfb2721ce64ab94fdbc9c97b54602acc3c654be.zip
Merge pull request #250 from Zephyrrus/feature/system_status_page
Feature/system status page
Diffstat (limited to 'src/api/utils/multerStorage.js')
-rw-r--r--src/api/utils/multerStorage.js56
1 files changed, 37 insertions, 19 deletions
diff --git a/src/api/utils/multerStorage.js b/src/api/utils/multerStorage.js
index a2d01a4..1f1f0dd 100644
--- a/src/api/utils/multerStorage.js
+++ b/src/api/utils/multerStorage.js
@@ -2,13 +2,14 @@ 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;
if (typeof opts.destination === 'string') {
jetpack.dir(opts.destination);
- this.getDestination = function($0, $1, cb) { cb(null, opts.destination); };
+ this.getDestination = ($0, $1, cb) => { cb(null, opts.destination); };
} else {
this.getDestination = opts.destination;
}
@@ -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);
}
});
});
@@ -86,6 +106,4 @@ DiskStorage.prototype._removeFile = function _removeFile(req, file, cb) {
fs.unlink(path, cb);
};
-module.exports = function(opts) {
- return new DiskStorage(opts);
-};
+module.exports = opts => new DiskStorage(opts);