aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/files
diff options
context:
space:
mode:
authorPitu <[email protected]>2019-03-02 22:07:24 +0900
committerPitu <[email protected]>2019-03-02 22:07:24 +0900
commitce76a8ec7ba2084ab75fb901933b3d4d84505032 (patch)
tree8b883904e8e41c85ae0f1c7de74aebb6e050a831 /src/api/routes/files
parentFix token (diff)
downloadhost.fuwn.me-ce76a8ec7ba2084ab75fb901933b3d4d84505032.tar.xz
host.fuwn.me-ce76a8ec7ba2084ab75fb901933b3d4d84505032.zip
Be able to add a file to multiple albums
Diffstat (limited to 'src/api/routes/files')
-rw-r--r--src/api/routes/files/albumAddPOST.js26
-rw-r--r--src/api/routes/files/albumDelPOST.js27
-rw-r--r--src/api/routes/files/filesGET.js15
-rw-r--r--src/api/routes/files/uploadPOST.js4
4 files changed, 68 insertions, 4 deletions
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);
}