/
dropbox-provider.js
164 lines (150 loc) · 5.94 KB
/
dropbox-provider.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// DROPBOX
import {Dropbox} from 'dropbox';
import window from 'global/window';
import {parseQueryString} from '../utils/url';
import DropboxIcon from '../components/icons/dropbox-icon';
const NAME = 'dropbox';
const DOMAIN = 'www.dropbox.com';
const KEPLER_DROPBOX_FOLDER_LINK = `//${DOMAIN}/home/Apps`;
const CORS_FREE_DOMAIN = 'dl.dropboxusercontent.com';
export default class DropboxProvider {
constructor(clientId, appName, icon = DropboxIcon) {
// All cloud-providers providers must implement the following properties
this.name = NAME;
this.clientId = clientId;
this.appName = appName;
this.icon = icon;
this._folderLink = `${KEPLER_DROPBOX_FOLDER_LINK}/${appName}`;
// Initialize Dropbox API
this._dropbox = new Dropbox({fetch: window.fetch});
this._dropbox.setClientId(clientId);
}
/**
* This method will handle the oauth flow by performing the following steps:
* - Opening a new window
* - Subscribe to message channel
* - Receive the token when ready
* - Close the opened tab
*/
login(onCloudLoginSuccess) {
const link = this._authLink();
const authWindow = window.open(link, '_blank', 'width=1024,height=716');
const handleToken = e => {
// TODO: add security step to validate which domain the message is coming from
authWindow.close();
window.removeEventListener('message', handleToken);
this._dropbox.setAccessToken(e.data.token);
if (window.localStorage) {
window.localStorage.setItem('dropbox', JSON.stringify({
// dropbox token doesn't expire unless revoked by the user
token: e.data.token,
timestamp: new Date()
}));
}
onCloudLoginSuccess(this.name);
};
window.addEventListener('message', handleToken);
}
/**
*
* @param blob to upload
* @param name if blob doesn't contain a file name, this field is used
* @param isPublic define whether the file will be available pubblicaly once uploaded
* @returns {Promise<DropboxTypes.files.FileMetadata>}
*/
async uploadFile({blob, name, isPublic = true}) {
const metadata = await this._dropbox.filesUpload({
path: name || blob.name,
contents: blob
});
return isPublic ? await this.shareFile(metadata) : metadata;
}
/**
* It will set access to file to public
* @param {Object} metadata metadata response from uploading the file
* @returns {Promise<DropboxTypes.sharing.FileLinkMetadataReference | DropboxTypes.sharing.FolderLinkMetadataReference | DropboxTypes.sharing.SharedLinkMetadataReference>}
*/
shareFile(metadata) {
return this._dropbox.sharingCreateSharedLinkWithSettings({
path: metadata.path_display || metadata.path_lower
}).then(
// Update URL to avoid CORS issue
// Unfortunately this is not the ideal scenario but it will make sure people
// can share dropbox urls with users without the dropbox account (publish on twitter, facebook)
result => ({
...result,
folder_link: this._folderLink,
url: this._overrideUrl(result.url)
})
);
}
/**
* Provides the current dropbox auth token. If stored in localStorage is set onto dropbox handler and returned
* @returns {any}
*/
getAccessToken() {
let token = this._dropbox.getAccessToken();
if (!token && window.localStorage) {
const jsonString = window.localStorage.getItem('dropbox');
token = jsonString && JSON.parse(jsonString).token;
if (token) {
this._dropbox.setAccessToken(token);
}
}
return (token || '') !== '' ? token : null;
}
/**
* This method will extract the auth token from the third party service callback url.
* @param {object} location the window location provided by react router
* @returns {?string} the token extracted from the oauth 2 callback URL
*/
getAccessTokenFromLocation(location) {
if (!(location && location.hash.length)) {
return null;
}
// dropbox token usually start with # therefore we want to remore the '#'
const query = window.location.hash.substring(1);
return parseQueryString(query).access_token;
}
// PRIVATE
/**
* Generate auth link url to open to be used to handle OAuth2
* @param {string} path
*/
_authLink(path = 'auth') {
return this._dropbox.getAuthenticationUrl(
`${window.location.origin}/${path}`,
btoa(JSON.stringify({handler: 'dropbox', origin: window.location.origin}))
)
}
/**
* Override dropbox cloud-providers url
* https://www.dropbox.com/s/bxwwdb81z0jg7pb/keplergl_2018-11-01T23%3A22%3A43.940Z.json?dl=0
* ->
* https://dl.dropboxusercontent.com/s/bxwwdb81z0jg7pb/keplergl_2018-11-01T23%3A22%3A43.940Z.json
* @param metadata
* @returns {DropboxTypes.sharing.FileLinkMetadataReference}
*/
_overrideUrl(url) {
return url ? url.slice(0, url.indexOf('?')).replace(DOMAIN, CORS_FREE_DOMAIN) : null;
}
}