mirror of
https://github.com/zadam/trilium.git
synced 2025-11-17 02:30:42 +01:00
#98, working sync setup from server to desktop instance + refactoring of DB initialization
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
"use strict";
|
||||
|
||||
const migrationService = require('./migration');
|
||||
const sql = require('./sql');
|
||||
const sqlInit = require('./sql_init');
|
||||
const utils = require('./utils');
|
||||
const passwordEncryptionService = require('./password_encryption');
|
||||
const optionService = require('./options');
|
||||
|
||||
async function checkAuth(req, res, next) {
|
||||
if (!await sqlInit.isUserInitialized()) {
|
||||
if (!await sqlInit.isDbInitialized()) {
|
||||
res.redirect("setup");
|
||||
}
|
||||
else if (!req.session.loggedIn && !utils.isElectron()) {
|
||||
@@ -38,7 +39,7 @@ async function checkApiAuth(req, res, next) {
|
||||
}
|
||||
|
||||
async function checkAppNotInitialized(req, res, next) {
|
||||
if (await sqlInit.isUserInitialized()) {
|
||||
if (await sqlInit.isDbInitialized()) {
|
||||
res.status(400).send("App already initialized.");
|
||||
}
|
||||
else {
|
||||
@@ -57,10 +58,27 @@ async function checkSenderToken(req, res, next) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkBasicAuth(req, res, next) {
|
||||
const header = req.headers.authorization || '';
|
||||
const token = header.split(/\s+/).pop() || '';
|
||||
const auth = new Buffer.from(token, 'base64').toString();
|
||||
const [username, password] = auth.split(/:/);
|
||||
|
||||
const dbUsername = await optionService.getOption('username');
|
||||
|
||||
if (dbUsername !== username || !await passwordEncryptionService.verifyPassword(password)) {
|
||||
res.status(401).send("Not authorized");
|
||||
}
|
||||
else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkAuth,
|
||||
checkApiAuth,
|
||||
checkAppNotInitialized,
|
||||
checkApiAuthOrElectron,
|
||||
checkSenderToken
|
||||
checkSenderToken,
|
||||
checkBasicAuth
|
||||
};
|
||||
@@ -13,9 +13,14 @@ function getSourceId() {
|
||||
return namespace.get('sourceId');
|
||||
}
|
||||
|
||||
function reset() {
|
||||
clsHooked.reset();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init,
|
||||
wrap,
|
||||
namespace,
|
||||
getSourceId
|
||||
getSourceId,
|
||||
reset
|
||||
};
|
||||
@@ -86,7 +86,7 @@ async function migrate() {
|
||||
}
|
||||
|
||||
if (sqlInit.isDbUpToDate()) {
|
||||
sqlInit.setDbReadyAsResolved();
|
||||
await sqlInit.initDbConnection();
|
||||
}
|
||||
|
||||
return migrations;
|
||||
|
||||
@@ -157,10 +157,10 @@ async function transactional(func) {
|
||||
transactionActive = true;
|
||||
transactionPromise = new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
cls.namespace.set('isInTransaction', true);
|
||||
|
||||
await beginTransaction();
|
||||
|
||||
cls.namespace.set('isInTransaction', true);
|
||||
|
||||
ret = await func();
|
||||
|
||||
await commit();
|
||||
|
||||
@@ -11,38 +11,32 @@ async function createConnection() {
|
||||
return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise});
|
||||
}
|
||||
|
||||
let schemaReadyResolve = null;
|
||||
const schemaReady = new Promise((resolve, reject) => schemaReadyResolve = resolve);
|
||||
|
||||
let dbReadyResolve = null;
|
||||
const dbReady = new Promise((resolve, reject) => {
|
||||
cls.init(async () => {
|
||||
dbReadyResolve = resolve;
|
||||
|
||||
initDbConnection();
|
||||
});
|
||||
|
||||
async function isDbInitialized() {
|
||||
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
|
||||
|
||||
return tableResults.length === 1;
|
||||
}
|
||||
|
||||
async function initDbConnection() {
|
||||
await cls.init(async () => {
|
||||
const db = await createConnection();
|
||||
sql.setDbConnection(db);
|
||||
|
||||
await sql.execute("PRAGMA foreign_keys = ON");
|
||||
|
||||
dbReadyResolve = () => {
|
||||
log.info("DB ready.");
|
||||
|
||||
resolve(db);
|
||||
};
|
||||
|
||||
const tableResults = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'");
|
||||
if (tableResults.length !== 1) {
|
||||
if (isDbInitialized()) {
|
||||
log.info("DB not found, please visit setup page to initialize Trilium.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
schemaReadyResolve();
|
||||
|
||||
if (!await isUserInitialized()) {
|
||||
log.info("Login/password not initialized. DB not ready.");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await isDbUpToDate()) {
|
||||
// avoiding circular dependency
|
||||
const migrationService = require('./migration');
|
||||
@@ -50,9 +44,10 @@ const dbReady = new Promise((resolve, reject) => {
|
||||
await migrationService.migrate();
|
||||
}
|
||||
|
||||
resolve(db);
|
||||
log.info("DB ready.");
|
||||
dbReadyResolve(db);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function createInitialDatabase(username, password) {
|
||||
log.info("Connected to db, but schema doesn't exist. Initializing schema ...");
|
||||
@@ -78,11 +73,7 @@ async function createInitialDatabase(username, password) {
|
||||
|
||||
log.info("Schema and initial content generated. Waiting for user to enter username/password to finish setup.");
|
||||
|
||||
setDbReadyAsResolved();
|
||||
}
|
||||
|
||||
function setDbReadyAsResolved() {
|
||||
dbReadyResolve();
|
||||
await initDbConnection();
|
||||
}
|
||||
|
||||
async function isDbUpToDate() {
|
||||
@@ -97,23 +88,10 @@ async function isDbUpToDate() {
|
||||
return upToDate;
|
||||
}
|
||||
|
||||
async function isUserInitialized() {
|
||||
const optionsTable = await sql.getRows("SELECT name FROM sqlite_master WHERE type='table' AND name='options'");
|
||||
|
||||
if (optionsTable.length !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const username = await sql.getValue("SELECT value FROM options WHERE name = 'username'");
|
||||
|
||||
return !!username;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
dbReady,
|
||||
schemaReady,
|
||||
isUserInitialized,
|
||||
setDbReadyAsResolved,
|
||||
isDbInitialized,
|
||||
initDbConnection,
|
||||
isDbUpToDate,
|
||||
createInitialDatabase
|
||||
};
|
||||
Reference in New Issue
Block a user