aboutsummaryrefslogtreecommitdiff
path: root/src/api/structures/Database.js
blob: dc26afeecab316ce27977aa442d49e926a22c5b7 (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const log = require('../utils/Log');
const { server } = require('../../../config');
const db = require('knex')(server.database);
const bcrypt = require('bcrypt');
const moment = require('moment');
const randomstring = require('randomstring');

class Database {
	constructor() {
		this.createTables();
	}

	async createTables() {
		if (!await db.schema.hasTable('users')) {
			await db.schema.createTable('users', table => {
				table.increments();
				table.string('username');
				table.string('password');
				table.boolean('enabled').defaultTo(true);
				table.boolean('isAdmin').defaultTo(false);
				table.string('apiKey');
				table.timestamp('passwordEditedAt');
				table.timestamp('apiKeyEditedAt');
				table.timestamp('createdAt');
				table.timestamp('editedAt');
			});
		}

		if (!await db.schema.hasTable('albums')) {
			await db.schema.createTable('albums', table => {
				table.increments();
				table.integer('userId');
				table.string('name');
				// table.string('identifier');
				// table.boolean('enabled');
				// table.boolean('enableDownload').defaultTo(true);
				table.timestamp('createdAt');
				table.timestamp('editedAt');
			});
		}

		if (!await db.schema.hasTable('files')) {
			await db.schema.createTable('files', table => {
				table.increments();
				table.integer('userId');
				table.string('name');
				table.string('original');
				table.string('type');
				table.integer('size');
				table.string('hash');
				table.string('ip');
				table.timestamp('createdAt');
				table.timestamp('editedAt');
			});
		}

		if (!await db.schema.hasTable('links')) {
			await db.schema.createTable('links', table => {
				table.increments();
				table.integer('albumId');
				table.string('identifier');
				table.integer('views').defaultTo(0);
				table.boolean('enabled').defaultTo(true);
				table.boolean('enableDownload').defaultTo(true);
				table.timestamp('expiresAt');
				table.timestamp('createdAt');
				table.timestamp('editedAt');
			});
		}

		if (!await db.schema.hasTable('albumsFiles')) {
			await db.schema.createTable('albumsFiles', table => {
				table.increments();
				table.integer('albumId');
				table.integer('fileId');
			});
		}

		if (!await db.schema.hasTable('albumsLinks')) {
			await db.schema.createTable('albumsLinks', table => {
				table.increments();
				table.integer('albumId');
				table.integer('linkId');
			});
		}

		const now = moment.utc().toDate();
		const user = await db.table('users').where({ username: 'root' }).first();
		if (user) return;
		try {
			const hash = await bcrypt.hash('root', 10);
			await db.table('users').insert({
				username: 'root',
				password: hash,
				apiKey: randomstring.generate(64),
				passwordEditedAt: now,
				apiKeyEditedAt: now,
				createdAt: now,
				editedAt: now,
				isAdmin: true
			});
			log.success('Successfully created the root user with password "root". Make sure to log in and change it!');
		} catch (error) {
			log.error(error);
			if (error) log.error('Error generating password hash for root');
		}
	}
}

module.exports = Database;