diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/databaseMigration.js | 2 | ||||
| -rw-r--r-- | src/api/routes/admin/userGET.js | 22 | ||||
| -rw-r--r-- | src/api/routes/albums/albumGET.js | 26 | ||||
| -rw-r--r-- | src/api/structures/Server.js | 25 |
4 files changed, 50 insertions, 25 deletions
diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js index 24cfcff..1e3518e 100644 --- a/src/api/databaseMigration.js +++ b/src/api/databaseMigration.js @@ -58,7 +58,7 @@ const start = async () => { id: user.id, username: user.username, password: user.password, - enabled: user.enabled == 1, + enabled: user.enabled, isAdmin: false, apiKey: user.token, passwordEditedAt: now, diff --git a/src/api/routes/admin/userGET.js b/src/api/routes/admin/userGET.js index 48c6e9b..430dfd7 100644 --- a/src/api/routes/admin/userGET.js +++ b/src/api/routes/admin/userGET.js @@ -15,10 +15,27 @@ class usersGET extends Route { .select('id', 'username', 'enabled', 'createdAt', 'editedAt', 'apiKeyEditedAt', 'isAdmin') .where({ id }) .first(); - const files = await db.table('files') + + let count = 0; + let files = db.table('files') .where({ userId: user.id }) .orderBy('id', '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 (let file of files) { file = Util.constructFilePublicLink(file); } @@ -26,7 +43,8 @@ class usersGET extends Route { return res.json({ message: 'Successfully retrieved user', user, - files + files, + count }); } catch (error) { return super.error(res, error); diff --git a/src/api/routes/albums/albumGET.js b/src/api/routes/albums/albumGET.js index c9f6763..4ac7089 100644 --- a/src/api/routes/albums/albumGET.js +++ b/src/api/routes/albums/albumGET.js @@ -18,14 +18,31 @@ class albumGET extends Route { const album = await db.table('albums').where('id', link.albumId).first(); if (!album) return res.status(404).json({ message: 'Album not found' }); - const files = await db.table('albumsFiles') + let count = 0; + let files = db.table('albumsFiles') .where({ albumId: link.albumId }) .join('files', 'albumsFiles.fileId', 'files.id') .select('files.name', 'files.id') .orderBy('files.id', 'desc'); - // Create the links for each file - // eslint-disable-next-line no-restricted-syntax + const { page, limit = 50 } = req.query; + if (page && page >= 0) { + files = await files.offset((page - 1) * limit).limit(limit); + + const dbRes = await db.table('albumsFiles') + .where({ albumId: link.albumId }) + .join('files', 'albumsFiles.fileId', 'files.id') + .select('files.name', 'files.id') + .orderBy('files.id', 'desc') + .count('* as count') + .first(); + + count = dbRes.count; + } else { + files = await files; // execute the query + count = files.length; + } + for (let file of files) { file = Util.constructFilePublicLink(file); } @@ -38,7 +55,8 @@ class albumGET extends Route { name: album.name, downloadEnabled: link.enableDownload, isNsfw: album.nsfw, - files + files, + count }); } } diff --git a/src/api/structures/Server.js b/src/api/structures/Server.js index 446c621..268ba68 100644 --- a/src/api/structures/Server.js +++ b/src/api/structures/Server.js @@ -5,6 +5,7 @@ if (!process.env.SERVER_PORT) { process.exit(0); } +const { loadNuxt, build } = require('nuxt'); const express = require('express'); const helmet = require('helmet'); const cors = require('cors'); @@ -80,25 +81,13 @@ class Server { }); } - serveNuxt() { - // Serve the frontend if we are in production mode - if (process.env.NODE_ENV === 'production') { - this.server.use(express.static(path.join(__dirname, '../../../dist'))); + async serveNuxt() { + const isProd = process.env.NODE_ENV === 'production'; + const nuxt = await loadNuxt(isProd ? 'start' : 'dev'); + this.server.use(nuxt.render); + if (!isProd) { + build(nuxt); } - - /* - For vue router to work with express we need this fallback. - After all the routes are loaded and the static files handled and if the - user is trying to access a non-mapped route we serve the website instead - since it has routes of it's own that don't work if accessed directly - */ - this.server.all('*', (_req, res) => { - try { - res.sendFile(path.join(__dirname, '../../../dist/index.html')); - } catch (error) { - res.json({ success: false, message: 'Something went wrong' }); - } - }); } createJobs() { |