1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00
joplin/packages/lib/SyncTargetWebDAV.js

89 lines
2.3 KiB
JavaScript
Raw Normal View History

const BaseSyncTarget = require('./BaseSyncTarget').default;
2020-11-05 18:58:23 +02:00
const { _ } = require('./locale');
const Setting = require('./models/Setting').default;
const { FileApi } = require('./file-api.js');
const Synchronizer = require('./Synchronizer').default;
const WebDavApi = require('./WebDavApi');
const { FileApiDriverWebDav } = require('./file-api-driver-webdav');
class SyncTargetWebDAV extends BaseSyncTarget {
static id() {
return 6;
}
static supportsConfigCheck() {
return true;
}
static targetName() {
return 'webdav';
}
static label() {
return _('WebDAV');
}
2021-08-16 16:20:14 +02:00
static description() {
return 'The WebDAV protocol allows users to create, change and move documents on a server. There are many WebDAV compatible servers, including SeaFile, Nginx or Apache.';
}
2018-03-26 19:33:55 +02:00
async isAuthenticated() {
return true;
}
static async newFileApi_(syncTargetId, options) {
const apiOptions = {
baseUrl: () => options.path(),
username: () => options.username(),
password: () => options.password(),
ignoreTlsErrors: () => options.ignoreTlsErrors(),
};
const api = new WebDavApi(apiOptions);
const driver = new FileApiDriverWebDav(api);
const fileApi = new FileApi('', driver);
fileApi.setSyncTargetId(syncTargetId);
return fileApi;
}
static async checkConfig(options) {
const fileApi = await SyncTargetWebDAV.newFileApi_(SyncTargetWebDAV.id(), options);
fileApi.requestRepeatCount_ = 0;
2019-07-29 15:43:53 +02:00
const output = {
ok: false,
errorMessage: '',
};
2019-07-29 15:43:53 +02:00
try {
const result = await fileApi.stat('');
2019-09-19 23:51:18 +02:00
if (!result) throw new Error(`WebDAV directory not found: ${options.path()}`);
output.ok = true;
} catch (error) {
output.errorMessage = error.message;
2019-09-19 23:51:18 +02:00
if (error.code) output.errorMessage += ` (Code ${error.code})`;
}
return output;
}
async initFileApi() {
const fileApi = await SyncTargetWebDAV.newFileApi_(SyncTargetWebDAV.id(), {
path: () => Setting.value('sync.6.path'),
username: () => Setting.value('sync.6.username'),
password: () => Setting.value('sync.6.password'),
ignoreTlsErrors: () => Setting.value('net.ignoreTlsErrors'),
});
fileApi.setLogger(this.logger());
return fileApi;
}
async initSynchronizer() {
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
}
}
2019-07-29 15:43:53 +02:00
module.exports = SyncTargetWebDAV;