2020-12-28 13:48:47 +02:00
|
|
|
import FileApiDriverJoplinServer from './file-api-driver-joplinServer';
|
|
|
|
import Setting from './models/Setting';
|
|
|
|
import Synchronizer from './Synchronizer';
|
|
|
|
import { _ } from './locale.js';
|
2021-01-29 20:45:11 +02:00
|
|
|
import JoplinServerApi from './JoplinServerApi';
|
|
|
|
import BaseSyncTarget from './BaseSyncTarget';
|
|
|
|
import { FileApi } from './file-api';
|
2021-06-03 17:12:07 +02:00
|
|
|
import Logger from './Logger';
|
2020-12-28 13:48:47 +02:00
|
|
|
|
2021-10-24 15:00:44 +02:00
|
|
|
const staticLogger = Logger.create('SyncTargetJoplinServer');
|
|
|
|
|
2020-12-28 13:48:47 +02:00
|
|
|
interface FileApiOptions {
|
|
|
|
path(): string;
|
2021-06-06 19:14:12 +02:00
|
|
|
userContentPath(): string;
|
2020-12-28 13:48:47 +02:00
|
|
|
username(): string;
|
|
|
|
password(): string;
|
|
|
|
}
|
|
|
|
|
2021-06-03 17:12:07 +02:00
|
|
|
export async function newFileApi(id: number, options: FileApiOptions) {
|
|
|
|
const apiOptions = {
|
|
|
|
baseUrl: () => options.path(),
|
2021-06-06 19:14:12 +02:00
|
|
|
userContentBaseUrl: () => options.userContentPath(),
|
2021-06-03 17:12:07 +02:00
|
|
|
username: () => options.username(),
|
|
|
|
password: () => options.password(),
|
|
|
|
env: Setting.value('env'),
|
|
|
|
};
|
|
|
|
|
|
|
|
const api = new JoplinServerApi(apiOptions);
|
|
|
|
const driver = new FileApiDriverJoplinServer(api);
|
|
|
|
const fileApi = new FileApi('', driver);
|
|
|
|
fileApi.setSyncTargetId(id);
|
|
|
|
await fileApi.initialize();
|
|
|
|
return fileApi;
|
|
|
|
}
|
|
|
|
|
2021-06-08 01:34:33 +02:00
|
|
|
export async function initFileApi(syncTargetId: number, logger: Logger, options: FileApiOptions) {
|
|
|
|
const fileApi = await newFileApi(syncTargetId, options);
|
2021-06-03 17:12:07 +02:00
|
|
|
fileApi.setLogger(logger);
|
|
|
|
return fileApi;
|
|
|
|
}
|
|
|
|
|
2020-12-28 13:48:47 +02:00
|
|
|
export default class SyncTargetJoplinServer extends BaseSyncTarget {
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public static id() {
|
2020-12-28 13:48:47 +02:00
|
|
|
return 9;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public static supportsConfigCheck() {
|
2020-12-28 13:48:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public static targetName() {
|
2020-12-28 13:48:47 +02:00
|
|
|
return 'joplinServer';
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static description() {
|
|
|
|
return 'Besides synchronisation and improved performances, Joplin Server also gives access to Joplin-specific sharing features.';
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public static label() {
|
2021-06-02 09:56:24 +02:00
|
|
|
return `${_('Joplin Server')} (Beta)`;
|
2020-12-28 13:48:47 +02:00
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public async isAuthenticated() {
|
2020-12-28 13:48:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public async fileApi(): Promise<FileApi> {
|
|
|
|
return super.fileApi();
|
|
|
|
}
|
|
|
|
|
2021-06-08 01:34:33 +02:00
|
|
|
public static async checkConfig(options: FileApiOptions, syncTargetId: number = null) {
|
2020-12-28 13:48:47 +02:00
|
|
|
const output = {
|
|
|
|
ok: false,
|
|
|
|
errorMessage: '',
|
|
|
|
};
|
|
|
|
|
2021-06-08 01:34:33 +02:00
|
|
|
syncTargetId = syncTargetId === null ? SyncTargetJoplinServer.id() : syncTargetId;
|
|
|
|
|
2021-10-24 15:00:44 +02:00
|
|
|
let fileApi = null;
|
2020-12-28 13:48:47 +02:00
|
|
|
try {
|
2021-10-24 15:00:44 +02:00
|
|
|
fileApi = await newFileApi(syncTargetId, options);
|
2021-01-14 15:07:38 +02:00
|
|
|
fileApi.requestRepeatCount_ = 0;
|
2021-10-24 15:00:44 +02:00
|
|
|
} catch (error) {
|
|
|
|
// If there's an error it's probably an application error, but we
|
|
|
|
// can't proceed anyway, so exit.
|
|
|
|
output.errorMessage = error.message;
|
|
|
|
if (error.code) output.errorMessage += ` (Code ${error.code})`;
|
|
|
|
return output;
|
|
|
|
}
|
2021-01-14 15:07:38 +02:00
|
|
|
|
2021-10-24 15:00:44 +02:00
|
|
|
// First we try to fetch info.json. It may not be present if it's a new
|
|
|
|
// sync target but otherwise, if it is, and it's valid, we know the
|
|
|
|
// credentials are valid. We do this test first because it will work
|
|
|
|
// even if account upload is disabled. And we need such account to
|
|
|
|
// successfully login so that they can fix it by deleting extraneous
|
|
|
|
// notes or resources.
|
|
|
|
try {
|
|
|
|
const r = await fileApi.get('info.json');
|
|
|
|
if (r) {
|
|
|
|
const parsed = JSON.parse(r);
|
|
|
|
if (parsed) {
|
|
|
|
output.ok = true;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Ignore because we'll use the next test to check for sure if it
|
|
|
|
// works or not.
|
|
|
|
staticLogger.warn('Could not fetch or parse info.json:', error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a more generic test, which writes a file and tries to read it
|
|
|
|
// back.
|
|
|
|
try {
|
2021-05-13 18:57:37 +02:00
|
|
|
await fileApi.put('testing.txt', 'testing');
|
|
|
|
const result = await fileApi.get('testing.txt');
|
|
|
|
if (result !== 'testing') throw new Error(`Could not access data on server "${options.path()}"`);
|
|
|
|
await fileApi.delete('testing.txt');
|
2020-12-28 13:48:47 +02:00
|
|
|
output.ok = true;
|
|
|
|
} catch (error) {
|
|
|
|
output.errorMessage = error.message;
|
|
|
|
if (error.code) output.errorMessage += ` (Code ${error.code})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
protected async initFileApi() {
|
2021-06-08 01:34:33 +02:00
|
|
|
return initFileApi(SyncTargetJoplinServer.id(), this.logger(), {
|
2020-12-28 13:48:47 +02:00
|
|
|
path: () => Setting.value('sync.9.path'),
|
2021-06-06 19:14:12 +02:00
|
|
|
userContentPath: () => Setting.value('sync.9.userContentPath'),
|
2020-12-28 13:48:47 +02:00
|
|
|
username: () => Setting.value('sync.9.username'),
|
|
|
|
password: () => Setting.value('sync.9.password'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
protected async initSynchronizer() {
|
2020-12-28 13:48:47 +02:00
|
|
|
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
|
|
|
|
}
|
|
|
|
}
|