2017-11-24 01:10:55 +02:00
|
|
|
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
|
|
|
const { OneDriveApi } = require('lib/onedrive-api.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2017-11-24 01:10:55 +02:00
|
|
|
const { parameters } = require('lib/parameters.js');
|
|
|
|
const { FileApi } = require('lib/file-api.js');
|
|
|
|
const { Synchronizer } = require('lib/synchronizer.js');
|
|
|
|
const { FileApiDriverOneDrive } = require('lib/file-api-driver-onedrive.js');
|
|
|
|
|
2017-11-24 21:09:15 +02:00
|
|
|
class SyncTargetOneDrive extends BaseSyncTarget {
|
2018-01-25 21:01:14 +02:00
|
|
|
static id() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:37:40 +02:00
|
|
|
constructor(db, options = null) {
|
|
|
|
super(db, options);
|
2017-11-24 20:06:30 +02:00
|
|
|
this.api_ = null;
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 21:21:30 +02:00
|
|
|
static targetName() {
|
|
|
|
return 'onedrive';
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
static label() {
|
2017-11-24 01:10:55 +02:00
|
|
|
return _('OneDrive');
|
|
|
|
}
|
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
async isAuthenticated() {
|
2019-12-18 11:21:12 +02:00
|
|
|
return !!this.api().auth();
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 21:47:24 +02:00
|
|
|
syncTargetId() {
|
|
|
|
return SyncTargetOneDrive.id();
|
|
|
|
}
|
|
|
|
|
|
|
|
oneDriveParameters() {
|
|
|
|
return parameters().oneDrive;
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:01:14 +02:00
|
|
|
authRouteName() {
|
|
|
|
return 'OneDriveLogin';
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:06:30 +02:00
|
|
|
api() {
|
|
|
|
if (this.api_) return this.api_;
|
2017-11-24 01:10:55 +02:00
|
|
|
|
2019-12-18 11:21:12 +02:00
|
|
|
const isPublic = Setting.value('appType') != 'cli' && Setting.value('appType') != 'desktop';
|
2017-11-24 01:10:55 +02:00
|
|
|
|
2017-11-24 21:47:24 +02:00
|
|
|
this.api_ = new OneDriveApi(this.oneDriveParameters().id, this.oneDriveParameters().secret, isPublic);
|
2017-11-24 20:06:30 +02:00
|
|
|
this.api_.setLogger(this.logger());
|
2017-11-24 01:10:55 +02:00
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
this.api_.on('authRefreshed', a => {
|
2017-11-24 01:10:55 +02:00
|
|
|
this.logger().info('Saving updated OneDrive auth.');
|
2019-09-19 23:51:18 +02:00
|
|
|
Setting.setValue(`sync.${this.syncTargetId()}.auth`, a ? JSON.stringify(a) : null);
|
2017-11-24 01:10:55 +02:00
|
|
|
});
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
let auth = Setting.value(`sync.${this.syncTargetId()}.auth`);
|
2017-11-24 01:10:55 +02:00
|
|
|
if (auth) {
|
|
|
|
try {
|
|
|
|
auth = JSON.parse(auth);
|
|
|
|
} catch (error) {
|
|
|
|
this.logger().warn('Could not parse OneDrive auth token');
|
|
|
|
this.logger().warn(error);
|
|
|
|
auth = null;
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:06:30 +02:00
|
|
|
this.api_.setAuth(auth);
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
2019-07-29 15:43:53 +02:00
|
|
|
|
2017-11-24 20:06:30 +02:00
|
|
|
return this.api_;
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
async initFileApi() {
|
|
|
|
const appDir = await this.api().appDirectory();
|
2017-11-24 20:37:40 +02:00
|
|
|
const fileApi = new FileApi(appDir, new FileApiDriverOneDrive(this.api()));
|
2017-11-24 21:47:24 +02:00
|
|
|
fileApi.setSyncTargetId(this.syncTargetId());
|
2017-11-24 01:10:55 +02:00
|
|
|
fileApi.setLogger(this.logger());
|
2017-11-24 20:37:40 +02:00
|
|
|
return fileApi;
|
|
|
|
}
|
2017-11-24 01:10:55 +02:00
|
|
|
|
2017-11-24 20:37:40 +02:00
|
|
|
async initSynchronizer() {
|
2019-07-29 15:43:53 +02:00
|
|
|
if (!(await this.isAuthenticated())) throw new Error('User is not authentified');
|
2017-11-24 20:59:16 +02:00
|
|
|
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = SyncTargetOneDrive;
|