2022-08-09 10:28:21 +00:00
|
|
|
const logger = require('./../../logger')
|
|
|
|
|
|
|
|
// Default root user's credentials
|
|
|
|
// Root user will only be created if "users" table is empty
|
|
|
|
const DEFAULT_ROOT_USERNAME = 'root'
|
|
|
|
const DEFAULT_ROOT_PASSWORD = 'changeme'
|
|
|
|
|
2022-06-22 06:53:01 +00:00
|
|
|
const initDatabase = async db => {
|
2018-01-23 20:06:30 +00:00
|
|
|
// Create the tables we need to store galleries and files
|
2019-10-21 10:49:52 +00:00
|
|
|
await db.schema.hasTable('albums').then(exists => {
|
2020-11-10 15:56:18 +00:00
|
|
|
if (!exists) {
|
2019-10-21 10:49:52 +00:00
|
|
|
return db.schema.createTable('albums', function (table) {
|
|
|
|
table.increments()
|
|
|
|
table.integer('userid')
|
|
|
|
table.string('name')
|
|
|
|
table.string('identifier')
|
|
|
|
table.integer('enabled')
|
|
|
|
table.integer('timestamp')
|
|
|
|
table.integer('editedAt')
|
|
|
|
table.integer('zipGeneratedAt')
|
|
|
|
table.integer('download')
|
|
|
|
table.integer('public')
|
|
|
|
table.string('description')
|
|
|
|
})
|
2020-11-10 15:56:18 +00:00
|
|
|
}
|
2018-03-14 06:57:09 +00:00
|
|
|
})
|
2017-01-14 08:50:18 +00:00
|
|
|
|
2019-10-21 10:49:52 +00:00
|
|
|
await db.schema.hasTable('files').then(exists => {
|
2020-11-10 15:56:18 +00:00
|
|
|
if (!exists) {
|
2019-10-21 10:49:52 +00:00
|
|
|
return db.schema.createTable('files', function (table) {
|
|
|
|
table.increments()
|
|
|
|
table.integer('userid')
|
|
|
|
table.string('name')
|
|
|
|
table.string('original')
|
|
|
|
table.string('type')
|
|
|
|
table.string('size')
|
|
|
|
table.string('hash')
|
|
|
|
table.string('ip')
|
|
|
|
table.integer('albumid')
|
|
|
|
table.integer('timestamp')
|
|
|
|
table.integer('expirydate')
|
|
|
|
})
|
2020-11-10 15:56:18 +00:00
|
|
|
}
|
2018-03-14 06:57:09 +00:00
|
|
|
})
|
2017-01-14 08:50:18 +00:00
|
|
|
|
2019-10-21 10:49:52 +00:00
|
|
|
await db.schema.hasTable('users').then(exists => {
|
2020-11-10 15:56:18 +00:00
|
|
|
if (!exists) {
|
2019-10-21 10:49:52 +00:00
|
|
|
return db.schema.createTable('users', function (table) {
|
|
|
|
table.increments()
|
|
|
|
table.string('username')
|
|
|
|
table.string('password')
|
|
|
|
table.string('token')
|
|
|
|
table.integer('enabled')
|
|
|
|
table.integer('timestamp')
|
|
|
|
table.integer('permission')
|
2020-05-16 15:32:32 +00:00
|
|
|
table.integer('registration')
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2020-11-10 15:56:18 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2019-10-21 10:49:52 +00:00
|
|
|
|
2022-08-09 10:28:21 +00:00
|
|
|
const usersCount = await db.table('users')
|
|
|
|
.count('id as count')
|
|
|
|
.then(rows => rows[0].count)
|
2019-10-21 10:49:52 +00:00
|
|
|
|
2022-08-09 10:28:21 +00:00
|
|
|
if (usersCount === 0) {
|
|
|
|
const hash = await require('bcrypt').hash(DEFAULT_ROOT_PASSWORD, 10)
|
2020-05-16 15:32:32 +00:00
|
|
|
const timestamp = Math.floor(Date.now() / 1000)
|
2022-08-09 10:28:21 +00:00
|
|
|
await db.table('users')
|
|
|
|
.insert({
|
|
|
|
username: DEFAULT_ROOT_USERNAME,
|
|
|
|
password: hash,
|
|
|
|
token: require('randomstring').generate(64),
|
|
|
|
timestamp,
|
|
|
|
permission: require('./../permissionController').permissions.superadmin,
|
|
|
|
registration: timestamp
|
|
|
|
})
|
|
|
|
logger.log(`Created user "${DEFAULT_ROOT_USERNAME}" with password "${DEFAULT_ROOT_PASSWORD}".`)
|
2019-10-21 10:49:52 +00:00
|
|
|
}
|
2017-01-16 07:45:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 06:53:01 +00:00
|
|
|
module.exports = initDatabase
|