aboutsummaryrefslogtreecommitdiff
path: root/src/api/routes/files
diff options
context:
space:
mode:
authorPitu <[email protected]>2020-05-10 20:02:48 +0900
committerPitu <[email protected]>2020-05-10 20:02:48 +0900
commit4c52932426a3e91a205940a6ab08bfee3e23fadf (patch)
tree5970a1b1402ec1b205248389a8f09cc6649239cc /src/api/routes/files
parentFeature: Migration script from v3 to v4 (diff)
downloadhost.fuwn.me-4c52932426a3e91a205940a6ab08bfee3e23fadf.tar.xz
host.fuwn.me-4c52932426a3e91a205940a6ab08bfee3e23fadf.zip
Features:
* Serve files during development * Own endpoint for fetching the albums of a file
Diffstat (limited to 'src/api/routes/files')
-rw-r--r--src/api/routes/files/filesAlbumsGET.js31
-rw-r--r--src/api/routes/files/filesGET.js23
2 files changed, 33 insertions, 21 deletions
diff --git a/src/api/routes/files/filesAlbumsGET.js b/src/api/routes/files/filesAlbumsGET.js
new file mode 100644
index 0000000..c834658
--- /dev/null
+++ b/src/api/routes/files/filesAlbumsGET.js
@@ -0,0 +1,31 @@
+const Route = require('../../structures/Route');
+
+class filesGET extends Route {
+ constructor() {
+ super('/file/:id/albums', 'get');
+ }
+
+ async run(req, res, db, user) {
+ const { id } = req.params;
+ if (!id) return res.status(400).json({ message: 'Invalid file ID supplied' });
+
+ let albums = [];
+ let albumFiles = await db.table('albumsFiles')
+ .where('fileId', id)
+ .select('albumId');
+
+ if (albumFiles.length) {
+ albumFiles = albumFiles.map(a => a.albumId);
+ albums = await db.table('albums')
+ .whereIn('id', albumFiles)
+ .select('id', 'name');
+ }
+
+ return res.json({
+ message: 'Successfully retrieved file albums',
+ albums
+ });
+ }
+}
+
+module.exports = filesGET;
diff --git a/src/api/routes/files/filesGET.js b/src/api/routes/files/filesGET.js
index ce288ff..f1a3a26 100644
--- a/src/api/routes/files/filesGET.js
+++ b/src/api/routes/files/filesGET.js
@@ -7,31 +7,12 @@ class filesGET extends Route {
}
async run(req, res, db, user) {
- /*
- Get all the files from the user
- */
+ // Get all the files from the user
const files = await db.table('files')
.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
- */
+ // For each file, create the public link to be able to display the file
for (let file of files) {
file = Util.constructFilePublicLink(file);
}