aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/routes')
-rw-r--r--src/api/routes/admin/userDemote.js3
-rw-r--r--src/api/routes/admin/userDisable.js3
-rw-r--r--src/api/routes/admin/userEnable.js3
-rw-r--r--src/api/routes/albums/albumDELETE.js3
-rw-r--r--src/api/routes/albums/albumPOST.js6
-rw-r--r--src/api/routes/albums/albumPurgeDELETE.js3
-rw-r--r--src/api/routes/albums/albumZipGET.js3
-rw-r--r--src/api/routes/albums/link/linkPOST.js2
-rw-r--r--src/api/routes/auth/registerPOST.js2
-rw-r--r--src/api/routes/files/albumAddPOST.js2
-rw-r--r--src/api/routes/files/albumDelPOST.js3
-rw-r--r--src/api/routes/files/tagAddBatchPOST.js2
-rw-r--r--src/api/routes/files/tagAddPOST.js2
-rw-r--r--src/api/routes/files/tagDelPOST.js3
-rw-r--r--src/api/routes/service/statsGET.js48
-rw-r--r--src/api/routes/tags/tagDELETE.js3
-rw-r--r--src/api/routes/tags/tagPOST.js5
-rw-r--r--src/api/routes/uploads/uploadPOST.js42
18 files changed, 103 insertions, 35 deletions
diff --git a/src/api/routes/admin/userDemote.js b/src/api/routes/admin/userDemote.js
index b430a48..6611d9f 100644
--- a/src/api/routes/admin/userDemote.js
+++ b/src/api/routes/admin/userDemote.js
@@ -14,7 +14,8 @@ class userDemote extends Route {
try {
await db.table('users')
.where({ id })
- .update({ isAdmin: false });
+ .update({ isAdmin: false })
+ .wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/admin/userDisable.js b/src/api/routes/admin/userDisable.js
index e39c811..a3dbb15 100644
--- a/src/api/routes/admin/userDisable.js
+++ b/src/api/routes/admin/userDisable.js
@@ -14,7 +14,8 @@ class userDisable extends Route {
try {
await db.table('users')
.where({ id })
- .update({ enabled: false });
+ .update({ enabled: false })
+ .wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/admin/userEnable.js b/src/api/routes/admin/userEnable.js
index cff622f..47a9dcb 100644
--- a/src/api/routes/admin/userEnable.js
+++ b/src/api/routes/admin/userEnable.js
@@ -14,7 +14,8 @@ class userEnable extends Route {
try {
await db.table('users')
.where({ id })
- .update({ enabled: true });
+ .update({ enabled: true })
+ .wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/albums/albumDELETE.js b/src/api/routes/albums/albumDELETE.js
index f9c22e6..8fd79ca 100644
--- a/src/api/routes/albums/albumDELETE.js
+++ b/src/api/routes/albums/albumDELETE.js
@@ -26,7 +26,8 @@ class albumDELETE extends Route {
await db.table('albumsLinks').where({ albumId: id }).delete();
// Delete any album links created for this album
- await db.table('links').where({ albumId: id }).delete();
+ await db.table('links').where({ albumId: id }).delete()
+ .wasMutated();
return res.json({ message: 'The album was deleted successfully' });
} catch (error) {
diff --git a/src/api/routes/albums/albumPOST.js b/src/api/routes/albums/albumPOST.js
index 52352a1..04cab1e 100644
--- a/src/api/routes/albums/albumPOST.js
+++ b/src/api/routes/albums/albumPOST.js
@@ -28,7 +28,11 @@ class albumPOST extends Route {
editedAt: now
};
- const dbRes = await db.table('albums').insert(insertObj);
+ const dbRes = await db
+ .table('albums')
+ .insert(insertObj)
+ .returning('id')
+ .wasMutated();
insertObj.id = dbRes.pop();
diff --git a/src/api/routes/albums/albumPurgeDELETE.js b/src/api/routes/albums/albumPurgeDELETE.js
index a63eafc..ff48c33 100644
--- a/src/api/routes/albums/albumPurgeDELETE.js
+++ b/src/api/routes/albums/albumPurgeDELETE.js
@@ -18,7 +18,8 @@ class albumDELETE extends Route {
try {
await Util.deleteAllFilesFromAlbum(id);
- await db.table('albums').where({ id }).delete();
+ await db.table('albums').where({ id }).delete()
+ .wasMutated();
return res.json({ message: 'The album was deleted successfully' });
} catch (error) {
return super.error(res, error);
diff --git a/src/api/routes/albums/albumZipGET.js b/src/api/routes/albums/albumZipGET.js
index c560cff..22b0b6f 100644
--- a/src/api/routes/albums/albumZipGET.js
+++ b/src/api/routes/albums/albumZipGET.js
@@ -74,7 +74,8 @@ class albumGET extends Route {
Util.createZip(filesToZip, album);
await db.table('albums')
.where('id', link.albumId)
- .update('zippedAt', db.fn.now());
+ .update('zippedAt', db.fn.now())
+ .wasMutated();
const filePath = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
const fileName = `${process.env.SERVICE_NAME}-${identifier}.zip`;
diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js
index 28e9dfe..42eac58 100644
--- a/src/api/routes/albums/link/linkPOST.js
+++ b/src/api/routes/albums/link/linkPOST.js
@@ -63,7 +63,7 @@ class linkPOST extends Route {
expiresAt: null,
views: 0
};
- await db.table('links').insert(insertObj);
+ await db.table('links').insert(insertObj).wasMutated();
return res.json({
message: 'The link was created successfully',
diff --git a/src/api/routes/auth/registerPOST.js b/src/api/routes/auth/registerPOST.js
index 54e683e..7b9eb3c 100644
--- a/src/api/routes/auth/registerPOST.js
+++ b/src/api/routes/auth/registerPOST.js
@@ -55,7 +55,7 @@ class registerPOST extends Route {
editedAt: now,
enabled: true,
isAdmin: false
- });
+ }).wasMutated();
return res.json({ message: 'The account was created successfully' });
}
}
diff --git a/src/api/routes/files/albumAddPOST.js b/src/api/routes/files/albumAddPOST.js
index 7b8acf7..645a6be 100644
--- a/src/api/routes/files/albumAddPOST.js
+++ b/src/api/routes/files/albumAddPOST.js
@@ -18,7 +18,7 @@ class albumAddPOST extends Route {
try {
await db.table('albumsFiles')
- .insert({ fileId, albumId });
+ .insert({ fileId, albumId }).wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/files/albumDelPOST.js b/src/api/routes/files/albumDelPOST.js
index 8304163..63da791 100644
--- a/src/api/routes/files/albumDelPOST.js
+++ b/src/api/routes/files/albumDelPOST.js
@@ -19,7 +19,8 @@ class albumDelPOST extends Route {
try {
await db.table('albumsFiles')
.where({ fileId, albumId })
- .delete();
+ .delete()
+ .wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/files/tagAddBatchPOST.js b/src/api/routes/files/tagAddBatchPOST.js
index 679945d..de41d8f 100644
--- a/src/api/routes/files/tagAddBatchPOST.js
+++ b/src/api/routes/files/tagAddBatchPOST.js
@@ -20,7 +20,7 @@ class tagAddBatchPOST extends Route {
try {
const tag = await db.table('tags').where({ name: tagName, userId: user.id }).first();
if (!tag) throw new Error('Tag doesn\'t exist in the database');
- await db.table('fileTags').insert({ fileId, tagId: tag.id });
+ await db.table('fileTags').insert({ fileId, tagId: tag.id }).wasMutated();
addedTags.push(tag);
} catch (e) {
diff --git a/src/api/routes/files/tagAddPOST.js b/src/api/routes/files/tagAddPOST.js
index 2bbfa07..0a0ab42 100644
--- a/src/api/routes/files/tagAddPOST.js
+++ b/src/api/routes/files/tagAddPOST.js
@@ -20,7 +20,7 @@ class tagAddPOST extends Route {
if (!tag) return res.status(400).json({ message: 'Tag doesn\'t exist. ' });
try {
- await db.table('fileTags').insert({ fileId, tagId: tag.id });
+ await db.table('fileTags').insert({ fileId, tagId: tag.id }).wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/files/tagDelPOST.js b/src/api/routes/files/tagDelPOST.js
index ac0bfe4..78e461b 100644
--- a/src/api/routes/files/tagDelPOST.js
+++ b/src/api/routes/files/tagDelPOST.js
@@ -22,7 +22,8 @@ class tagDelPost extends Route {
try {
await db.table('fileTags')
.where({ fileId, tagId: tag.id })
- .delete();
+ .delete()
+ .wasMutated();
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/service/statsGET.js b/src/api/routes/service/statsGET.js
new file mode 100644
index 0000000..2241ca8
--- /dev/null
+++ b/src/api/routes/service/statsGET.js
@@ -0,0 +1,48 @@
+const Route = require('../../structures/Route');
+const StatsGenerator = require('../../utils/StatsGenerator');
+const moment = require('moment');
+
+// Thank you Bobby for the stats code https://github.com/BobbyWibowo/lolisafe/blob/safe.fiery.me/controllers/utilsController.js
+class filesGET extends Route {
+ constructor() {
+ super('/service/statistics', 'get', { adminOnly: true });
+ }
+
+ async run(req, res, db) {
+ const cachedStats = await db('statistics')
+ .select('type', 'data', 'batchId', 'createdAt')
+ .where('batchId', '=', db('statistics').max('batchId'));
+
+ let stats = cachedStats.reduce((acc, { type, data, createdAt }) => {
+ try {
+ // pg returns json, sqlite retuns a string...
+ if (typeof data === 'string' || data instanceof String) {
+ acc[type] = JSON.parse(data);
+ } else {
+ acc[type] = data;
+ }
+
+ acc[type].meta = {
+ cached: true,
+ generatedOn: moment(createdAt).format('MMMM Do YYYY, h:mm:ss a z'), // pg returns this as a date, sqlite3 returns an unix timestamp :<
+ type: StatsGenerator.Type.HIDDEN
+ };
+ } catch (e) {
+ console.error(e);
+ }
+
+ return acc;
+ }, {});
+
+ stats = { ...stats, ...(await StatsGenerator.getMissingStats(db, Object.keys(stats))) };
+
+ const ordered = StatsGenerator.keyOrder.reduce((acc, k) => {
+ acc[k] = stats[k];
+ return acc;
+ }, {});
+
+ return res.json({ statistics: ordered });
+ }
+}
+
+module.exports = filesGET;
diff --git a/src/api/routes/tags/tagDELETE.js b/src/api/routes/tags/tagDELETE.js
index cf74029..733fabf 100644
--- a/src/api/routes/tags/tagDELETE.js
+++ b/src/api/routes/tags/tagDELETE.js
@@ -26,7 +26,8 @@ class tagDELETE extends Route {
/*
Delete the tag
*/
- await db.table('tags').where({ id }).delete();
+ await db.table('tags').where({ id }).delete()
+ .wasMutated();
return res.json({ message: 'The tag was deleted successfully', data: tag });
} catch (error) {
return super.error(res, error);
diff --git a/src/api/routes/tags/tagPOST.js b/src/api/routes/tags/tagPOST.js
index 89b296d..8177067 100644
--- a/src/api/routes/tags/tagPOST.js
+++ b/src/api/routes/tags/tagPOST.js
@@ -25,7 +25,10 @@ class tagPOST extends Route {
editedAt: now
};
- const dbRes = await db.table('tags').insert(insertObj);
+ const dbRes = await db.table('tags')
+ .insert(insertObj)
+ .returning('id')
+ .wasMutated();
insertObj.id = dbRes.pop();
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', {