aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/albums/link
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/albums/link
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/albums/link')
-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
3 files changed, 49 insertions, 21 deletions
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);