1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/ReactNativeClient/lib/SyncTargetDropbox.js

75 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-03-24 21:35:10 +02:00
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
const { _ } = require('lib/locale.js');
const DropboxApi = require('lib/DropboxApi');
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 { FileApiDriverDropbox } = require('lib/file-api-driver-dropbox.js');
class SyncTargetDropbox extends BaseSyncTarget {
static id() {
return 7;
}
constructor(db, options = null) {
super(db, options);
this.api_ = null;
}
static targetName() {
return 'dropbox';
}
static label() {
return _('Dropbox');
}
2018-03-26 19:33:55 +02:00
authRouteName() {
return 'DropboxLogin';
}
async isAuthenticated() {
const f = await this.fileApi();
2019-07-29 15:43:53 +02:00
return !!f
.driver()
.api()
.authToken();
2018-03-26 19:33:55 +02:00
}
async api() {
const fileApi = await this.fileApi();
return fileApi.driver().api();
2018-03-24 21:35:10 +02:00
}
async initFileApi() {
2018-03-26 19:33:55 +02:00
const params = parameters().dropbox;
const api = new DropboxApi({
id: params.id,
secret: params.secret,
});
api.on('authRefreshed', auth => {
2018-05-22 16:02:35 +02:00
this.logger().info('Saving updated Dropbox auth.');
2019-09-19 23:51:18 +02:00
Setting.setValue(`sync.${SyncTargetDropbox.id()}.auth`, auth ? auth : null);
});
2019-09-19 23:51:18 +02:00
const authToken = Setting.value(`sync.${SyncTargetDropbox.id()}.auth`);
api.setAuthToken(authToken);
2018-03-26 19:33:55 +02:00
2018-03-24 21:35:10 +02:00
const appDir = '';
const fileApi = new FileApi(appDir, new FileApiDriverDropbox(api));
fileApi.setSyncTargetId(SyncTargetDropbox.id());
2018-03-24 21:35:10 +02:00
fileApi.setLogger(this.logger());
return fileApi;
}
async initSynchronizer() {
2018-03-26 19:33:55 +02:00
if (!(await this.isAuthenticated())) throw new Error('User is not authentified');
2018-03-24 21:35:10 +02:00
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
}
}
2018-05-22 16:02:35 +02:00
module.exports = SyncTargetDropbox;