2018-03-09 22:59:12 +02:00
|
|
|
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const Setting = require('lib/models/Setting.js');
|
|
|
|
const { FileApi } = require('lib/file-api.js');
|
|
|
|
const { Synchronizer } = require('lib/synchronizer.js');
|
|
|
|
const WebDavApi = require('lib/WebDavApi');
|
|
|
|
const { FileApiDriverWebDav } = require('lib/file-api-driver-webdav');
|
2018-02-02 01:40:05 +02:00
|
|
|
|
|
|
|
class SyncTargetWebDAV extends BaseSyncTarget {
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2018-02-02 01:40:05 +02:00
|
|
|
static id() {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
2018-02-06 20:59:36 +02:00
|
|
|
static supportsConfigCheck() {
|
|
|
|
return true;
|
2018-02-02 01:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static targetName() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return 'webdav';
|
2018-02-02 01:40:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static label() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return _('WebDAV');
|
2018-02-02 01:40:05 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
async isAuthenticated() {
|
2018-02-02 01:40:05 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-21 20:36:29 +02:00
|
|
|
static async newFileApi_(syncTargetId, options) {
|
2018-02-06 20:59:36 +02:00
|
|
|
const apiOptions = {
|
2018-03-15 19:57:11 +02:00
|
|
|
baseUrl: () => options.path(),
|
|
|
|
username: () => options.username(),
|
|
|
|
password: () => options.password(),
|
2018-02-02 01:40:05 +02:00
|
|
|
};
|
|
|
|
|
2018-02-06 20:59:36 +02:00
|
|
|
const api = new WebDavApi(apiOptions);
|
2018-02-02 01:40:05 +02:00
|
|
|
const driver = new FileApiDriverWebDav(api);
|
2018-03-09 22:59:12 +02:00
|
|
|
const fileApi = new FileApi('', driver);
|
2018-02-15 20:01:05 +02:00
|
|
|
fileApi.setSyncTargetId(syncTargetId);
|
2018-02-06 20:59:36 +02:00
|
|
|
return fileApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
static async checkConfig(options) {
|
2018-02-21 20:36:29 +02:00
|
|
|
const fileApi = await SyncTargetWebDAV.newFileApi_(SyncTargetWebDAV.id(), options);
|
|
|
|
fileApi.requestRepeatCount_ = 0;
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2018-02-06 20:59:36 +02:00
|
|
|
const output = {
|
|
|
|
ok: false,
|
2018-03-09 22:59:12 +02:00
|
|
|
errorMessage: '',
|
2018-02-06 20:59:36 +02:00
|
|
|
};
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2018-02-06 20:59:36 +02:00
|
|
|
try {
|
2018-03-09 22:59:12 +02:00
|
|
|
const result = await fileApi.stat('');
|
|
|
|
if (!result) throw new Error('WebDAV directory not found: ' + options.path);
|
2018-02-06 20:59:36 +02:00
|
|
|
output.ok = true;
|
|
|
|
} catch (error) {
|
|
|
|
output.errorMessage = error.message;
|
2018-03-09 22:59:12 +02:00
|
|
|
if (error.code) output.errorMessage += ' (Code ' + error.code + ')';
|
2018-02-06 20:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
async initFileApi() {
|
2018-02-21 20:36:29 +02:00
|
|
|
const fileApi = await SyncTargetWebDAV.newFileApi_(SyncTargetWebDAV.id(), {
|
2018-03-15 19:57:11 +02:00
|
|
|
path: () => Setting.value('sync.6.path'),
|
|
|
|
username: () => Setting.value('sync.6.username'),
|
|
|
|
password: () => Setting.value('sync.6.password'),
|
2018-02-06 20:59:36 +02:00
|
|
|
});
|
|
|
|
|
2018-02-02 01:40:05 +02:00
|
|
|
fileApi.setLogger(this.logger());
|
2018-02-06 20:59:36 +02:00
|
|
|
|
2018-02-02 01:40:05 +02:00
|
|
|
return fileApi;
|
|
|
|
}
|
|
|
|
|
|
|
|
async initSynchronizer() {
|
2018-03-09 22:59:12 +02:00
|
|
|
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
|
2018-02-02 01:40:05 +02:00
|
|
|
}
|
2018-03-09 22:59:12 +02:00
|
|
|
|
2018-02-02 01:40:05 +02:00
|
|
|
}
|
|
|
|
|
2018-03-09 22:59:12 +02:00
|
|
|
module.exports = SyncTargetWebDAV;
|