-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
SyncTargetDropbox.js
74 lines (60 loc) · 1.77 KB
/
SyncTargetDropbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const BaseSyncTarget = require('lib/BaseSyncTarget.js');
const { _ } = require('lib/locale');
const DropboxApi = require('lib/DropboxApi');
const Setting = require('lib/models/Setting').default;
const { parameters } = require('lib/parameters.js');
const { FileApi } = require('lib/file-api.js');
const Synchronizer = require('lib/Synchronizer').default;
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');
}
authRouteName() {
return 'DropboxLogin';
}
async isAuthenticated() {
const f = await this.fileApi();
return !!f
.driver()
.api()
.authToken();
}
async api() {
const fileApi = await this.fileApi();
return fileApi.driver().api();
}
async initFileApi() {
const params = parameters().dropbox;
const api = new DropboxApi({
id: params.id,
secret: params.secret,
});
api.on('authRefreshed', auth => {
this.logger().info('Saving updated Dropbox auth.');
Setting.setValue(`sync.${SyncTargetDropbox.id()}.auth`, auth ? auth : null);
});
const authToken = Setting.value(`sync.${SyncTargetDropbox.id()}.auth`);
api.setAuthToken(authToken);
const appDir = '';
const fileApi = new FileApi(appDir, new FileApiDriverDropbox(api));
fileApi.setSyncTargetId(SyncTargetDropbox.id());
fileApi.setLogger(this.logger());
return fileApi;
}
async initSynchronizer() {
if (!(await this.isAuthenticated())) throw new Error('User is not authentified');
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
}
}
module.exports = SyncTargetDropbox;