aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/uploads/chunksPOST.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2019-10-01 14:11:16 -0300
committerPitu <[email protected]>2019-10-01 14:11:16 -0300
commit579e1e754ab59a69925b5114641174aa70d18555 (patch)
tree59a49fefb8c4753dfef4df455b8b8c5cd19519bc /src/api/routes/uploads/chunksPOST.js
parentchore: Remove unnecesary stuff (diff)
downloadhost.fuwn.me-579e1e754ab59a69925b5114641174aa70d18555.tar.xz
host.fuwn.me-579e1e754ab59a69925b5114641174aa70d18555.zip
feature: uploader with chunks support
Diffstat (limited to 'src/api/routes/uploads/chunksPOST.js')
-rw-r--r--src/api/routes/uploads/chunksPOST.js76
1 files changed, 76 insertions, 0 deletions
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;