2021-01-20 17:49:02 +02:00
|
|
|
import OneDriveApi from './onedrive-api';
|
|
|
|
import { _ } from './locale';
|
|
|
|
import Setting from './models/Setting';
|
|
|
|
import Synchronizer from './Synchronizer';
|
2021-01-29 20:45:11 +02:00
|
|
|
import BaseSyncTarget from './BaseSyncTarget';
|
2021-01-20 17:49:02 +02:00
|
|
|
|
2020-11-05 18:58:23 +02:00
|
|
|
const { parameters } = require('./parameters.js');
|
|
|
|
const { FileApi } = require('./file-api.js');
|
|
|
|
const { FileApiDriverOneDrive } = require('./file-api-driver-onedrive.js');
|
2017-11-24 01:10:55 +02:00
|
|
|
|
2021-01-20 17:49:02 +02:00
|
|
|
export default class SyncTargetOneDrive extends BaseSyncTarget {
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
private api_: any;
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static id() {
|
2018-01-25 21:01:14 +02:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(db: any, options: any = null) {
|
2017-11-24 20:37:40 +02:00
|
|
|
super(db, options);
|
2017-11-24 20:06:30 +02:00
|
|
|
this.api_ = null;
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static targetName() {
|
2017-11-24 21:21:30 +02:00
|
|
|
return 'onedrive';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static label() {
|
2017-11-24 01:10:55 +02:00
|
|
|
return _('OneDrive');
|
|
|
|
}
|
|
|
|
|
2021-08-16 16:20:14 +02:00
|
|
|
public static description() {
|
|
|
|
return 'A file hosting service operated by Microsoft as part of its web version of Office.';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static supportsSelfHosted(): boolean {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async isAuthenticated() {
|
2019-12-18 11:21:12 +02:00
|
|
|
return !!this.api().auth();
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public syncTargetId() {
|
2017-11-24 21:47:24 +02:00
|
|
|
return SyncTargetOneDrive.id();
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public isTesting() {
|
2020-08-02 13:28:50 +02:00
|
|
|
const p = parameters();
|
|
|
|
return !!p.oneDriveTest;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public authRouteName() {
|
2018-01-25 21:01:14 +02:00
|
|
|
return 'OneDriveLogin';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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
|
|
|
|
2022-07-23 09:31:32 +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 01:10:55 +02:00
|
|
|
|
2021-01-20 17:49:02 +02:00
|
|
|
this.api_.on('authRefreshed', (a: any) => {
|
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
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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;
|
2023-03-30 18:41:29 +02:00
|
|
|
const api = this.api();
|
|
|
|
|
2020-08-08 01:35:30 +02:00
|
|
|
if (!accountProperties) {
|
2023-03-30 18:41:29 +02:00
|
|
|
accountProperties = await api.execAccountPropertiesRequest();
|
2020-08-08 01:35:30 +02:00
|
|
|
context ? context.accountProperties = accountProperties : context = { accountProperties: accountProperties };
|
|
|
|
Setting.setValue(`sync.${this.syncTargetId()}.context`, JSON.stringify(context));
|
|
|
|
}
|
2023-03-30 18:41:29 +02:00
|
|
|
api.setAccountProperties(accountProperties);
|
2017-11-24 20:59:16 +02:00
|
|
|
const appDir = await this.api().appDirectory();
|
2023-03-12 17:21:31 +02:00
|
|
|
// the appDir might contain non-ASCII characters
|
|
|
|
// /[^\u0021-\u00ff]/ is used in Node.js to detect the unescaped characters.
|
|
|
|
// See https://github.com/nodejs/node/blob/bbbf97b6dae63697371082475dc8651a6a220336/lib/_http_client.js#L176
|
|
|
|
const baseDir = RegExp(/[^\u0021-\u00ff]/).exec(appDir) !== null ? encodeURI(appDir) : appDir;
|
|
|
|
const fileApi = new FileApi(baseDir, 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
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|