diff --git a/README.md b/README.md index d00c8e3..d1af026 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,10 @@ Configuration file of lolisafe, `config.js`, is also NOT fully compatible with t 5. Run `yarn install --production` to install all production dependencies (Yes, use [yarn](https://yarnpkg.com)). 6. Run `yarn start` to start the service. +> Default admin account: +> Username: `root` +> Password: `changeme` + You can also start it with `yarn pm2` if you have [PM2](https://pm2.keymetrics.io/). When running in production mode, the safe will use pre-built client-side CSS/JS files from `dist` directory, while the actual source codes are in `src` directory. diff --git a/TODO.md b/TODO.md index 6b31549..59ff6c7 100644 --- a/TODO.md +++ b/TODO.md @@ -23,7 +23,7 @@ Normal priority: * [x] Update fb_share.png. * [x] I forsaked all `Promise.all()` in favor of `await-in-for-loop` a while back. I personally think it was fine, considering a lot of them were tasks that required serial processing (continuation be dependant on previous iterations), but maybe I should review the current codes to find any sections that would do just fine, or maybe even great, with `Promise.all()`. * [x] Black-ish colorscheme. -* [ ] Colorscheme options. Bring back the old dark grey colorscheme, and also add lolisafe's stock light colorscheme. +* [ ] Colorschemes option. Bring back the old dark grey colorscheme, and also add lolisafe's stock light colorscheme. I think it may be fair to load the colorscheming JS file before `style.css` gets loaded. Meaning the colorscheme script in particular needs to be in HEAD tag, as opposed to the standard where we put all JS files at the end of BODY tag. Once this is implemented, default colorscheme should be the old dark grey. * [ ] Turn `render.js` into a standalone script (don't use `page` window variable). diff --git a/database/db.js b/database/db.js index 9297424..391c52d 100644 --- a/database/db.js +++ b/database/db.js @@ -1,69 +1,66 @@ -const randomstring = require('randomstring') -const perms = require('./../controllers/permissionController') -const logger = require('./../logger') - -const init = function (db) { +const init = async 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(() => {}) - }) - - 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') - table.integer('expirydate') - }).then(() => {}) - }) - - 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('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, - token: randomstring.generate(64), - timestamp: Math.floor(Date.now() / 1000), - permission: perms.permissions.superadmin - }).then(() => {}) - }) + await 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') }) - }) }) + + await 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') + table.integer('expirydate') + }) + }) + + await 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('permission') + }) + }) + + const root = await db.table('users') + .where('username', 'root') + .first() + + if (!root) { + const hash = await require('bcrypt').hash('changeme', 10) + await db.table('users').insert({ + username: 'root', + password: hash, + token: require('randomstring').generate(64), + timestamp: Math.floor(Date.now() / 1000), + permission: require('./../controllers/permissionController').permissions.superadmin + }) + } } module.exports = init diff --git a/lolisafe.js b/lolisafe.js index 0f758cd..947f6fc 100644 --- a/lolisafe.js +++ b/lolisafe.js @@ -26,7 +26,6 @@ const api = require('./routes/api') const nojs = require('./routes/nojs') const db = require('knex')(config.database) -require('./database/db.js')(db) safe.use(helmet()) if (config.trustProxy) safe.set('trust proxy', 1) @@ -106,6 +105,9 @@ safe.use('/api', api) ;(async () => { try { + // Init database + await require('./database/db.js')(db) + // Verify paths, create missing ones, clean up temp ones await paths.init()