From e7767ac7095f93393a627fd5e867af4a1ca4b011 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Sun, 16 Sep 2018 00:56:13 -0300 Subject: Routes --- src/api/routes/albums/link/linkEnabledPOST.js | 34 +++++++++++++++++++++ src/api/routes/albums/link/linkPOST.js | 43 +++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/api/routes/albums/link/linkEnabledPOST.js create mode 100644 src/api/routes/albums/link/linkPOST.js (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEnabledPOST.js b/src/api/routes/albums/link/linkEnabledPOST.js new file mode 100644 index 0000000..863fe0b --- /dev/null +++ b/src/api/routes/albums/link/linkEnabledPOST.js @@ -0,0 +1,34 @@ +const Route = require('../../../structures/Route'); +const config = require('../../../../../config'); +const db = require('knex')(config.server.database); +const log = require('../../../utils/Log'); + +class linkEnabledPOST extends Route { + constructor() { + super('/album/link/enabled', 'post'); + } + + async run(req, res, user) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { identifier, enabled } = req.body; + if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); + + 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') + .where({ identifier }) + .update({ enabled }); + return res.json({ message: 'The link status was changed successfully' }); + } catch (error) { + log.error(error); + return res.json({ message: 'There was a problem changing the status of the link' }); + } + } +} + +module.exports = linkEnabledPOST; diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js new file mode 100644 index 0000000..e8f3731 --- /dev/null +++ b/src/api/routes/albums/link/linkPOST.js @@ -0,0 +1,43 @@ +const Route = require('../../../structures/Route'); +const config = require('../../../../../config'); +const db = require('knex')(config.server.database); +const Util = require('../../../utils/Util'); +const log = require('../../../utils/Log'); + +class linkPOST extends Route { + constructor() { + super('/album/link/new', 'post'); + } + + async run(req, res) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { albumId, enabled, enableDownload, expiresAt } = req.body; + if (!albumId) return res.status(400).json({ message: 'No album provided' }); + + const exists = await db.table('albums').where('id', albumId).first(); + if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' }); + + const identifier = 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({ + identifier, + albumId, + enabled, + enableDownload, + expiresAt + }); + + return res.json({ + message: 'The link was created successfully', + identifier + }); + } catch (error) { + log.error(error); + return res.status(500).json({ message: 'There was a problem creating the link' }); + } + } +} + +module.exports = linkPOST; -- cgit v1.2.3 From e073fb4317ae8bd55dfcd0531de10e67383aa408 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Sun, 16 Sep 2018 17:52:46 -0300 Subject: Links can now be created --- src/api/routes/albums/link/linkPOST.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index e8f3731..26a527a 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -11,22 +11,22 @@ class linkPOST extends Route { async run(req, res) { if (!req.body) return res.status(400).json({ message: 'No body provided' }); - const { albumId, enabled, enableDownload, expiresAt } = req.body; + const { albumId } = req.body; if (!albumId) return res.status(400).json({ message: 'No album provided' }); const exists = await db.table('albums').where('id', albumId).first(); if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' }); - const identifier = Util.getUniqueAlbumIdentifier(); + const 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({ identifier, albumId, - enabled, - enableDownload, - expiresAt + enabled: true, + enableDownload: true, + expiresAt: null }); return res.json({ -- cgit v1.2.3 From 46ed1c6a824252fc5ae0dad5b5c2a369bad9ad39 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Mon, 17 Sep 2018 04:37:27 -0300 Subject: This route should handle more stuff, so it does now --- src/api/routes/albums/link/linkEditPOST.js | 38 +++++++++++++++++++++++++++ src/api/routes/albums/link/linkEnabledPOST.js | 34 ------------------------ 2 files changed, 38 insertions(+), 34 deletions(-) create mode 100644 src/api/routes/albums/link/linkEditPOST.js delete mode 100644 src/api/routes/albums/link/linkEnabledPOST.js (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js new file mode 100644 index 0000000..46b851a --- /dev/null +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -0,0 +1,38 @@ +const Route = require('../../../structures/Route'); +const config = require('../../../../../config'); +const db = require('knex')(config.server.database); +const log = require('../../../utils/Log'); + +class linkEditPOST extends Route { + constructor() { + super('/album/link/edit', 'post'); + } + + async run(req, res, user) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { identifier, enabled, enableDownload, expiresAt } = req.body; + if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); + + 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') + .where({ identifier }) + .update({ + enabled: enabled || false, + enableDownload: enableDownload || false, + expiresAt // This one should be null if not supplied + }); + return res.json({ message: 'Editing the link was successfully' }); + } catch (error) { + log.error(error); + return res.json({ message: 'There was a problem editing the link' }); + } + } +} + +module.exports = linkEditPOST; diff --git a/src/api/routes/albums/link/linkEnabledPOST.js b/src/api/routes/albums/link/linkEnabledPOST.js deleted file mode 100644 index 863fe0b..0000000 --- a/src/api/routes/albums/link/linkEnabledPOST.js +++ /dev/null @@ -1,34 +0,0 @@ -const Route = require('../../../structures/Route'); -const config = require('../../../../../config'); -const db = require('knex')(config.server.database); -const log = require('../../../utils/Log'); - -class linkEnabledPOST extends Route { - constructor() { - super('/album/link/enabled', 'post'); - } - - async run(req, res, user) { - if (!req.body) return res.status(400).json({ message: 'No body provided' }); - const { identifier, enabled } = req.body; - if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); - - 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') - .where({ identifier }) - .update({ enabled }); - return res.json({ message: 'The link status was changed successfully' }); - } catch (error) { - log.error(error); - return res.json({ message: 'There was a problem changing the status of the link' }); - } - } -} - -module.exports = linkEnabledPOST; -- cgit v1.2.3 From c2c6e99878853fafdbd5e708c3163921f8529ae1 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Mon, 17 Sep 2018 04:38:25 -0300 Subject: Public albums wooo! --- src/api/routes/albums/link/linkPOST.js | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 26a527a..9c8c0bc 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -17,6 +17,9 @@ class linkPOST extends Route { const exists = await db.table('albums').where('id', albumId).first(); if (!exists) return res.status(400).json({ message: 'Album doesn\t exist' }); + const count = await db.table('links').where('albumId', albumId).count({ count: 'id' }); + if (count[0].count >= config.albums.maxLinksPerAlbum) return res.status(400).json({ message: 'Maximum links per album reached' }); + const identifier = await Util.getUniqueAlbumIdentifier(); if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' }); -- cgit v1.2.3 From f2c885b718528d42df412e612520fb471c46d0bd Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Mon, 17 Sep 2018 04:55:42 -0300 Subject: Commented all the code --- src/api/routes/albums/link/linkEditPOST.js | 10 +++++----- src/api/routes/albums/link/linkPOST.js | 9 +++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 46b851a..d9dbcac 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -13,12 +13,12 @@ class linkEditPOST extends Route { const { identifier, enabled, enableDownload, expiresAt } = req.body; if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); - const link = await db.table('links').where({ - identifier, - userId: user.id - }).first(); - + /* + 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' }); + try { await db.table('links') .where({ identifier }) diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 9c8c0bc..4b24eae 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -14,12 +14,21 @@ class linkPOST extends Route { const { albumId } = req.body; if (!albumId) return res.status(400).json({ message: 'No album provided' }); + /* + Make sure the album exists + */ const exists = await db.table('albums').where('id', albumId).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 >= config.albums.maxLinksPerAlbum) 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' }); -- cgit v1.2.3 From 4b2b02110b457d8ebeee78e1bdf99eb0660d0626 Mon Sep 17 00:00:00 2001 From: Pitu <7425261+Pitu@users.noreply.github.com> Date: Tue, 18 Sep 2018 03:34:00 -0300 Subject: We can now download albums yayyyy --- src/api/routes/albums/link/linkPOST.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 4b24eae..1edf891 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -9,7 +9,7 @@ class linkPOST extends Route { super('/album/link/new', 'post'); } - async run(req, res) { + async run(req, res, user) { if (!req.body) return res.status(400).json({ message: 'No body provided' }); const { albumId } = req.body; if (!albumId) return res.status(400).json({ message: 'No album provided' }); @@ -35,6 +35,7 @@ class linkPOST extends Route { try { await db.table('links').insert({ identifier, + userId: user.id, albumId, enabled: true, enableDownload: true, -- cgit v1.2.3 From 89a271818ed25b0a17a17dd1d6804e34d1f2ec0f Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 19 Feb 2019 23:52:24 +0900 Subject: Switch config to .env --- src/api/routes/albums/link/linkEditPOST.js | 4 +--- src/api/routes/albums/link/linkPOST.js | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index d9dbcac..753c496 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -1,6 +1,4 @@ const Route = require('../../../structures/Route'); -const config = require('../../../../../config'); -const db = require('knex')(config.server.database); const log = require('../../../utils/Log'); class linkEditPOST extends Route { @@ -8,7 +6,7 @@ class linkEditPOST extends Route { super('/album/link/edit', 'post'); } - async run(req, res, user) { + async run(req, res, db, user) { if (!req.body) return res.status(400).json({ message: 'No body provided' }); const { identifier, enabled, enableDownload, expiresAt } = req.body; if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 1edf891..91e1521 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -1,6 +1,4 @@ const Route = require('../../../structures/Route'); -const config = require('../../../../../config'); -const db = require('knex')(config.server.database); const Util = require('../../../utils/Util'); const log = require('../../../utils/Log'); @@ -9,7 +7,7 @@ class linkPOST extends Route { super('/album/link/new', 'post'); } - async run(req, res, user) { + async run(req, res, db, user) { if (!req.body) return res.status(400).json({ message: 'No body provided' }); const { albumId } = req.body; if (!albumId) return res.status(400).json({ message: 'No album provided' }); @@ -24,7 +22,7 @@ class linkPOST extends Route { 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 >= config.albums.maxLinksPerAlbum) return res.status(400).json({ message: 'Maximum links per album reached' }); + if (count[0].count >= process.env.MAX_LINKS_PER_ALBUM) return res.status(400).json({ message: 'Maximum links per album reached' }); /* Try to allocate a new identifier on the db -- cgit v1.2.3 From fc95cb7b0f047806937c25f0fc1104c72b0a32cb Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 23 Feb 2019 00:45:45 +0900 Subject: Better DB handling and stuff --- src/api/routes/albums/link/linkPOST.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 91e1521..968e57d 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -37,7 +37,8 @@ class linkPOST extends Route { albumId, enabled: true, enableDownload: true, - expiresAt: null + expiresAt: null, + views: 0 }); return res.json({ -- cgit v1.2.3 From 73d85e8c7938e1db30da3cc4354b143d4a078473 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 2 Mar 2019 02:08:11 +0900 Subject: Enviroment variables parsing fix --- src/api/routes/albums/link/linkPOST.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 968e57d..e929c89 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -22,7 +22,7 @@ class linkPOST extends Route { 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 >= process.env.MAX_LINKS_PER_ALBUM) return res.status(400).json({ message: 'Maximum links per album reached' }); + if (count[0].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 -- cgit v1.2.3 From 789f5fc259b90dd6a3b21fd2aef1a9e54a19506e Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 2 Mar 2019 22:16:35 +0900 Subject: Removed google analytics --- src/api/routes/albums/link/linkEditPOST.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 753c496..bb3c41b 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -25,7 +25,7 @@ class linkEditPOST extends Route { enableDownload: enableDownload || false, expiresAt // This one should be null if not supplied }); - return res.json({ message: 'Editing the link was successfully' }); + return res.json({ message: 'Editing the link was successfull' }); } catch (error) { log.error(error); return res.json({ message: 'There was a problem editing the link' }); -- cgit v1.2.3 From 71f24504317a8391209789275549a94be5c99e4e Mon Sep 17 00:00:00 2001 From: Pitu Date: Fri, 8 Mar 2019 00:47:30 +0900 Subject: WIP --- src/api/routes/albums/link/linkDELETE.js | 36 ++++++++++++++++++++++++++++++ src/api/routes/albums/link/linkEditPOST.js | 3 +-- 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 src/api/routes/albums/link/linkDELETE.js (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js new file mode 100644 index 0000000..4f948ba --- /dev/null +++ b/src/api/routes/albums/link/linkDELETE.js @@ -0,0 +1,36 @@ +const Route = require('../../../structures/Route'); + +class linkDELETE extends Route { + constructor() { + super('/album/link/delete/:identifier', 'delete'); + } + + async run(req, res, db) { + const { identifier } = req.params; + if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' }); + + try { + const link = await db.table('links') + .where({ identifier }) + .first(); + + if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist' }); + + await db.table('links') + .where({ id: link.id }) + .delete(); + await db.table('albumsLinks') + .where({ linkId: link.id }) + .delete(); + } catch (error) { + console.log(error); + return super.error(res, error); + } + + return res.json({ + message: 'Successfully deleted link' + }); + } +} + +module.exports = linkDELETE; diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index bb3c41b..1db0a53 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -8,7 +8,7 @@ class linkEditPOST extends Route { async run(req, res, db, user) { if (!req.body) return res.status(400).json({ message: 'No body provided' }); - const { identifier, enabled, enableDownload, expiresAt } = req.body; + const { identifier, enableDownload, expiresAt } = req.body; if (!identifier) return res.status(400).json({ message: 'Invalid album identifier supplied' }); /* @@ -21,7 +21,6 @@ class linkEditPOST extends Route { await db.table('links') .where({ identifier }) .update({ - enabled: enabled || false, enableDownload: enableDownload || false, expiresAt // This one should be null if not supplied }); -- cgit v1.2.3 From 85ac74483764de66d2be0f6ea1ff84626e32ffff Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 12 Mar 2019 05:31:55 +0000 Subject: Typo --- src/api/routes/albums/link/linkEditPOST.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 1db0a53..0586f08 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -24,7 +24,7 @@ class linkEditPOST extends Route { enableDownload: enableDownload || false, expiresAt // This one should be null if not supplied }); - return res.json({ message: 'Editing the link was successfull' }); + return res.json({ message: 'Editing the link was successful' }); } catch (error) { log.error(error); return res.json({ message: 'There was a problem editing the link' }); -- cgit v1.2.3 From dd98cecfebff9f98383d604b9a20fa8a8f32380a Mon Sep 17 00:00:00 2001 From: Pitu Date: Tue, 12 Mar 2019 05:38:13 +0000 Subject: Error consistency --- src/api/routes/albums/link/linkDELETE.js | 1 - src/api/routes/albums/link/linkEditPOST.js | 3 +-- src/api/routes/albums/link/linkPOST.js | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 4f948ba..d6d98c4 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -23,7 +23,6 @@ class linkDELETE extends Route { .where({ linkId: link.id }) .delete(); } catch (error) { - console.log(error); return super.error(res, error); } diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 0586f08..6776b73 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -26,8 +26,7 @@ class linkEditPOST extends Route { }); return res.json({ message: 'Editing the link was successful' }); } catch (error) { - log.error(error); - return res.json({ message: 'There was a problem editing the link' }); + return super.error(res, error); } } } diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index e929c89..297348c 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -46,8 +46,7 @@ class linkPOST extends Route { identifier }); } catch (error) { - log.error(error); - return res.status(500).json({ message: 'There was a problem creating the link' }); + return super.error(res, error); } } } -- cgit v1.2.3 From f06c8c9d336cbee561d9a80abc78568c28463e52 Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 14 Mar 2019 23:14:45 +0900 Subject: dunno what's wrong here yet --- src/api/routes/albums/link/linkDELETE.js | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index d6d98c4..3ec4d9d 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -1,4 +1,5 @@ const Route = require('../../../structures/Route'); +const { dump } = require('dumper.js'); class linkDELETE extends Route { constructor() { @@ -6,6 +7,10 @@ class linkDELETE extends Route { } async run(req, res, db) { + console.log('------------------------------'); + console.log('YES HI'); + console.log('------------------------------'); + console.log('WHO NEEDS FANCY DEBUGGING TOOLS ANYWAYS'); const { identifier } = req.params; if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' }); @@ -14,6 +19,8 @@ class linkDELETE extends Route { .where({ identifier }) .first(); + dump(link); + if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist' }); await db.table('links') -- cgit v1.2.3 From 4db167ec43806e20cc40932a292efc2909e69328 Mon Sep 17 00:00:00 2001 From: Pitu Date: Mon, 30 Sep 2019 07:24:37 +0000 Subject: Fix deletion of albums and links --- src/api/routes/albums/link/linkDELETE.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 3ec4d9d..7adcaac 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -7,10 +7,6 @@ class linkDELETE extends Route { } async run(req, res, db) { - console.log('------------------------------'); - console.log('YES HI'); - console.log('------------------------------'); - console.log('WHO NEEDS FANCY DEBUGGING TOOLS ANYWAYS'); const { identifier } = req.params; if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' }); -- cgit v1.2.3 From bca8fbcd839d2239e3f6f141f662fbbc74726835 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 12 Oct 2019 21:14:19 +0900 Subject: refactor: removed useless code, cleaned up, fixed permissions --- src/api/routes/albums/link/linkDELETE.js | 4 ++-- src/api/routes/albums/link/linkPOST.js | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 7adcaac..904687f 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -6,13 +6,13 @@ class linkDELETE extends Route { super('/album/link/delete/:identifier', 'delete'); } - async run(req, res, db) { + async run(req, res, db, user) { const { identifier } = req.params; if (!identifier) return res.status(400).json({ message: 'Invalid identifier supplied' }); try { const link = await db.table('links') - .where({ identifier }) + .where({ identifier, userId: user.id }) .first(); dump(link); diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 297348c..6009922 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -1,6 +1,5 @@ const Route = require('../../../structures/Route'); const Util = require('../../../utils/Util'); -const log = require('../../../utils/Log'); class linkPOST extends Route { constructor() { @@ -15,7 +14,7 @@ class linkPOST extends Route { /* Make sure the album exists */ - const exists = await db.table('albums').where('id', albumId).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' }); /* -- cgit v1.2.3 From b886fda0793b8a26de58cd462acf6676a0a8e7ed Mon Sep 17 00:00:00 2001 From: Pitu Date: Mon, 11 May 2020 00:19:10 +0900 Subject: chore: cleanup and todo --- src/api/routes/albums/link/linkDELETE.js | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 904687f..23db411 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -15,8 +15,6 @@ class linkDELETE extends Route { .where({ identifier, userId: user.id }) .first(); - dump(link); - if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist' }); await db.table('links') -- cgit v1.2.3 From b526d8803696161961ffb9eb912cb4b83a3c9eff Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 25 Jun 2020 01:35:52 +0900 Subject: Optimize the queries fetching albums/files --- src/api/routes/albums/link/linksGET.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/api/routes/albums/link/linksGET.js (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linksGET.js b/src/api/routes/albums/link/linksGET.js new file mode 100644 index 0000000..edab49a --- /dev/null +++ b/src/api/routes/albums/link/linksGET.js @@ -0,0 +1,22 @@ +const Route = require('../../../structures/Route'); + +class linkPOST extends Route { + constructor() { + super('/album/:id/links', 'get'); + } + + async run(req, res, db, user) { + const { id } = req.params; + if (!id) return res.status(400).json({ message: 'Invalid id supplied' }); + + const links = await db.table('links') + .where({ albumId: id, userId: user.id }); + + return res.json({ + message: 'Successfully retrieved links', + links + }); + } +} + +module.exports = linkPOST; -- cgit v1.2.3 From a9fe08f9e577de8df0f742a428214c42e532d04c Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 25 Jun 2020 01:36:04 +0900 Subject: Update error message --- src/api/routes/albums/link/linkDELETE.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 23db411..b02d0b4 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -15,7 +15,7 @@ class linkDELETE extends Route { .where({ identifier, userId: user.id }) .first(); - if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist' }); + if (!link) return res.status(400).json({ message: 'Identifier doesn\'t exist or doesnt\'t belong to the user' }); await db.table('links') .where({ id: link.id }) -- cgit v1.2.3 From b620aa981546cb42e19f64d5349a9372e3d0c269 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Sat, 4 Jul 2020 03:25:04 +0300 Subject: feat: refactor some of the queries to returned added/updated data --- src/api/routes/albums/link/linkEditPOST.js | 21 +++++++++++++-------- src/api/routes/albums/link/linkPOST.js | 24 +++++++++++++++++------- 2 files changed, 30 insertions(+), 15 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 6776b73..0c7233b 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -14,17 +14,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..7ecc5cb 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -14,23 +14,32 @@ 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' }); + 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 +47,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); -- cgit v1.2.3 From ad852de51a0d2dd5d29c08838d5a430c58849e74 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Wed, 8 Jul 2020 04:00:12 +0300 Subject: chore: linter the entire project using the new rules --- src/api/routes/albums/link/linkDELETE.js | 3 +-- src/api/routes/albums/link/linkEditPOST.js | 3 +-- src/api/routes/albums/link/linkPOST.js | 10 ++++------ src/api/routes/albums/link/linksGET.js | 2 +- 4 files changed, 7 insertions(+), 11 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index b02d0b4..0381b50 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() { @@ -28,7 +27,7 @@ class linkDELETE extends Route { } return res.json({ - message: 'Successfully deleted link' + message: 'Successfully deleted link', }); } } diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 0c7233b..4e0e0e1 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() { @@ -23,7 +22,7 @@ class linkEditPOST extends Route { try { const updateObj = { enableDownload: enableDownload || false, - expiresAt // This one should be null if not supplied + expiresAt, // This one should be null if not supplied }; await db .table('links') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 7ecc5cb..d58598a 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -28,15 +28,13 @@ class linkPOST extends Route { .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' }); + 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' }); + if (!identifier) return res.status(500).json({ message: 'There was a problem allocating a link for your album' }); try { const insertObj = { @@ -46,13 +44,13 @@ class linkPOST extends Route { enabled: true, enableDownload: true, expiresAt: null, - views: 0 + views: 0, }; await db.table('links').insert(insertObj); return res.json({ message: 'The link was created successfully', - data: insertObj + data: insertObj, }); } catch (error) { return super.error(res, error); diff --git a/src/api/routes/albums/link/linksGET.js b/src/api/routes/albums/link/linksGET.js index edab49a..4487c26 100644 --- a/src/api/routes/albums/link/linksGET.js +++ b/src/api/routes/albums/link/linksGET.js @@ -14,7 +14,7 @@ class linkPOST extends Route { return res.json({ message: 'Successfully retrieved links', - links + links, }); } } -- cgit v1.2.3 From 151c360740aac5733759888220d91a1d3713b6e1 Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Fri, 2 Oct 2020 22:16:34 +0300 Subject: feat: allow administrators to create custom links for albums --- src/api/routes/albums/link/linkPOST.js | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index d58598a..ba247b5 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -30,11 +30,28 @@ class linkPOST extends Route { .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 { const insertObj = { -- cgit v1.2.3 From 90001c2df56d58e69fd199a518ae7f3e4ed327fc Mon Sep 17 00:00:00 2001 From: Zephyrrus Date: Thu, 24 Dec 2020 10:40:50 +0200 Subject: chore: remove trailing commas --- src/api/routes/albums/link/linkDELETE.js | 2 +- src/api/routes/albums/link/linkEditPOST.js | 2 +- src/api/routes/albums/link/linkPOST.js | 4 ++-- src/api/routes/albums/link/linksGET.js | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/api/routes/albums/link') diff --git a/src/api/routes/albums/link/linkDELETE.js b/src/api/routes/albums/link/linkDELETE.js index 0381b50..1af704e 100644 --- a/src/api/routes/albums/link/linkDELETE.js +++ b/src/api/routes/albums/link/linkDELETE.js @@ -27,7 +27,7 @@ class linkDELETE extends Route { } return res.json({ - message: 'Successfully deleted link', + message: 'Successfully deleted link' }); } } diff --git a/src/api/routes/albums/link/linkEditPOST.js b/src/api/routes/albums/link/linkEditPOST.js index 4e0e0e1..97122a2 100644 --- a/src/api/routes/albums/link/linkEditPOST.js +++ b/src/api/routes/albums/link/linkEditPOST.js @@ -22,7 +22,7 @@ class linkEditPOST extends Route { try { const updateObj = { enableDownload: enableDownload || false, - expiresAt, // This one should be null if not supplied + expiresAt // This one should be null if not supplied }; await db .table('links') diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index ba247b5..28e9dfe 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -61,13 +61,13 @@ class linkPOST extends Route { enabled: true, enableDownload: true, expiresAt: null, - views: 0, + views: 0 }; await db.table('links').insert(insertObj); return res.json({ message: 'The link was created successfully', - data: insertObj, + data: insertObj }); } catch (error) { return super.error(res, error); diff --git a/src/api/routes/albums/link/linksGET.js b/src/api/routes/albums/link/linksGET.js index 4487c26..edab49a 100644 --- a/src/api/routes/albums/link/linksGET.js +++ b/src/api/routes/albums/link/linksGET.js @@ -14,7 +14,7 @@ class linkPOST extends Route { return res.json({ message: 'Successfully retrieved links', - links, + links }); } } -- cgit v1.2.3