mirror of
https://github.com/zadam/trilium.git
synced 2025-11-16 10:15:52 +01:00
#98, sync setup now doesn't copy the whole DB file, but sets up minimal database and starts off sync
This commit is contained in:
@@ -2,14 +2,10 @@
|
||||
|
||||
const sqlInit = require('../../services/sql_init');
|
||||
const sql = require('../../services/sql');
|
||||
const cls = require('../../services/cls');
|
||||
const tmp = require('tmp-promise');
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const rp = require('request-promise');
|
||||
const Option = require('../../entities/option');
|
||||
const syncService = require('../../services/sync');
|
||||
const log = require('../../services/log');
|
||||
const DOCUMENT_PATH = require('../../services/data_dir').DOCUMENT_PATH;
|
||||
const sourceIdService = require('../../services/source_id');
|
||||
const url = require('url');
|
||||
|
||||
async function setupNewDocument(req) {
|
||||
const { username, password } = req.body;
|
||||
@@ -20,52 +16,44 @@ async function setupNewDocument(req) {
|
||||
async function setupSyncFromServer(req) {
|
||||
const { serverAddress, username, password } = req.body;
|
||||
|
||||
const tempFile = await tmp.file();
|
||||
try {
|
||||
log.info("Getting document options from sync server.");
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
const file = fs.createWriteStream(tempFile.path);
|
||||
const parsedAddress = url.parse(serverAddress);
|
||||
// response is expected to contain documentId and documentSecret options
|
||||
const options = await rp.get({
|
||||
uri: serverAddress + '/api/sync/document',
|
||||
auth: {
|
||||
'user': username,
|
||||
'pass': password
|
||||
},
|
||||
json: true
|
||||
});
|
||||
|
||||
const options = {
|
||||
method: 'GET',
|
||||
protocol: parsedAddress.protocol,
|
||||
host: parsedAddress.hostname,
|
||||
port: parsedAddress.port,
|
||||
path: '/api/sync/document',
|
||||
auth: username + ':' + password
|
||||
log.info("Creating database for sync");
|
||||
|
||||
await sql.transactional(async () => {
|
||||
await sqlInit.createDatabaseForSync(serverAddress);
|
||||
|
||||
for (const opt of options) {
|
||||
await new Option(opt).save();
|
||||
}
|
||||
});
|
||||
|
||||
log.info("Triggering sync.");
|
||||
|
||||
// it's ok to not wait for it here
|
||||
syncService.sync();
|
||||
|
||||
return { result: 'success' };
|
||||
}
|
||||
catch (e) {
|
||||
log.error("Sync failed: " + e.message);
|
||||
|
||||
return {
|
||||
result: 'failure',
|
||||
error: e.message
|
||||
};
|
||||
|
||||
log.info("Getting document from: " + serverAddress);
|
||||
|
||||
http.request(options, function(response) {
|
||||
response.pipe(file);
|
||||
|
||||
file.on('finish', function() {
|
||||
log.info("Document download finished, closing & renaming.");
|
||||
|
||||
file.close(() => { // close() is async, call after close completes.
|
||||
fs.rename(tempFile.path, DOCUMENT_PATH, async () => {
|
||||
cls.reset();
|
||||
|
||||
await sqlInit.initDbConnection();
|
||||
|
||||
// we need to generate new source ID for this instance, otherwise it will
|
||||
// match the original server one
|
||||
await sql.transactional(async () => {
|
||||
await sourceIdService.generateSourceId();
|
||||
});
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
});
|
||||
}).on('error', function(err) { // Handle errors
|
||||
fs.unlink(tempFile.path); // Delete the file async. (But we don't check the result)
|
||||
|
||||
reject(err.message);
|
||||
log.error(err.message);
|
||||
}).end();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@@ -7,7 +7,7 @@ const sql = require('../../services/sql');
|
||||
const optionService = require('../../services/options');
|
||||
const contentHashService = require('../../services/content_hash');
|
||||
const log = require('../../services/log');
|
||||
const DOCUMENT_PATH = require('../../services/data_dir').DOCUMENT_PATH;
|
||||
const repository = require('../../services/repository');
|
||||
|
||||
async function testSync() {
|
||||
try {
|
||||
@@ -23,6 +23,10 @@ async function testSync() {
|
||||
}
|
||||
}
|
||||
|
||||
async function getStats() {
|
||||
return syncService.stats;
|
||||
}
|
||||
|
||||
async function checkSync() {
|
||||
return {
|
||||
hashes: await contentHashService.getHashes(),
|
||||
@@ -75,7 +79,10 @@ async function getChanged(req) {
|
||||
|
||||
const syncs = await sql.getRows("SELECT * FROM sync WHERE id > ? LIMIT 1000", [lastSyncId]);
|
||||
|
||||
return await syncService.getSyncRecords(syncs);
|
||||
return {
|
||||
syncs: await syncService.getSyncRecords(syncs),
|
||||
maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync')
|
||||
};
|
||||
}
|
||||
|
||||
async function update(req) {
|
||||
@@ -87,10 +94,13 @@ async function update(req) {
|
||||
}
|
||||
}
|
||||
|
||||
async function getDocument(req, resp) {
|
||||
log.info("Serving document.");
|
||||
async function getDocument() {
|
||||
log.info("Serving document options.");
|
||||
|
||||
resp.sendFile(DOCUMENT_PATH);
|
||||
return [
|
||||
await repository.getOption('documentId'),
|
||||
await repository.getOption('documentSecret')
|
||||
];
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
@@ -102,5 +112,6 @@ module.exports = {
|
||||
forceNoteSync,
|
||||
getChanged,
|
||||
update,
|
||||
getDocument
|
||||
getDocument,
|
||||
getStats
|
||||
};
|
||||
Reference in New Issue
Block a user