aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/files
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/files
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/files')
-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
8 files changed, 166 insertions, 18 deletions
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;