diff options
| author | Kana <[email protected]> | 2021-01-08 19:48:25 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-01-08 19:48:25 +0900 |
| commit | 3cfb2721ce64ab94fdbc9c97b54602acc3c654be (patch) | |
| tree | b427becf78c51081e58f1b2af98b2662f7626a39 /src/api/routes/uploads | |
| parent | Merge pull request #248 from WeebDev/feature/stats-dashboard (diff) | |
| parent | fix: pg driver doesn't return anything without .returning() (diff) | |
| download | host.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/routes/uploads')
| -rw-r--r-- | src/api/routes/uploads/uploadPOST.js | 42 |
1 files changed, 23 insertions, 19 deletions
diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js index bba7989..a0dba27 100644 --- a/src/api/routes/uploads/uploadPOST.js +++ b/src/api/routes/uploads/uploadPOST.js @@ -1,6 +1,7 @@ const path = require('path'); const jetpack = require('fs-jetpack'); const multer = require('multer'); + const Util = require('../../utils/Util'); const Route = require('../../structures/Route'); const multerStorage = require('../../utils/multerStorage'); @@ -10,6 +11,22 @@ const chunkedUploadsTimeout = 1800000; const chunksDir = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'chunks'); const uploadDir = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER); + +const cleanUpChunks = async (uuid, onTimeout) => { + // Remove tmp file + await jetpack.removeAsync(path.join(chunksData[uuid].root, chunksData[uuid].filename)) + .catch(error => { + if (error.code !== 'ENOENT') console.error(error); + }); + + // Remove UUID dir + await jetpack.removeAsync(chunksData[uuid].root); + + // Delete cached chunks data + if (!onTimeout) chunksData[uuid].clearTimeout(); + delete chunksData[uuid]; +}; + class ChunksData { constructor(uuid, root) { this.uuid = uuid; @@ -134,7 +151,7 @@ const uploadFile = async (req, res) => { // If the uploaded file is a chunk then just say that it was a success const uuid = req.body.uuid; if (chunksData[uuid] !== undefined) { - req.files.forEach(file => { + req.files.forEach(() => { chunksData[uuid].chunks++; }); res.json({ success: true }); @@ -143,13 +160,13 @@ 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]; }; -const finishChunks = async (req, res) => { +const finishChunks = async req => { const check = file => typeof file.uuid !== 'string' || !chunksData[file.uuid] || chunksData[file.uuid].chunks < 2; @@ -172,6 +189,8 @@ const finishChunks = async (req, res) => { */ 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 @@ -201,7 +220,7 @@ const finishChunks = async (req, res) => { filename: name, originalname: file.original || '', extname: file.extname, - mimetype: file.type || '', + mimetype: file.mimetype, size: file.size, hash }; @@ -228,21 +247,6 @@ const finishChunks = async (req, res) => { } }; -const cleanUpChunks = async (uuid, onTimeout) => { - // Remove tmp file - await jetpack.removeAsync(path.join(chunksData[uuid].root, chunksData[uuid].filename)) - .catch(error => { - if (error.code !== 'ENOENT') console.error(error); - }); - - // Remove UUID dir - await jetpack.removeAsync(chunksData[uuid].root); - - // Delete cached chunks data - if (!onTimeout) chunksData[uuid].clearTimeout(); - delete chunksData[uuid]; -}; - class uploadPOST extends Route { constructor() { super('/upload', 'post', { |