aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/uploads
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/routes/uploads')
-rw-r--r--src/api/routes/uploads/chunksPOST.js14
-rw-r--r--src/api/routes/uploads/uploadPOST.js54
2 files changed, 32 insertions, 36 deletions
diff --git a/src/api/routes/uploads/chunksPOST.js b/src/api/routes/uploads/chunksPOST.js
index 013c0d6..a9baf55 100644
--- a/src/api/routes/uploads/chunksPOST.js
+++ b/src/api/routes/uploads/chunksPOST.js
@@ -1,27 +1,27 @@
-const Route = require('../../structures/Route');
const path = require('path');
-const Util = require('../../utils/Util');
const jetpack = require('fs-jetpack');
const randomstring = require('randomstring');
+const Util = require('../../utils/Util');
+const Route = require('../../structures/Route');
class uploadPOST extends Route {
constructor() {
super('/upload/chunks', 'post', {
bypassAuth: true,
- canApiKey: true
+ canApiKey: true,
});
}
- async run(req, res, db) {
+ async run(req, res) {
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}/`
+ url: `${process.env.DOMAIN}/`,
};
for (const chunk of req.body.files) {
- const { uuid, count } = chunk;
+ const { uuid } = chunk;
// console.log('Chunk', chunk);
const chunkOutput = path.join(__dirname,
@@ -65,7 +65,7 @@ class uploadPOST extends Route {
return res.status(201).send({
message: 'Sucessfully merged the chunk(s).',
- ...info
+ ...info,
/*
name: `${filename}${ext || ''}`,
size: exists.size,
diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js
index 6c01dd3..99f5ee5 100644
--- a/src/api/routes/uploads/uploadPOST.js
+++ b/src/api/routes/uploads/uploadPOST.js
@@ -1,17 +1,18 @@
-const Route = require('../../structures/Route');
const path = require('path');
-const Util = require('../../utils/Util');
const jetpack = require('fs-jetpack');
const multer = require('multer');
const moment = require('moment');
+const Util = require('../../utils/Util');
+const Route = require('../../structures/Route');
+
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: parseInt(process.env.MAX_SIZE, 10) * (1000 * 1000),
- files: 1
+ files: 1,
},
- fileFilter: (req, file, cb) => {
- // TODO: Enable blacklisting of files/extensions
+ fileFilter: (req, file, cb) =>
+ // TODO: Enable blacklisting of files/extensions
/*
if (options.blacklist.mimes.includes(file.mimetype)) {
return cb(new Error(`${file.mimetype} is a blacklisted filetype.`));
@@ -19,35 +20,34 @@ const upload = multer({
return cb(new Error(`${path.extname(file.originalname).toLowerCase()} is a blacklisted extension.`));
}
*/
- return cb(null, true);
- }
+ cb(null, true)
+ ,
}).array('files[]');
/*
TODO: If source has transparency generate a png thumbnail, otherwise a jpg.
TODO: If source is a gif, generate a thumb of the first frame and play the gif on hover on the frontend.
- TODO: If source is a video, generate a thumb of the first frame and save the video length to the file?
- Another possible solution would be to play a gif on hover that grabs a few chunks like youtube.
TODO: Think if its worth making a folder with the user uuid in uploads/ and upload the pictures there so
that this way at least not every single file will be in 1 directory
- - Addendum to this: Now that the default behaviour is to serve files with node, we can actually pull this off. Before this, having files in
- subfolders meant messing with nginx and the paths, but now it should be fairly easy to re-arrange the folder structure with express.static
- I see great value in this, open to suggestions.
+ XXX: Now that the default behaviour is to serve files with node, we can actually pull this off.
+ Before this, having files in subfolders meant messing with nginx and the paths,
+ but now it should be fairly easy to re-arrange the folder structure with express.static
+ I see great value in this, open to suggestions.
*/
class uploadPOST extends Route {
constructor() {
super('/upload', 'post', {
bypassAuth: true,
- canApiKey: true
+ canApiKey: 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' });
+ if (!user && process.env.PUBLIC_MODE === 'false') return res.status(401).json({ message: 'Not authorized to use this resource' });
const albumId = req.body.albumid || req.headers.albumid;
if (albumId && !user) return res.status(401).json({ message: 'Only registered users can upload files to an album' });
@@ -56,12 +56,13 @@ class uploadPOST extends Route {
if (!album) return res.status(401).json({ message: 'Album doesn\'t exist or it doesn\'t belong to the user' });
}
- return upload(req, res, async err => {
+ return upload(req, res, async (err) => {
if (err) console.error(err.message);
let uploadedFile = {};
let insertedId;
+ // eslint-disable-next-line no-underscore-dangle
const remappedKeys = this._remapKeys(req.body);
const file = req.files[0];
@@ -83,10 +84,7 @@ class uploadPOST extends Route {
if (remappedKeys && remappedKeys.uuid) {
const chunkOutput = path.join(__dirname,
- '..',
- '..',
- '..',
- '..',
+ '../../../../',
process.env.UPLOAD_FOLDER,
'chunks',
remappedKeys.uuid,
@@ -94,10 +92,7 @@ class uploadPOST extends Route {
await jetpack.writeAsync(chunkOutput, file.buffer);
} else {
const output = path.join(__dirname,
- '..',
- '..',
- '..',
- '..',
+ '../../../../',
process.env.UPLOAD_FOLDER,
filename);
await jetpack.writeAsync(output, file.buffer);
@@ -105,7 +100,7 @@ class uploadPOST extends Route {
name: filename,
hash,
size: file.buffer.length,
- url: filename
+ url: filename,
};
}
@@ -124,7 +119,7 @@ class uploadPOST extends Route {
return res.status(201).send({
message: 'Sucessfully uploaded the file.',
- ...uploadedFile
+ ...uploadedFile,
});
});
}
@@ -137,7 +132,7 @@ class uploadPOST extends Route {
size: exists.size,
url: `${process.env.DOMAIN}/${exists.name}`,
deleteUrl: `${process.env.DOMAIN}/api/file/${exists.id}`,
- repeated: true
+ repeated: true,
});
return Util.deleteFile(filename);
@@ -145,7 +140,7 @@ class uploadPOST extends Route {
async checkIfFileExists(db, user, hash) {
const exists = await db.table('files')
- .where(function() { // eslint-disable-line func-names
+ .where(function () { // eslint-disable-line func-names
if (user) this.where('userId', user.id);
else this.whereNull('userId');
})
@@ -186,7 +181,7 @@ class uploadPOST extends Route {
hash: file.hash,
ip: req.ip,
createdAt: now,
- editedAt: now
+ editedAt: now,
});
} else {
insertedId = await db.table('files').insert({
@@ -198,7 +193,7 @@ class uploadPOST extends Route {
hash: file.hash,
ip: req.ip,
createdAt: now,
- editedAt: now
+ editedAt: now,
}, 'id');
}
return insertedId;
@@ -220,6 +215,7 @@ class uploadPOST extends Route {
}
return body;
}
+ return keys;
}
}