diff options
| author | Pitu <[email protected]> | 2019-02-23 00:45:45 +0900 |
|---|---|---|
| committer | Pitu <[email protected]> | 2019-02-23 00:45:45 +0900 |
| commit | fc95cb7b0f047806937c25f0fc1104c72b0a32cb (patch) | |
| tree | fcf55a9e1f280dc34912a914e0cc47f69ce0b8c4 /src/api | |
| parent | Update Util.js (diff) | |
| download | host.fuwn.me-fc95cb7b0f047806937c25f0fc1104c72b0a32cb.tar.xz host.fuwn.me-fc95cb7b0f047806937c25f0fc1104c72b0a32cb.zip | |
Better DB handling and stuff
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/database/migrations/20190221225812_initialMigration.js | 10 | ||||
| -rw-r--r-- | src/api/database/seeds/initial.js | 1 | ||||
| -rw-r--r-- | src/api/routes/albums/link/linkPOST.js | 3 | ||||
| -rw-r--r-- | src/api/routes/auth/registerPOST.js | 4 | ||||
| -rw-r--r-- | src/api/routes/files/uploadPOST.js | 44 | ||||
| -rw-r--r-- | src/api/structures/Route.js | 3 | ||||
| -rw-r--r-- | src/api/utils/Util.js | 3 |
7 files changed, 46 insertions, 22 deletions
diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js index 455d5c5..f2192f6 100644 --- a/src/api/database/migrations/20190221225812_initialMigration.js +++ b/src/api/database/migrations/20190221225812_initialMigration.js @@ -3,8 +3,8 @@ exports.up = async knex => { table.increments(); table.string('username'); table.string('password'); - table.boolean('enabled').defaultTo(true); - table.boolean('isAdmin').defaultTo(false); + table.boolean('enabled'); + table.boolean('isAdmin'); table.string('apiKey'); table.timestamp('passwordEditedAt'); table.timestamp('apiKeyEditedAt'); @@ -39,9 +39,9 @@ exports.up = async knex => { table.integer('userId'); table.integer('albumId'); table.string('identifier'); - table.integer('views').defaultTo(0); - table.boolean('enabled').defaultTo(true); - table.boolean('enableDownload').defaultTo(true); + table.integer('views'); + table.boolean('enabled'); + table.boolean('enableDownload'); table.timestamp('expiresAt'); table.timestamp('createdAt'); table.timestamp('editedAt'); diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js index d4a343c..7445916 100644 --- a/src/api/database/seeds/initial.js +++ b/src/api/database/seeds/initial.js @@ -16,6 +16,7 @@ exports.seed = async db => { apiKeyEditedAt: now, createdAt: now, editedAt: now, + enabled: true, isAdmin: true }); console.log(); diff --git a/src/api/routes/albums/link/linkPOST.js b/src/api/routes/albums/link/linkPOST.js index 91e1521..968e57d 100644 --- a/src/api/routes/albums/link/linkPOST.js +++ b/src/api/routes/albums/link/linkPOST.js @@ -37,7 +37,8 @@ class linkPOST extends Route { albumId, enabled: true, enableDownload: true, - expiresAt: null + expiresAt: null, + views: 0 }); return res.json({ diff --git a/src/api/routes/auth/registerPOST.js b/src/api/routes/auth/registerPOST.js index 762eaf2..ee8e5ae 100644 --- a/src/api/routes/auth/registerPOST.js +++ b/src/api/routes/auth/registerPOST.js @@ -51,7 +51,9 @@ class registerPOST extends Route { apiKey: randomstring.generate(64), apiKeyEditedAt: now, createdAt: now, - editedAt: now + editedAt: now, + enabled: true, + isAdmin: false }); return res.json({ message: 'The account was created successfully' }); } diff --git a/src/api/routes/files/uploadPOST.js b/src/api/routes/files/uploadPOST.js index d6cb8b7..fdab035 100644 --- a/src/api/routes/files/uploadPOST.js +++ b/src/api/routes/files/uploadPOST.js @@ -118,19 +118,37 @@ class uploadPOST extends Route { store the details on the database. */ const now = moment.utc().toDate(); - let inserted = null; + let insertedId = null; try { - inserted = await db.table('files').insert({ - userId: user ? user.id : null, - name: upload.filename, - original: upload.originalname, - type: upload.mimetype || '', - size: upload.size, - hash, - ip: req.ip, - createdAt: now, - editedAt: now - }, 'id'); + /* + This is so fucking dumb + */ + if (process.env.DB_CLIENT === 'sqlite3') { + insertedId = await db.table('files').insert({ + userId: user ? user.id : null, + name: upload.filename, + original: upload.originalname, + type: upload.mimetype || '', + size: upload.size, + hash, + ip: req.ip, + createdAt: now, + editedAt: now + }); + } else { + insertedId = await db.table('files').insert({ + userId: user ? user.id : null, + name: upload.filename, + original: upload.originalname, + type: upload.mimetype || '', + size: upload.size, + hash, + ip: req.ip, + createdAt: now, + editedAt: now + }, 'id'); + } + /* TODO: Something funny here, I'm not sure since I don't use MySQL but I think the argument id on the insert function on top behaves differently on psql/mysql/sqlite. Needs testing. @@ -155,7 +173,7 @@ class uploadPOST extends Route { */ if (albumId) { try { - await db.table('albumsFiles').insert({ albumId, fileId: inserted[0] }); + await db.table('albumsFiles').insert({ albumId, fileId: insertedId[0] }); await db.table('albums').where('id', albumId).update('editedAt', now); } catch (error) { log.error('There was an error updating editedAt on an album'); diff --git a/src/api/structures/Route.js b/src/api/structures/Route.js index 5466147..60c8b06 100644 --- a/src/api/structures/Route.js +++ b/src/api/structures/Route.js @@ -8,7 +8,8 @@ const db = require('knex')({ password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, filename: nodePath.join(__dirname, '..', '..', '..', 'database.sqlite') - } + }, + useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false }); const moment = require('moment'); const log = require('../utils/Log'); diff --git a/src/api/utils/Util.js b/src/api/utils/Util.js index f99d8e0..26edf4b 100644 --- a/src/api/utils/Util.js +++ b/src/api/utils/Util.js @@ -10,7 +10,8 @@ const db = require('knex')({ password: process.env.DB_PASSWORD, database: process.env.DB_DATABASE, filename: path.join(__dirname, '..', '..', '..', 'database.sqlite') - } + }, + useNullAsDefault: process.env.DB_CLIENT === 'sqlite' ? true : false }); const moment = require('moment'); const log = require('../utils/Log'); |