From 579e1e754ab59a69925b5114641174aa70d18555 Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 1 Oct 2019 14:11:16 -0300 Subject: feature: uploader with chunks support --- src/api/routes/uploads/chunksPOST.js | 76 ++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/api/routes/uploads/chunksPOST.js (limited to 'src/api/routes/uploads/chunksPOST.js') diff --git a/src/api/routes/uploads/chunksPOST.js b/src/api/routes/uploads/chunksPOST.js new file mode 100644 index 0000000..075b4cd --- /dev/null +++ b/src/api/routes/uploads/chunksPOST.js @@ -0,0 +1,76 @@ +const Route = require('../../structures/Route'); +const path = require('path'); +const Util = require('../../utils/Util'); +const jetpack = require('fs-jetpack'); +const randomstring = require('randomstring'); + +class uploadPOST extends Route { + constructor() { + super('/upload/chunks', 'post', { bypassAuth: true }); + } + + async run(req, res, db) { + const filename = Util.getUniqueFilename(randomstring.generate(32)); + // console.log('Files', req.body.files); + const info = { + size: req.body.files[0].size, + url: `${process.env.DOMAIN}/` + }; + + for (const chunk of req.body.files) { + const { uuid, count } = chunk; + // console.log('Chunk', chunk); + + const chunkOutput = path.join(__dirname, + '..', + '..', + '..', + '..', + process.env.UPLOAD_FOLDER, + 'chunks', + uuid); + const chunkDir = await jetpack.list(chunkOutput); + const ext = path.extname(chunkDir[0]); + const output = path.join(__dirname, + '..', + '..', + '..', + '..', + process.env.UPLOAD_FOLDER, + `${filename}${ext || ''}`); + chunkDir.sort(); + + // Save some data + info.name = `${filename}${ext || ''}`; + info.url += `${filename}${ext || ''}`; + + for (let i = 0; i < chunkDir.length; i++) { + const dir = path.join(__dirname, + '..', + '..', + '..', + '..', + process.env.UPLOAD_FOLDER, + 'chunks', + uuid, + chunkDir[i]); + const file = await jetpack.readAsync(dir, 'buffer'); + await jetpack.appendAsync(output, file); + } + await jetpack.removeAsync(chunkOutput); + } + + return res.send(201, { + message: 'Sucessfully merged the chunk(s).', + ...info + /* + name: `${filename}${ext || ''}`, + size: exists.size, + url: `${process.env.DOMAIN}/${exists.name}`, + deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}` + */ + }); + } +} + +module.exports = uploadPOST; -- cgit v1.2.3