From ce76a8ec7ba2084ab75fb901933b3d4d84505032 Mon Sep 17 00:00:00 2001 From: Pitu Date: Sat, 2 Mar 2019 22:07:24 +0900 Subject: [PATCH] Be able to add a file to multiple albums --- src/api/routes/files/albumAddPOST.js | 26 ++++++++++ src/api/routes/files/albumDelPOST.js | 27 +++++++++++ src/api/routes/files/filesGET.js | 15 ++++++ src/api/routes/files/uploadPOST.js | 4 -- src/api/utils/Util.js | 2 +- src/site/components/grid/Grid.vue | 2 +- src/site/pages/dashboard/index.vue | 72 ++++++++++++++++++++++++---- 7 files changed, 134 insertions(+), 14 deletions(-) create mode 100644 src/api/routes/files/albumAddPOST.js create mode 100644 src/api/routes/files/albumDelPOST.js diff --git a/src/api/routes/files/albumAddPOST.js b/src/api/routes/files/albumAddPOST.js new file mode 100644 index 0000000..fc4ee71 --- /dev/null +++ b/src/api/routes/files/albumAddPOST.js @@ -0,0 +1,26 @@ +const Route = require('../../structures/Route'); + +class albumAddPOST extends Route { + constructor() { + super('/file/album/add', 'post'); + } + + async run(req, res, db) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { fileId, albumId } = req.body; + if (!fileId || !albumId) return res.status(400).json({ message: 'No id provided' }); + + try { + await db.table('albumsFiles') + .insert({ fileId, albumId }); + } catch (error) { + return super.error(res, error); + } + + return res.json({ + message: 'Successfully added file to album' + }); + } +} + +module.exports = albumAddPOST; diff --git a/src/api/routes/files/albumDelPOST.js b/src/api/routes/files/albumDelPOST.js new file mode 100644 index 0000000..fd6bbd0 --- /dev/null +++ b/src/api/routes/files/albumDelPOST.js @@ -0,0 +1,27 @@ +const Route = require('../../structures/Route'); + +class albumDelPOST extends Route { + constructor() { + super('/file/album/del', 'post'); + } + + async run(req, res, db) { + if (!req.body) return res.status(400).json({ message: 'No body provided' }); + const { fileId, albumId } = req.body; + if (!fileId || !albumId) return res.status(400).json({ message: 'No id provided' }); + + try { + await db.table('albumsFiles') + .where({ fileId, albumId }) + .delete(); + } catch (error) { + return super.error(res, error); + } + + return res.json({ + message: 'Successfully removed file from album' + }); + } +} + +module.exports = albumDelPOST; diff --git a/src/api/routes/files/filesGET.js b/src/api/routes/files/filesGET.js index b41996b..ce288ff 100644 --- a/src/api/routes/files/filesGET.js +++ b/src/api/routes/files/filesGET.js @@ -14,6 +14,21 @@ class filesGET extends Route { .where('userId', user.id) .orderBy('id', 'desc'); + for (const file of files) { + file.albums = []; + const albumFiles = await db.table('albumsFiles') + .where('fileId', file.id); + if (!albumFiles.length) continue; + + for (const albumFile of albumFiles) { + const album = await db.table('albums') + .where('id', albumFile.albumId) + .select('id', 'name') + .first(); + if (!album) continue; + file.albums.push(album); + } + } /* For each file, create the public link to be able to display the file */ diff --git a/src/api/routes/files/uploadPOST.js b/src/api/routes/files/uploadPOST.js index e88091a..ef95b83 100644 --- a/src/api/routes/files/uploadPOST.js +++ b/src/api/routes/files/uploadPOST.js @@ -6,14 +6,11 @@ const log = require('../../utils/Log'); const jetpack = require('fs-jetpack'); const Busboy = require('busboy'); const fs = require('fs'); -const { dump } = require('dumper.js'); /* TODO: Strip exif data if the owner/user configured it as such 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. TODO: If source is a video, generate a thumb of the first frame and save the video length. - TODO: Check that the async isAuthorized works and is not nulling out - TODO: Lowercase the file extensions */ class uploadPOST extends Route { @@ -23,7 +20,6 @@ class uploadPOST extends Route { async run(req, res, db) { const user = await Util.isAuthorized(req); - // TODO: .env variables are all casted to strings. pepehands if (!user && process.env.PUBLIC_MODE == 'false') return res.status(401).json({ message: 'Not authorized to use this resource' }); return this.uploadFile(req, res, db, user); } diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index 1242a5a..a514e20 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -108,7 +108,7 @@ class Util { const filename = randomstring.generate({ length: parseInt(process.env.GENERATED_FILENAME_LENGTH, 10), capitalization: 'lowercase' - }) + path.extname(name); + }) + path.extname(name).toLowerCase(); const exists = jetpack.exists(path.join(__dirname, '..', '..', '..', process.env.UPLOAD_FOLDER, filename)); if (!exists) return filename; if (i < 5) return retry(i + 1); diff --git a/src/site/components/grid/Grid.vue b/src/site/components/grid/Grid.vue index 615d68f..e136f90 100644 --- a/src/site/components/grid/Grid.vue +++ b/src/site/components/grid/Grid.vue @@ -104,7 +104,7 @@ - + diff --git a/src/site/pages/dashboard/index.vue b/src/site/pages/dashboard/index.vue index 3834522..76c465b 100644 --- a/src/site/pages/dashboard/index.vue +++ b/src/site/pages/dashboard/index.vue @@ -4,6 +4,8 @@ section.hero div.hero-body { align-items: baseline; } + + .albumsModal .columns .column { padding: .25rem; }