You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2026-05-22 09:05:38 +02:00
Refactored handling of multiple sync targets
This commit is contained in:
@@ -70,7 +70,7 @@ class SideMenuContentComponent extends Component {
|
||||
}
|
||||
|
||||
async synchronize_press() {
|
||||
const sync = await reg.synchronizer()
|
||||
const sync = await reg.synchronizer(Setting.value('sync.target'))
|
||||
|
||||
if (this.props.syncStarted) {
|
||||
sync.cancel();
|
||||
|
||||
@@ -185,12 +185,10 @@ class FileApiDriverOneDrive {
|
||||
};
|
||||
|
||||
let context = options ? options.context : null;
|
||||
|
||||
let url = null;
|
||||
let url = context ? context.nextLink : null;
|
||||
let query = null;
|
||||
if (context) {
|
||||
url = context.nextLink;
|
||||
} else {
|
||||
|
||||
if (!url) {
|
||||
url = this.makePath_(path) + ':/delta';
|
||||
query = this.itemFilter_();
|
||||
query.select += ',deleted';
|
||||
|
||||
@@ -166,7 +166,11 @@ Setting.defaults_ = {
|
||||
'activeFolderId': { value: '', type: 'string', public: false },
|
||||
'sync.onedrive.auth': { value: '', type: 'string', public: false },
|
||||
'sync.filesystem.path': { value: '', type: 'string', public: true, appTypes: ['cli'] },
|
||||
'sync.target': { value: 'onedrive', type: 'string', public: true, label: () => _('Synchronisation target') },
|
||||
'sync.target': { value: 'onedrive', type: 'enum', public: true, label: () => _('Synchronisation target'), options: () => ({
|
||||
1: 'Memory',
|
||||
2: _('File system'),
|
||||
3: _('OneDrive'),
|
||||
})},
|
||||
'sync.context': { value: '', type: 'string', public: false },
|
||||
'editor': { value: '', type: 'string', public: true, appTypes: ['cli'] },
|
||||
'locale': { value: 'en_GB', type: 'string', public: true },
|
||||
|
||||
@@ -127,6 +127,8 @@ class OneDriveApi {
|
||||
}
|
||||
|
||||
async exec(method, path, query = null, data = null, options = null) {
|
||||
if (!path) throw new Error('Path is required');
|
||||
|
||||
method = method.toUpperCase();
|
||||
|
||||
if (!options) options = {};
|
||||
|
||||
@@ -3,8 +3,11 @@ import { Setting } from 'lib/models/setting.js';
|
||||
import { OneDriveApi } from 'lib/onedrive-api.js';
|
||||
import { parameters } from 'lib/parameters.js';
|
||||
import { FileApi } from 'lib/file-api.js';
|
||||
import { Database } from 'lib/database.js';
|
||||
import { Synchronizer } from 'lib/synchronizer.js';
|
||||
import { FileApiDriverOneDrive } from 'lib/file-api-driver-onedrive.js';
|
||||
import { shim } from 'lib/shim.js';
|
||||
import { FileApiDriverMemory } from 'lib/file-api-driver-memory.js';
|
||||
|
||||
const reg = {};
|
||||
|
||||
@@ -50,28 +53,46 @@ reg.oneDriveApi = () => {
|
||||
return reg.oneDriveApi_;
|
||||
}
|
||||
|
||||
reg.fileApi = async () => {
|
||||
if (reg.fileApi_) return reg.fileApi_;
|
||||
|
||||
let driver = new FileApiDriverOneDrive(reg.oneDriveApi());
|
||||
let appDir = await reg.oneDriveApi().appDirectory();
|
||||
|
||||
reg.fileApi_ = new FileApi(appDir, driver);
|
||||
reg.fileApi_.setLogger(reg.logger());
|
||||
|
||||
return reg.fileApi_;
|
||||
}
|
||||
|
||||
reg.synchronizer = async () => {
|
||||
if (reg.synchronizer_) return reg.synchronizer_;
|
||||
reg.synchronizer = async (syncTargetId) => {
|
||||
if (!reg.synchronizers_) reg.synchronizers_ = [];
|
||||
if (reg.synchronizers_[syncTargetId]) return reg.synchronizers_[syncTargetId];
|
||||
|
||||
if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized');
|
||||
|
||||
let fileApi = await reg.fileApi();
|
||||
reg.synchronizer_ = new Synchronizer(reg.db(), fileApi, Setting.value('appType'));
|
||||
reg.synchronizer_.setLogger(reg.logger());
|
||||
reg.synchronizer_.dispatch = reg.dispatch;
|
||||
return reg.synchronizer_;
|
||||
let fileApi = null;
|
||||
|
||||
if (syncTargetId == 'onedrive') {
|
||||
|
||||
if (!reg.oneDriveApi().auth()) throw new Error('User is not authentified');
|
||||
let appDir = await reg.oneDriveApi().appDirectory();
|
||||
fileApi = new FileApi(appDir, new FileApiDriverOneDrive(reg.oneDriveApi()));
|
||||
|
||||
} else if (syncTargetId == 'memory') {
|
||||
|
||||
fileApi = new FileApi('joplin', new FileApiDriverMemory());
|
||||
|
||||
} else if (syncTargetId == 'filesystem') {
|
||||
|
||||
let syncDir = Setting.value('sync.filesystem.path');
|
||||
if (!syncDir) throw new Error(_('Please set the "sync.filesystem.path" config value to the desired synchronisation destination.'));
|
||||
await shim.fs.mkdirp(syncDir, 0o755);
|
||||
fileApi = new FileApi(syncDir, new shim.FileApiDriverLocal());
|
||||
|
||||
} else {
|
||||
|
||||
throw new Error('Unknown sync target: ' + syncTargetId);
|
||||
|
||||
}
|
||||
|
||||
fileApi.setLogger(reg.logger());
|
||||
|
||||
let sync = new Synchronizer(reg.db(), fileApi, Setting.value('appType'));
|
||||
sync.setLogger(reg.logger());
|
||||
sync.dispatch = reg.dispatch;
|
||||
|
||||
reg.synchronizers_[syncTargetId] = sync;
|
||||
|
||||
return sync;
|
||||
}
|
||||
|
||||
reg.scheduleSync = async (delay = null) => {
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import fs from 'fs-extra';
|
||||
import { shim } from 'lib/shim.js';
|
||||
import { GeolocationNode } from 'lib/geolocation-node.js';
|
||||
import { FileApiDriverLocal } from 'lib/file-api-driver-local.js';
|
||||
|
||||
|
||||
function shimInit() {
|
||||
shim.fs = fs;
|
||||
shim.FileApiDriverLocal = FileApiDriverLocal;
|
||||
shim.Geolocation = GeolocationNode;
|
||||
|
||||
shim.fetch = require('node-fetch');
|
||||
shim.FormData = require('form-data');
|
||||
|
||||
shim.fetchBlob = async function(url, options) {
|
||||
if (!options || !options.path) throw new Error('fetchBlob: target file path is missing');
|
||||
if (!options.method) options.method = 'GET';
|
||||
|
||||
@@ -11,15 +11,7 @@ shim.isReactNative = () => {
|
||||
|
||||
shim.fetch = typeof fetch !== 'undefined' ? fetch : null;
|
||||
shim.FormData = typeof FormData !== 'undefined' ? FormData : null;
|
||||
|
||||
if (!shim.fetch) {
|
||||
let moduleName = 'node-fetch';
|
||||
shim.fetch = require(moduleName);
|
||||
}
|
||||
|
||||
if (!shim.FormData) {
|
||||
let moduleName = 'form-data';
|
||||
shim.FormData = require(moduleName);
|
||||
}
|
||||
shim.fs = null;
|
||||
shim.FileApiDriverLocal = null;
|
||||
|
||||
export { shim };
|
||||
Reference in New Issue
Block a user