1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-04-20 11:28:40 +02:00
joplin/ReactNativeClient/lib/SyncTargetOneDrive.js

88 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-03-09 17:49:35 +00:00
const BaseSyncTarget = require("lib/BaseSyncTarget.js");
const { _ } = require("lib/locale.js");
const { OneDriveApi } = require("lib/onedrive-api.js");
const Setting = require("lib/models/Setting.js");
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 19:09:15 +00:00
class SyncTargetOneDrive extends BaseSyncTarget {
2018-01-25 19:01:14 +00:00
static id() {
return 3;
}
constructor(db, options = null) {
super(db, options);
this.api_ = null;
}
static targetName() {
2018-03-09 17:49:35 +00:00
return "onedrive";
}
static label() {
2018-03-09 17:49:35 +00:00
return _("OneDrive");
}
isAuthenticated() {
return this.api().auth();
}
syncTargetId() {
return SyncTargetOneDrive.id();
}
oneDriveParameters() {
return parameters().oneDrive;
}
2018-01-25 19:01:14 +00:00
authRouteName() {
2018-03-09 17:49:35 +00:00
return "OneDriveLogin";
2018-01-25 19:01:14 +00:00
}
api() {
if (this.api_) return this.api_;
2018-03-09 17:49:35 +00:00
const isPublic = Setting.value("appType") != "cli";
this.api_ = new OneDriveApi(this.oneDriveParameters().id, this.oneDriveParameters().secret, isPublic);
this.api_.setLogger(this.logger());
2018-03-09 17:49:35 +00:00
this.api_.on("authRefreshed", a => {
this.logger().info("Saving updated OneDrive auth.");
Setting.setValue("sync." + this.syncTargetId() + ".auth", a ? JSON.stringify(a) : null);
});
2018-03-09 17:49:35 +00:00
let auth = Setting.value("sync." + this.syncTargetId() + ".auth");
if (auth) {
try {
auth = JSON.parse(auth);
} catch (error) {
2018-03-09 17:49:35 +00:00
this.logger().warn("Could not parse OneDrive auth token");
this.logger().warn(error);
auth = null;
}
this.api_.setAuth(auth);
}
2018-03-09 17:49:35 +00:00
return this.api_;
}
async initFileApi() {
const appDir = await this.api().appDirectory();
const fileApi = new FileApi(appDir, new FileApiDriverOneDrive(this.api()));
fileApi.setSyncTargetId(this.syncTargetId());
fileApi.setLogger(this.logger());
return fileApi;
}
async initSynchronizer() {
2018-03-09 17:49:35 +00:00
if (!this.isAuthenticated()) throw new Error("User is not authentified");
return new Synchronizer(this.db(), await this.fileApi(), Setting.value("appType"));
}
}
2018-03-09 17:49:35 +00:00
module.exports = SyncTargetOneDrive;