aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes
diff options
context:
space:
mode:
authorKana <[email protected]>2020-12-24 21:41:24 +0900
committerGitHub <[email protected]>2020-12-24 21:41:24 +0900
commit2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab (patch)
treedbf2b2cad342f31849a62089dedd40165758af86 /src/api/routes
parentEnable deleting files with the API key (diff)
parentbug: fix showlist resetting itself every time the page is changed (diff)
downloadhost.fuwn.me-2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab.tar.xz
host.fuwn.me-2412a60bd4cb2364a477a3af79a8c6dcb6b0ddab.zip
Merge pull request #228 from Zephyrrus/begone_trailing_commas
Merge own dev branch into main dev branch
Diffstat (limited to 'src/api/routes')
-rw-r--r--src/api/routes/admin/fileGET.js7
-rw-r--r--src/api/routes/admin/userGET.js5
-rw-r--r--src/api/routes/albums/albumDELETE.js1
-rw-r--r--src/api/routes/albums/albumFullGET.js32
-rw-r--r--src/api/routes/albums/albumGET.js3
-rw-r--r--src/api/routes/albums/albumPOST.js19
-rw-r--r--src/api/routes/albums/albumZipGET.js12
-rw-r--r--src/api/routes/albums/albumsGET.js28
-rw-r--r--src/api/routes/albums/link/linkDELETE.js1
-rw-r--r--src/api/routes/albums/link/linkEditPOST.js22
-rw-r--r--src/api/routes/albums/link/linkPOST.js47
-rw-r--r--src/api/routes/auth/loginPOST.js2
-rw-r--r--src/api/routes/auth/registerPOST.js6
-rw-r--r--src/api/routes/files/albumAddPOST.js3
-rw-r--r--src/api/routes/files/albumDelPOST.js3
-rw-r--r--src/api/routes/files/fileGET.js46
-rw-r--r--src/api/routes/files/filesAlbumsGET.js2
-rw-r--r--src/api/routes/files/filesGET.js27
-rw-r--r--src/api/routes/files/tagAddBatchPOST.js40
-rw-r--r--src/api/routes/files/tagAddPOST.js25
-rw-r--r--src/api/routes/files/tagDelPOST.js38
-rw-r--r--src/api/routes/search/searchGET.js63
-rw-r--r--src/api/routes/service/configGET.js8
-rw-r--r--src/api/routes/tags/tagDELETE.js2
-rw-r--r--src/api/routes/tags/tagPOST.js12
-rw-r--r--src/api/routes/tags/tagsGET.js1
-rw-r--r--src/api/routes/uploads/chunksPOST.js23
-rw-r--r--src/api/routes/uploads/uploadPOST.js40
-rw-r--r--src/api/routes/user/apiKey.js2
-rw-r--r--src/api/routes/user/changePasswordPOST.js4
-rw-r--r--src/api/routes/user/userGET.js3
31 files changed, 397 insertions, 130 deletions
diff --git a/src/api/routes/admin/fileGET.js b/src/api/routes/admin/fileGET.js
index 3bb8da4..9605da4 100644
--- a/src/api/routes/admin/fileGET.js
+++ b/src/api/routes/admin/fileGET.js
@@ -3,7 +3,7 @@ const Util = require('../../utils/Util');
class filesGET extends Route {
constructor() {
- super('/file/:id', 'get', { adminOnly: true });
+ super('/admin/file/:id', 'get', { adminOnly: true });
}
async run(req, res, db) {
@@ -11,7 +11,10 @@ class filesGET extends Route {
if (!id) return res.status(400).json({ message: 'Invalid file ID supplied' });
let file = await db.table('files').where({ id }).first();
- const user = await db.table('users').where({ id: file.userId }).first();
+ const user = await db.table('users')
+ .select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin')
+ .where({ id: file.userId })
+ .first();
file = Util.constructFilePublicLink(file);
// Additional relevant data
diff --git a/src/api/routes/admin/userGET.js b/src/api/routes/admin/userGET.js
index 14a6c92..48c6e9b 100644
--- a/src/api/routes/admin/userGET.js
+++ b/src/api/routes/admin/userGET.js
@@ -11,7 +11,10 @@ class usersGET extends Route {
if (!id) return res.status(400).json({ message: 'Invalid user ID supplied' });
try {
- const user = await db.table('users').where({ id }).first();
+ const user = await db.table('users')
+ .select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin')
+ .where({ id })
+ .first();
const files = await db.table('files')
.where({ userId: user.id })
.orderBy('id', 'desc');
diff --git a/src/api/routes/albums/albumDELETE.js b/src/api/routes/albums/albumDELETE.js
index 4e6640e..f9c22e6 100644
--- a/src/api/routes/albums/albumDELETE.js
+++ b/src/api/routes/albums/albumDELETE.js
@@ -1,5 +1,4 @@
const Route = require('../../structures/Route');
-const Util = require('../../utils/Util');
class albumDELETE extends Route {
constructor() {
diff --git a/src/api/routes/albums/albumFullGET.js b/src/api/routes/albums/albumFullGET.js
index 93b56ce..d25fe15 100644
--- a/src/api/routes/albums/albumFullGET.js
+++ b/src/api/routes/albums/albumFullGET.js
@@ -10,15 +10,38 @@ class albumGET extends Route {
const { id } = req.params;
if (!id) return res.status(400).json({ message: 'Invalid id supplied' });
- const album = await db.table('albums').where({ id, userId: user.id }).first();
+ const album = await db
+ .table('albums')
+ .where({ id, userId: user.id })
+ .first();
if (!album) return res.status(404).json({ message: 'Album not found' });
- const files = await db.table('albumsFiles')
+ let count = 0;
+
+ let files = db
+ .table('albumsFiles')
.where({ albumId: id })
.join('files', 'albumsFiles.fileId', 'files.id')
- .select('files.id', 'files.name')
+ .select('files.id', 'files.name', 'files.createdAt')
.orderBy('files.id', 'desc');
+ const { page, limit = 100 } = req.query;
+ if (page && page >= 0) {
+ files = await files.offset((page - 1) * limit).limit(limit);
+
+ const dbRes = await db
+ .table('albumsFiles')
+ .count('* as count')
+ .where({ albumId: id })
+ .first();
+
+ count = dbRes.count;
+ } else {
+ files = await files; // execute the query
+ count = files.length;
+ }
+
+ // eslint-disable-next-line no-restricted-syntax
for (let file of files) {
file = Util.constructFilePublicLink(file);
}
@@ -26,7 +49,8 @@ class albumGET extends Route {
return res.json({
message: 'Successfully retrieved album',
name: album.name,
- files
+ files,
+ count
});
}
}
diff --git a/src/api/routes/albums/albumGET.js b/src/api/routes/albums/albumGET.js
index 1bf3630..950a1fd 100644
--- a/src/api/routes/albums/albumGET.js
+++ b/src/api/routes/albums/albumGET.js
@@ -21,10 +21,11 @@ class albumGET extends Route {
const files = await db.table('albumsFiles')
.where({ albumId: link.albumId })
.join('files', 'albumsFiles.fileId', 'files.id')
- .select('files.name')
+ .select('files.name', 'files.id')
.orderBy('files.id', 'desc');
// Create the links for each file
+ // eslint-disable-next-line no-restricted-syntax
for (let file of files) {
file = Util.constructFilePublicLink(file);
}
diff --git a/src/api/routes/albums/albumPOST.js b/src/api/routes/albums/albumPOST.js
index 0d3a44c..52352a1 100644
--- a/src/api/routes/albums/albumPOST.js
+++ b/src/api/routes/albums/albumPOST.js
@@ -1,5 +1,5 @@
-const Route = require('../../structures/Route');
const moment = require('moment');
+const Route = require('../../structures/Route');
class albumPOST extends Route {
constructor() {
@@ -14,18 +14,25 @@ class albumPOST extends Route {
/*
Check that an album with that name doesn't exist yet
*/
- const album = await db.table('albums').where({ name, userId: user.id }).first();
- if (album) return res.status(401).json({ message: 'There\'s already an album with that name' });
+ const album = await db
+ .table('albums')
+ .where({ name, userId: user.id })
+ .first();
+ if (album) return res.status(401).json({ message: "There's already an album with that name" });
const now = moment.utc().toDate();
- await db.table('albums').insert({
+ const insertObj = {
name,
userId: user.id,
createdAt: now,
editedAt: now
- });
+ };
+
+ const dbRes = await db.table('albums').insert(insertObj);
+
+ insertObj.id = dbRes.pop();
- return res.json({ message: 'The album was created successfully' });
+ return res.json({ message: 'The album was created successfully', data: insertObj });
}
}
diff --git a/src/api/routes/albums/albumZipGET.js b/src/api/routes/albums/albumZipGET.js
index a6ef6fd..cf1f6f8 100644
--- a/src/api/routes/albums/albumZipGET.js
+++ b/src/api/routes/albums/albumZipGET.js
@@ -1,8 +1,8 @@
+const path = require('path');
+const jetpack = require('fs-jetpack');
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');
const log = require('../../utils/Log');
-const path = require('path');
-const jetpack = require('fs-jetpack');
class albumGET extends Route {
constructor() {
@@ -38,7 +38,7 @@ class albumGET extends Route {
If the date when the album was zipped is greater than the album's last edit, we just send the zip to the user
*/
if (album.zippedAt > album.editedAt) {
- const filePath = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
+ const filePath = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
const exists = await jetpack.existsAsync(filePath);
/*
Make sure the file exists just in case, and if not, continue to it's generation.
@@ -64,11 +64,11 @@ class albumGET extends Route {
/*
Get the actual files
*/
- const fileIds = fileList.map(el => el.fileId);
+ const fileIds = fileList.map((el) => el.fileId);
const files = await db.table('files')
.whereIn('id', fileIds)
.select('name');
- const filesToZip = files.map(el => el.name);
+ const filesToZip = files.map((el) => el.name);
try {
Util.createZip(filesToZip, album);
@@ -76,7 +76,7 @@ class albumGET extends Route {
.where('id', link.albumId)
.update('zippedAt', db.fn.now());
- const filePath = path.join(__dirname, '..', '..', '..', '..', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
+ const filePath = path.join(__dirname, '../../../../', process.env.UPLOAD_FOLDER, 'zips', `${album.userId}-${album.id}.zip`);
const fileName = `lolisafe-${identifier}.zip`;
return res.download(filePath, fileName);
} catch (error) {
diff --git a/src/api/routes/albums/albumsGET.js b/src/api/routes/albums/albumsGET.js
index 1a7db87..93a23e3 100644
--- a/src/api/routes/albums/albumsGET.js
+++ b/src/api/routes/albums/albumsGET.js
@@ -1,3 +1,4 @@
+/* eslint-disable max-classes-per-file */
const Route = require('../../structures/Route');
const Util = require('../../utils/Util');
@@ -12,30 +13,28 @@ class albumsGET extends Route {
of the album files for displaying on the dashboard. It's probably useless
for anyone consuming the API outside of the lolisafe frontend.
*/
- const albums = await db.table('albums')
+ const albums = await db
+ .table('albums')
.where('albums.userId', user.id)
- .select('id', 'name', 'editedAt');
+ .select('id', 'name', 'createdAt', 'editedAt')
+ .orderBy('createdAt', 'desc');
for (const album of albums) {
- // TODO: Optimize the shit out of this. Ideally a JOIN that grabs all the needed stuff in 1 query instead of 3
-
// Fetch the total amount of files each album has.
- const fileCount = await db.table('albumsFiles') // eslint-disable-line no-await-in-loop
+ const fileCount = await db // eslint-disable-line no-await-in-loop
+ .table('albumsFiles')
.where('albumId', album.id)
.count({ count: 'id' });
// Fetch the file list from each album but limit it to 5 per album
- const filesToFetch = await db.table('albumsFiles') // eslint-disable-line no-await-in-loop
+ const files = await db // eslint-disable-line no-await-in-loop
+ .table('albumsFiles')
+ .join('files', { 'files.id': 'albumsFiles.fileId' })
.where('albumId', album.id)
- .select('fileId')
- .orderBy('id', 'desc')
+ .select('files.id', 'files.name')
+ .orderBy('albumsFiles.id', 'desc')
.limit(5);
- // Fetch the actual files
- const files = await db.table('files') // eslint-disable-line no-await-in-loop
- .whereIn('id', filesToFetch.map(el => el.fileId))
- .select('id', 'name');
-
// Fetch thumbnails and stuff
for (let file of files) {
file = Util.constructFilePublicLink(file);
@@ -58,7 +57,8 @@ class albumsDropdownGET extends Route {
}
async run(req, res, db, user) {
- const albums = await db.table('albums')
+ const albums = await db
+ .table('albums')
.where('userId', user.id)
.select('id', 'name');
return res.json({
diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js
index b02d0b4..1af704e 100644
--- a/src/api/routes/albums/link/linkDELETE.js
+++ b/src/api/routes/albums/link/linkDELETE.js
@@ -1,5 +1,4 @@
const Route = require('../../../structures/Route');
-const { dump } = require('dumper.js');
class linkDELETE extends Route {
constructor() {
diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js
index 6776b73..97122a2 100644
--- a/src/api/routes/albums/link/linkEditPOST.js
+++ b/src/api/routes/albums/link/linkEditPOST.js
@@ -1,5 +1,4 @@
const Route = require('../../../structures/Route');
-const log = require('../../../utils/Log');
class linkEditPOST extends Route {
constructor() {
@@ -14,17 +13,22 @@ class linkEditPOST extends Route {
/*
Make sure the link exists
*/
- const link = await db.table('links').where({ identifier, userId: user.id }).first();
- if (!link) return res.status(400).json({ message: 'The link doesn\'t exist or doesn\'t belong to the user' });
+ const link = await db
+ .table('links')
+ .where({ identifier, userId: user.id })
+ .first();
+ if (!link) return res.status(400).json({ message: "The link doesn't exist or doesn't belong to the user" });
try {
- await db.table('links')
+ const updateObj = {
+ enableDownload: enableDownload || false,
+ expiresAt // This one should be null if not supplied
+ };
+ await db
+ .table('links')
.where({ identifier })
- .update({
- enableDownload: enableDownload || false,
- expiresAt // This one should be null if not supplied
- });
- return res.json({ message: 'Editing the link was successful' });
+ .update(updateObj);
+ return res.json({ message: 'Editing the link was successful', data: updateObj });
} catch (error) {
return super.error(res, error);
}
diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js
index 6009922..28e9dfe 100644
--- a/src/api/routes/albums/link/linkPOST.js
+++ b/src/api/routes/albums/link/linkPOST.js
@@ -14,23 +14,47 @@ class linkPOST extends Route {
/*
Make sure the album exists
*/
- const exists = await db.table('albums').where({ id: albumId, userId: user.id }).first();
+ const exists = await db
+ .table('albums')
+ .where({ id: albumId, userId: user.id })
+ .first();
if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' });
/*
Count the amount of links created for that album already and error out if max was reached
*/
- const count = await db.table('links').where('albumId', albumId).count({ count: 'id' });
- if (count[0].count >= parseInt(process.env.MAX_LINKS_PER_ALBUM, 10)) return res.status(400).json({ message: 'Maximum links per album reached' });
+ const count = await db
+ .table('links')
+ .where('albumId', albumId)
+ .count({ count: 'id' })
+ .first();
+ if (count >= parseInt(process.env.MAX_LINKS_PER_ALBUM, 10)) return res.status(400).json({ message: 'Maximum links per album reached' });
- /*
- Try to allocate a new identifier on the db
- */
- const identifier = await Util.getUniqueAlbumIdentifier();
- if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' });
+ let { identifier } = req.body;
+ if (identifier) {
+ if (!user.isAdmin) return res.status(401).json({ message: 'Only administrators can create custom links' });
+
+ if (!(/^[a-zA-Z0-9-_]+$/.test(identifier))) return res.status(400).json({ message: 'Only alphanumeric, dashes, and underscore characters are allowed' });
+
+ /*
+ Make sure that the id doesn't already exists in the database
+ */
+ const idExists = await db
+ .table('links')
+ .where({ identifier })
+ .first();
+
+ if (idExists) return res.status(400).json({ message: 'Album with this identifier already exists' });
+ } else {
+ /*
+ Try to allocate a new identifier in the database
+ */
+ identifier = await Util.getUniqueAlbumIdentifier();
+ if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' });
+ }
try {
- await db.table('links').insert({
+ const insertObj = {
identifier,
userId: user.id,
albumId,
@@ -38,11 +62,12 @@ class linkPOST extends Route {
enableDownload: true,
expiresAt: null,
views: 0
- });
+ };
+ await db.table('links').insert(insertObj);
return res.json({
message: 'The link was created successfully',
- identifier
+ data: insertObj
});
} catch (error) {
return super.error(res, error);
diff --git a/src/api/routes/auth/loginPOST.js b/src/api/routes/auth/loginPOST.js
index 205737a..71867f0 100644
--- a/src/api/routes/auth/loginPOST.js
+++ b/src/api/routes/auth/loginPOST.js
@@ -1,7 +1,7 @@
-const Route = require('../../structures/Route');
const bcrypt = require('bcrypt');
const moment = require('moment');
const JWT = require('jsonwebtoken');
+const Route = require('../../structures/Route');
class loginPOST extends Route {
constructor() {
diff --git a/src/api/routes/auth/registerPOST.js b/src/api/routes/auth/registerPOST.js
index feeb360..1cf3630 100644
--- a/src/api/routes/auth/registerPOST.js
+++ b/src/api/routes/auth/registerPOST.js
@@ -1,7 +1,7 @@
-const Route = require('../../structures/Route');
-const log = require('../../utils/Log');
const bcrypt = require('bcrypt');
const moment = require('moment');
+const Route = require('../../structures/Route');
+const log = require('../../utils/Log');
class registerPOST extends Route {
constructor() {
@@ -9,7 +9,7 @@ class registerPOST extends Route {
}
async run(req, res, db) {
- if (process.env.USER_ACCOUNTS == 'false') return res.status(401).json({ message: 'Creation of new accounts is currently disabled' });
+ if (process.env.USER_ACCOUNTS === 'false') return res.status(401).json({ message: 'Creation of new accounts is currently disabled' });
if (!req.body) return res.status(400).json({ message: 'No body provided' });
const { username, password } = req.body;
if (!username || !password) return res.status(401).json({ message: 'Invalid body provided' });
diff --git a/src/api/routes/files/albumAddPOST.js b/src/api/routes/files/albumAddPOST.js
index af39caa..7b8acf7 100644
--- a/src/api/routes/files/albumAddPOST.js
+++ b/src/api/routes/files/albumAddPOST.js
@@ -24,7 +24,8 @@ class albumAddPOST extends Route {
}
return res.json({
- message: 'Successfully added file to album'
+ message: 'Successfully added file to album',
+ data: { fileId, album: { id: album.id, name: album.name } }
});
}
}
diff --git a/src/api/routes/files/albumDelPOST.js b/src/api/routes/files/albumDelPOST.js
index 9a4b87b..8304163 100644
--- a/src/api/routes/files/albumDelPOST.js
+++ b/src/api/routes/files/albumDelPOST.js
@@ -25,7 +25,8 @@ class albumDelPOST extends Route {
}
return res.json({
- message: 'Successfully removed file from album'
+ message: 'Successfully removed file from album',
+ data: { fileId, album: { id: album.id, name: album.name } }
});
}
}
diff --git a/src/api/routes/files/fileGET.js b/src/api/routes/files/fileGET.js
new file mode 100644
index 0000000..9ec6f22
--- /dev/null
+++ b/src/api/routes/files/fileGET.js
@@ -0,0 +1,46 @@
+const Route = require('../../structures/Route');
+const Util = require('../../utils/Util');
+
+class fileGET extends Route {
+ constructor() {
+ super('/file/:id', 'get');
+ }
+
+ async run(req, res, db, user) {
+ const { id } = req.params;
+ if (!id) return res.status(400).json({ message: 'Invalid file ID supplied' });
+
+ /*
+ Make sure the file exists
+ */
+ let file = await db.table('files').where({ id, userId: user.id }).first();
+ if (!file) return res.status(400).json({ message: 'The file doesn\'t exist or doesn\'t belong to the user' });
+
+ file = Util.constructFilePublicLink(file);
+
+ /*
+ Fetch the albums
+ */
+ const albums = await db.table('albumsFiles')
+ .where('fileId', id)
+ .join('albums', 'albums.id', 'albumsFiles.albumId')
+ .select('albums.id', 'albums.name');
+
+ /*
+ Fetch the tags
+ */
+ const tags = await db.table('fileTags')
+ .where('fileId', id)
+ .join('tags', 'tags.id', 'fileTags.tagId')
+ .select('tags.id', 'tags.uuid', 'tags.name');
+
+ return res.json({
+ message: 'Successfully retrieved file',
+ file,
+ albums,
+ tags
+ });
+ }
+}
+
+module.exports = fileGET;
diff --git a/src/api/routes/files/filesAlbumsGET.js b/src/api/routes/files/filesAlbumsGET.js
index 7f1190c..90aa654 100644
--- a/src/api/routes/files/filesAlbumsGET.js
+++ b/src/api/routes/files/filesAlbumsGET.js
@@ -18,7 +18,7 @@ class filesGET extends Route {
.select('albumId');
if (albumFiles.length) {
- albumFiles = albumFiles.map(a => a.albumId);
+ albumFiles = albumFiles.map((a) => a.albumId);
albums = await db.table('albums')
.whereIn('id', albumFiles)
.select('id', 'name');
diff --git a/src/api/routes/files/filesGET.js b/src/api/routes/files/filesGET.js
index f1a3a26..9e90633 100644
--- a/src/api/routes/files/filesGET.js
+++ b/src/api/routes/files/filesGET.js
@@ -7,10 +7,26 @@ class filesGET extends Route {
}
async run(req, res, db, user) {
- // Get all the files from the user
- const files = await db.table('files')
- .where('userId', user.id)
- .orderBy('id', 'desc');
+ let count = 0;
+
+ let files = db.table('files')
+ .where({ userId: user.id })
+ .orderBy('createdAt', 'desc');
+
+ const { page, limit = 100 } = req.query;
+ if (page && page >= 0) {
+ files = await files.offset((page - 1) * limit).limit(limit);
+
+ const dbRes = await db.table('files')
+ .count('* as count')
+ .where({ userId: user.id })
+ .first();
+
+ count = dbRes.count;
+ } else {
+ files = await files; // execute the query
+ count = files.length;
+ }
// For each file, create the public link to be able to display the file
for (let file of files) {
@@ -19,7 +35,8 @@ class filesGET extends Route {
return res.json({
message: 'Successfully retrieved files',
- files
+ files,
+ count
});
}
}
diff --git a/src/api/routes/files/tagAddBatchPOST.js b/src/api/routes/files/tagAddBatchPOST.js
new file mode 100644
index 0000000..679945d
--- /dev/null
+++ b/src/api/routes/files/tagAddBatchPOST.js
@@ -0,0 +1,40 @@
+const Route = require('../../structures/Route');
+
+class tagAddBatchPOST extends Route {
+ constructor() {
+ super('/file/tag/addBatch', 'post');
+ }
+
+ async run(req, res, db, user) {
+ if (!req.body) return res.status(400).json({ message: 'No body provided' });
+ const { fileId, tagNames } = req.body;
+ if (!fileId || !tagNames.length) return res.status(400).json({ message: 'No tags provided' });
+
+ // Make sure the file belongs to the user
+ const file = await db.table('files').where({ id: fileId, userId: user.id }).first();
+ if (!file) return res.status(400).json({ message: 'File doesn\'t exist.' });
+
+ const errors = {};
+ const addedTags = [];
+ for await (const tagName of tagNames) {
+ 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 });
+
+ addedTags.push(tag);
+ } catch (e) {
+ errors[tagName] = e.message;
+ }
+ }
+
+ return res.json({
+ message: 'Successfully added tags to file',
+ data: { fileId, tags: addedTags },
+ errors
+ });
+ // eslint-disable-next-line consistent-return
+ }
+}
+
+module.exports = tagAddBatchPOST;
diff --git a/src/api/routes/files/tagAddPOST.js b/src/api/routes/files/tagAddPOST.js
index 25467ab..2bbfa07 100644
--- a/src/api/routes/files/tagAddPOST.js
+++ b/src/api/routes/files/tagAddPOST.js
@@ -7,24 +7,29 @@ class tagAddPOST extends Route {
async run(req, res, db, user) {
if (!req.body) return res.status(400).json({ message: 'No body provided' });
- const { fileId, tagNames } = req.body;
- if (!fileId || !tagNames.length) return res.status(400).json({ message: 'No tags provided' });
+
+ const { fileId, tagName } = req.body;
+ if (!fileId || !tagName.length) return res.status(400).json({ message: 'No tag provided' });
// Make sure the file belongs to the user
const file = await db.table('files').where({ id: fileId, userId: user.id }).first();
if (!file) return res.status(400).json({ message: 'File doesn\'t exist.' });
- tagNames.forEach(async tag => {
- try {
- await db.table('fileTags').insert({ fileId, tag });
- } catch (error) {
- return super.error(res, error);
- }
- });
+ // Make sure user has a tag like that
+ const tag = await db.table('tags').where({ name: tagName, userId: user.id }).first();
+ if (!tag) return res.status(400).json({ message: 'Tag doesn\'t exist. ' });
+
+ try {
+ await db.table('fileTags').insert({ fileId, tagId: tag.id });
+ } catch (error) {
+ return super.error(res, error);
+ }
return res.json({
- message: 'Successfully added file to album'
+ message: 'Successfully added tag to file',
+ data: { fileId, tag }
});
+ // eslint-disable-next-line consistent-return
}
}
diff --git a/src/api/routes/files/tagDelPOST.js b/src/api/routes/files/tagDelPOST.js
new file mode 100644
index 0000000..ac0bfe4
--- /dev/null
+++ b/src/api/routes/files/tagDelPOST.js
@@ -0,0 +1,38 @@
+const Route = require('../../structures/Route');
+
+class tagDelPost extends Route {
+ constructor() {
+ super('/file/tag/del', 'post');
+ }
+
+ async run(req, res, db, user) {
+ if (!req.body) return res.status(400).json({ message: 'No body provided' });
+
+ const { fileId, tagName } = req.body;
+ if (!fileId || !tagName.length) return res.status(400).json({ message: 'No tag provided' });
+
+ // Make sure the file belongs to the user
+ const file = await db.table('files').where({ id: fileId, userId: user.id }).first();
+ if (!file) return res.status(400).json({ message: 'File doesn\'t exist.' });
+
+ // Make sure user has a tag like that
+ const tag = await db.table('tags').where({ name: tagName, userId: user.id }).first();
+ if (!tag) return res.status(400).json({ message: 'Tag doesn\'t exist. ' });
+
+ try {
+ await db.table('fileTags')
+ .where({ fileId, tagId: tag.id })
+ .delete();
+ } catch (error) {
+ return super.error(res, error);
+ }
+
+ return res.json({
+ message: 'Successfully removed tag from file',
+ data: { fileId, tag }
+ });
+ // eslint-disable-next-line consistent-return
+ }
+}
+
+module.exports = tagDelPost;
diff --git a/src/api/routes/search/searchGET.js b/src/api/routes/search/searchGET.js
new file mode 100644
index 0000000..40107d8
--- /dev/null
+++ b/src/api/routes/search/searchGET.js
@@ -0,0 +1,63 @@
+const searchQuery = require('search-query-parser');
+
+const Route = require('../../structures/Route');
+const Util = require('../../utils/Util');
+
+const queryHelper = require('../../utils/QueryHelper');
+
+const options = {
+ keywords: ['album', 'tag', 'before', 'after', 'file'],
+ offsets: false,
+ alwaysArray: true,
+ tokenize: true
+};
+
+class configGET extends Route {
+ constructor() {
+ super('/search/', 'get');
+ }
+
+ async run(req, res, db, user) {
+ let count = 0;
+
+ const { q } = req.query;
+ const parsed = searchQuery.parse(q, options);
+
+ let files = db.table('files')
+ .select('*')
+ .where({ 'files.userId': user.id })
+ .orderBy('files.createdAt', 'desc');
+
+ files = queryHelper.processQuery(db, files, parsed);
+
+ const query = files.toString();
+ const { page, limit = 100 } = req.query;
+
+ if (page && page >= 0) {
+ let dbRes = files.clone(); // clone the query to attach a count to it later on
+ files = await files.offset((page - 1) * limit).limit(limit);
+
+ dbRes = await dbRes.count('* as count').first();
+
+ count = dbRes.count;
+ } else {
+ files = await files; // execute the query
+ count = files.length;
+ }
+
+ // For each file, create the public link to be able to display the file
+ for (let file of files) {
+ file = Util.constructFilePublicLink(file);
+ }
+
+ return res.json({
+ message: 'Successfully retrieved files',
+ query,
+ parsed,
+ files,
+ count
+ });
+ }
+}
+
+module.exports = configGET;
diff --git a/src/api/routes/service/configGET.js b/src/api/routes/service/configGET.js
index b653066..bc91a7e 100644
--- a/src/api/routes/service/configGET.js
+++ b/src/api/routes/service/configGET.js
@@ -15,10 +15,10 @@ class configGET extends Route {
maxUploadSize: parseInt(process.env.MAX_SIZE, 10),
filenameLength: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10),
albumLinkLength: parseInt(process.env.GENERATED_ALBUM_LENGTH, 10),
- generateThumbnails: process.env.GENERATE_THUMBNAILS == 'true' ? true : false,
- generateZips: process.env.GENERATE_ZIPS == 'true' ? true : false,
- publicMode: process.env.PUBLIC_MODE == 'true' ? true : false,
- enableAccounts: process.env.USER_ACCOUNTS == 'true' ? true : false
+ generateThumbnails: process.env.GENERATE_THUMBNAILS === 'true',
+ generateZips: process.env.GENERATE_ZIPS === 'true',
+ publicMode: process.env.PUBLIC_MODE === 'true',
+ enableAccounts: process.env.USER_ACCOUNTS === 'true'
}
});
}
diff --git a/src/api/routes/tags/tagDELETE.js b/src/api/routes/tags/tagDELETE.js
index c03ca64..cf74029 100644
--- a/src/api/routes/tags/tagDELETE.js
+++ b/src/api/routes/tags/tagDELETE.js
@@ -27,7 +27,7 @@ class tagDELETE extends Route {
Delete the tag
*/
await db.table('tags').where({ id }).delete();
- return res.json({ message: 'The tag was deleted successfully' });
+ 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 b6ec395..89b296d 100644
--- a/src/api/routes/tags/tagPOST.js
+++ b/src/api/routes/tags/tagPOST.js
@@ -1,5 +1,5 @@
-const Route = require('../../structures/Route');
const moment = require('moment');
+const Route = require('../../structures/Route');
class tagPOST extends Route {
constructor() {
@@ -18,14 +18,18 @@ class tagPOST extends Route {
if (tag) return res.status(401).json({ message: 'There\'s already a tag with that name' });
const now = moment.utc().toDate();
- await db.table('tags').insert({
+ const insertObj = {
name,
userId: user.id,
createdAt: now,
editedAt: now
- });
+ };
+
+ const dbRes = await db.table('tags').insert(insertObj);
+
+ insertObj.id = dbRes.pop();
- return res.json({ message: 'The tag was created successfully' });
+ return res.json({ message: 'The tag was created successfully', data: insertObj });
}
}
diff --git a/src/api/routes/tags/tagsGET.js b/src/api/routes/tags/tagsGET.js
index 871148e..329d789 100644
--- a/src/api/routes/tags/tagsGET.js
+++ b/src/api/routes/tags/tagsGET.js
@@ -1,5 +1,4 @@
const Route = require('../../structures/Route');
-const Util = require('../../utils/Util');
class tagsGET extends Route {
constructor() {
diff --git a/src/api/routes/uploads/chunksPOST.js b/src/api/routes/uploads/chunksPOST.js
index 013c0d6..061cfb0 100644
--- a/src/api/routes/uploads/chunksPOST.js
+++ b/src/api/routes/uploads/chunksPOST.js
@@ -1,8 +1,8 @@
-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() {
@@ -12,7 +12,7 @@ class uploadPOST extends Route {
});
}
- async run(req, res, db) {
+ async run(req, res) {
const filename = Util.getUniqueFilename(randomstring.generate(32));
// console.log('Files', req.body.files);
const info = {
@@ -21,24 +21,18 @@ class uploadPOST extends Route {
};
for (const chunk of req.body.files) {
- const { uuid, count } = chunk;
+ const { uuid } = 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();
@@ -49,10 +43,7 @@ class uploadPOST extends Route {
for (let i = 0; i < chunkDir.length; i++) {
const dir = path.join(__dirname,
- '..',
- '..',
- '..',
- '..',
+ '../../../../',
process.env.UPLOAD_FOLDER,
'chunks',
uuid,
diff --git a/src/api/routes/uploads/uploadPOST.js b/src/api/routes/uploads/uploadPOST.js
index 4b84da6..567862a 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
},
- 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,22 +20,21 @@ 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 {
@@ -47,7 +47,7 @@ class uploadPOST extends Route {
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);
@@ -147,7 +142,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');
})
@@ -222,6 +217,7 @@ class uploadPOST extends Route {
}
return body;
}
+ return keys;
}
}
diff --git a/src/api/routes/user/apiKey.js b/src/api/routes/user/apiKey.js
index a87d98d..653c56a 100644
--- a/src/api/routes/user/apiKey.js
+++ b/src/api/routes/user/apiKey.js
@@ -1,7 +1,7 @@
-const Route = require('../../structures/Route');
const randomstring = require('randomstring');
const moment = require('moment');
const { dump } = require('dumper.js');
+const Route = require('../../structures/Route');
class apiKeyPOST extends Route {
constructor() {
diff --git a/src/api/routes/user/changePasswordPOST.js b/src/api/routes/user/changePasswordPOST.js
index 9cd621e..82bce40 100644
--- a/src/api/routes/user/changePasswordPOST.js
+++ b/src/api/routes/user/changePasswordPOST.js
@@ -1,7 +1,7 @@
-const Route = require('../../structures/Route');
-const log = require('../../utils/Log');
const bcrypt = require('bcrypt');
const moment = require('moment');
+const Route = require('../../structures/Route');
+const log = require('../../utils/Log');
class changePasswordPOST extends Route {
constructor() {
diff --git a/src/api/routes/user/userGET.js b/src/api/routes/user/userGET.js
index fe46fd4..7929aac 100644
--- a/src/api/routes/user/userGET.js
+++ b/src/api/routes/user/userGET.js
@@ -11,7 +11,8 @@ class usersGET extends Route {
user: {
id: user.id,
username: user.username,
- isAdmin: user.isAdmin
+ isAdmin: user.isAdmin,
+ apiKey: user.apiKey
}
});
}