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();
|
|
|
|
}
|
|
|
|
|
2020-08-02 13:28:50 +02:00
|
|
|
isTesting() {
|
|
|
|
const p = parameters();
|
|
|
|
return !!p.oneDriveTest;
|
|
|
|
}
|
|
|
|
|
2017-11-24 21:47:24 +02:00
|
|
|
oneDriveParameters() {
|
2020-08-02 13:28:50 +02:00
|
|
|
const p = parameters();
|
|
|
|
if (p.oneDriveTest) return p.oneDriveTest;
|
|
|
|
return p.oneDrive;
|
2017-11-24 21:47:24 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 21:01:14 +02:00
|
|
|
authRouteName() {
|
|
|
|
return 'OneDriveLogin';
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:06:30 +02:00
|
|
|
api() {
|
2020-08-02 13:28:50 +02:00
|
|
|
if (this.isTesting()) {
|
|
|
|
return this.fileApi_.driver().api();
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:06:30 +02:00
|
|
|
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() {
|
2020-08-08 01:35:30 +02:00
|
|
|
let context = Setting.value(`sync.${this.syncTargetId()}.context`);
|
|
|
|
context = context === '' ? null : JSON.parse(context);
|
|
|
|
let accountProperties = context ? context.accountProperties : null;
|
|
|
|
if (!accountProperties) {
|
|
|
|
accountProperties = await this.api_.execAccountPropertiesRequest();
|
|
|
|
context ? context.accountProperties = accountProperties : context = { accountProperties: accountProperties };
|
|
|
|
Setting.setValue(`sync.${this.syncTargetId()}.context`, JSON.stringify(context));
|
|
|
|
}
|
|
|
|
this.api_.setAccountProperties(accountProperties);
|
2017-11-24 20:59:16 +02:00
|
|
|
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() {
|
2020-08-08 01:35:30 +02:00
|
|
|
try {
|
|
|
|
if (!(await this.isAuthenticated())) throw new Error('User is not authentified');
|
|
|
|
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
|
|
|
|
} catch (error) {
|
|
|
|
BaseSyncTarget.dispatch({ type: 'SYNC_REPORT_UPDATE', report: { errors: [error] } });
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = SyncTargetOneDrive;
|