aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/uploads/uploadPOST.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/uploadPOST.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/uploadPOST.js')
-rw-r--r--src/api/routes/uploads/uploadPOST.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js
new file mode 100644
index 0000000..a461130
--- /dev/null
+++ b/src/api/routes/uploads/uploadPOST.js
@@ -0,0 +1,94 @@
+const Route = require('../../structures/Route');
+const path = require('path');
+const Util = require('../../utils/Util');
+const jetpack = require('fs-jetpack');
+const multer = require('multer');
+const upload = multer({
+ storage: multer.memoryStorage(),
+ limits: {
+ fileSize: parseInt(process.env.MAX_SIZE, 10) * (1000 * 1000),
+ files: 1
+ },
+ fileFilter: (req, file, cb) => {
+ /*
+ if (options.blacklist.mimes.includes(file.mimetype)) {
+ return cb(new Error(`${file.mimetype} is a blacklisted filetype.`));
+ } else if (options.blacklist.extensions.some(ext => path.extname(file.originalname).toLowerCase() === ext)) {
+ return cb(new Error(`${path.extname(file.originalname).toLowerCase()} is a blacklisted extension.`));
+ }
+ */
+ return cb(null, true);
+ }
+}).array('files[]');
+
+class uploadPOST extends Route {
+ constructor() {
+ super('/upload', 'post', { bypassAuth: true });
+ }
+
+ async run(req, res, db) {
+ const user = await Util.isAuthorized(req);
+ if (!user && process.env.PUBLIC_MODE == 'false') return res.status(401).json({ message: 'Not authorized to use this resource' });
+ return upload(req, res, async err => {
+ if (err) console.error(err.message);
+
+ const remappedKeys = this._remapKeys(req.body);
+ // const { uuid, chunkindex } = this._remapKeys(req.body);
+ let uploadedFile = {};
+ for (const file of req.files) {
+ // console.log(file);
+ const ext = path.extname(file.originalname);
+ const hash = Util.generateFileHash(file.buffer);
+ const filename = Util.getUniqueFilename(file.originalname);
+ if (remappedKeys && remappedKeys.uuid) {
+ const chunkOutput = path.join(__dirname,
+ '..',
+ '..',
+ '..',
+ '..',
+ process.env.UPLOAD_FOLDER,
+ 'chunks',
+ remappedKeys.uuid,
+ `${remappedKeys.chunkindex.padStart(3, 0)}${ext || ''}`);
+ await jetpack.writeAsync(chunkOutput, file.buffer);
+ } else {
+ const output = path.join(__dirname,
+ '..',
+ '..',
+ '..',
+ '..',
+ process.env.UPLOAD_FOLDER,
+ filename);
+ await jetpack.writeAsync(output, file.buffer);
+ uploadedFile = {
+ name: filename,
+ hash,
+ size: file.buffer.length,
+ url: filename
+ };
+ }
+ }
+
+ if (!remappedKeys || !remappedKeys.uuid) Util.generateThumbnails(uploadedFile.name);
+
+ return res.send(201, {
+ message: 'Sucessfully uploaded the file.',
+ ...uploadedFile
+ });
+ });
+ }
+
+ _remapKeys(body) {
+ const keys = Object.keys(body);
+ if (keys.length) {
+ for (const key of keys) {
+ if (!/^dz/.test(key)) continue;
+ body[key.replace(/^dz/, '')] = body[key];
+ delete body[key];
+ }
+ return body;
+ }
+ }
+}
+
+module.exports = uploadPOST;