aboutsummaryrefslogtreecommitdiff
path: root/database/db.js
blob: 8655f76a616744aa678f412c360d95745b523bfe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
let init = function(db){

	// Create the tables we need to store galleries and files
	db.schema.createTableIfNotExists('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');
	}).then(() => {});

	db.schema.createTableIfNotExists('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(() => {});

	db.schema.createTableIfNotExists('users', function (table) {
		table.increments();
		table.string('username');
		table.string('password');
		table.string('token');
		table.integer('enabled');
		table.integer('timestamp');
	}).then(() => {
		db.table('users').where({username: 'root'}).then((user) => {
			if(user.length > 0) return;

			require('bcrypt').hash('root', 10, function(err, hash) {
				if(err) console.error('Error generating password hash for root');

				db.table('users').insert({
					username: 'root',
					password: hash,
					token: require('randomstring').generate(64),
					timestamp: Math.floor(Date.now() / 1000)
				}).then(() => {});
			});
		});
	});
};

module.exports = init;