filesafe/database/db.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

const logger = require('./../logger')
const perms = require('./../controllers/permissionController')
2019-08-26 17:46:56 +00:00
const randomstring = require('randomstring')
2018-10-09 19:52:41 +00:00
const init = function (db) {
// Create the tables we need to store galleries and files
db.schema.hasTable('albums').then(exists => {
if (exists) 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')
}).then(() => {})
})
2017-01-14 08:50:18 +00:00
db.schema.hasTable('files').then(exists => {
if (exists) 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')
}).then(() => {})
})
2017-01-14 08:50:18 +00:00
db.schema.hasTable('users').then(exists => {
if (exists) 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('fileLength')
table.integer('permission')
}).then(() => {
db.table('users').where({ username: 'root' }).then((user) => {
if (user.length > 0) return
require('bcrypt').hash('root', 10, function (error, hash) {
if (error) logger.error('Error generating password hash for root')
db.table('users').insert({
username: 'root',
password: hash,
2019-08-26 17:46:56 +00:00
token: randomstring.generate(64),
timestamp: Math.floor(Date.now() / 1000),
permission: perms.permissions.superadmin
}).then(() => {})
})
})
})
})
2017-01-16 07:45:29 +00:00
}
module.exports = init