forked from jarrodek/ChromeRestClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.drive.js
More file actions
315 lines (310 loc) · 9.35 KB
/
Copy pathapp.drive.js
File metadata and controls
315 lines (310 loc) · 9.35 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
(function () {
'use strict';
/*******************************************************************************
* Copyright 2012 Pawel Psztyc
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
******************************************************************************/
/* global chrome */
/**
* App's client ID.
*/
//const CLIENT_ID = '10525470235.apps.googleusercontent.com';
/**
* Google Drive REST API namespace.
*
* @namespace
*/
window.drive = {};
/**
* Authorize the app in Google Drive service.
*
* @return {Promise} Fulfilled promise with auth token or reject.
*/
window.drive.auth = function() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({'interactive': true}, (authToken) => {
if (chrome.runtime.lastError) {
return reject(chrome.runtime.lastError.message);
}
if (!authToken) {
return reject('User canceled');
}
resolve(authToken);
});
});
};
/**
* Checks if the user authenticated in the APP.
*/
window.drive.isAuth = function() {
return new Promise((resolve, reject) => {
chrome.identity.getAuthToken({'interactive': false}, (authToken) => {
if (chrome.runtime.lastError) {
return reject(new Error(chrome.runtime.lastError.message));
}
resolve(!!authToken);
});
});
};
/**
* Files manipulation API.
*
* @namespace
*/
window.drive.file = {};
/**
* File meta boundary for POST calls.
*/
window.drive.file.boundary = 'ARCFormBoundary49nr1hyovoq1tt9';
window.drive.file.delimiter = '\r\n--' + window.drive.file.boundary + '\r\n';
window.drive.file.closeDelimiter = '\r\n--' + window.drive.file.boundary + '--';
/**
* Drive's registered content type.
* It will be used to search for app's files in the Drive.
* Drive's handlers will recognize the app and will run it from Drive UI.
*/
window.drive.file.mime = 'application/restclient+data';
/**
* Extension name for files stored in Google Drive.
*/
window.drive.file.extension = 'arc';
/**
* A list of allowed resources (file metadata) in the file create request.
* See https://developers.google.com/drive/v3/reference/files/create for full list.
*/
window.drive.file.allowedResource = [
'appProperties', 'contentHints', 'createdTime', 'description', 'folderColorRgb', 'id',
'mimeType', 'modifiedTime', 'name', 'parents', 'properties', 'starred', 'viewedByMeTime',
'viewersCanCopyContent', 'writersCanShare'
];
/**
* Creates a Google Drive File.
*
* If config.resource.mimeType is not set and drive.file.mime is set then `drive.file.mime` will
* be used as a mime type.
*
* This script will automatically set file thumbnail if not set
* (config.resource.contentHints.thumbnail object value).
*
* @param {Object} config A file creation configuration consisted with:
* - {Object} resource - A file metadata. See `drive.file.allowedResource` for more information.
* - {Object} media - A file contents definition to save on drive. It must have defined following
* keys:
* - {String} mimeType - A media mime type
* - {String|Object} body - A content to save.
*/
window.drive.file.create = function(config) {
return new Promise((resolve, reject) => {
try {
config = window.drive.file.ensureDriveFileConfig(config);
} catch (e) {
reject(e);
return;
}
if (!config.resource.contentHints || !config.resource.contentHints.thumbnail ||
!config.resource.contentHints.image) {
let at;
window.drive.auth()
.then((_at) => at = _at)
.then(window.drive.file._appSafeIcon)
.then((file) => {
if (!config.resource.contentHints) {
config.resource.contentHints = {};
}
config.resource.contentHints.thumbnail = {
image: file,
mimeType: 'image/png'
};
return window.drive.file._uploadFile(at, config);
})
.then(resolve)
.catch(reject);
} else {
let at;
window.drive.auth()
.then((_at) => at = _at)
.then(() => window.drive.file._uploadFile(at, config))
.then(resolve)
.catch(reject);
}
});
};
/**
* Update the file on Google Drive.
*
* @param {String} fileId A Google Drive file ID.
* @param {Object} config The same as for `create` function.
* @return {Promise} Fulfilled promise with file properties (the response).
*/
window.drive.file.update = function(fileId, config) {
return new Promise((resolve, reject) => {
try {
config = window.drive.file.ensureDriveFileConfig(config);
} catch (e) {
reject(e);
return;
}
let at;
window.drive.auth()
.then((_at) => at = _at)
.then(() => window.drive.file._uploadUpdate(fileId, at, config))
.then(resolve)
.catch(reject);
});
};
/**
* Ensure that the file has correct configuration and throw an error if not.
* Also it will add a mime type of the file if not present.
*/
window.drive.file.ensureDriveFileConfig = function(config) {
if (!('resource' in config) ||
!('media' in config)) {
throw new Error('Invalid arguments.');
}
let names = Object.getOwnPropertyNames(config.resource);
let error = false;
let invalidArguments = [];
names.forEach((key) => {
if (window.drive.file.allowedResource.indexOf(key) === -1) {
error = true;
invalidArguments.push(key);
}
});
if (error) {
throw new Error('Unknown argument for resource: ' + invalidArguments.join(', '));
}
if (!config.resource.mimeType && window.drive.file.mime) {
config.resource.mimeType = window.drive.file.mime;
}
return config;
};
/**
* Perform upload action.
*/
window.drive.file._uploadFile = function(accessToken, options) {
var init = {
method: 'POST',
body: window.drive.file._getPayload(options),
headers: window.drive.file._getUploadHeaders(accessToken),
};
return fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', init)
.then(function(response) {
return response.json();
});
};
window.drive.file._appSafeIcon = () => {
return new Promise((resolve) => {
let url = chrome.runtime.getURL ? chrome.runtime.getURL('/assets/arc_icon_128.png') :
'/app/assets/arc_icon_128.png'; // for test cases
fetch(url)
.then((response) => {
return response.blob();
})
.then((blob) => {
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function(e) {
let data = e.target.result;
data = btoa(data)
.replace(/\+/g, '-')
.replace(/\//g, '_');
resolve(data);
};
});
});
};
window.drive.file._uploadUpdate = function(fileId, accessToken, options) {
var init = {
method: 'PATCH',
body: window.drive.file._getPayload(options),
headers: window.drive.file._getUploadHeaders(accessToken),
};
return fetch(`https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart`,
init)
.then(function(response) {
return response.json();
})
.catch((e) => {
throw e;
});
};
window.drive.file._getUploadHeaders = function(accessToken) {
var headers = new Headers();
headers.set('Authorization', 'Bearer ' + accessToken);
headers.set('Content-Type', 'multipart/related; boundary="' + window.drive.file.boundary + '"');
return headers;
};
window.drive.file._getPayload = function(config) {
var content;
if (typeof config.media.body !== 'string') {
content = JSON.stringify(config.media.body);
} else {
content = config.media.body;
}
var d = window.drive.file.delimiter;
var cd = window.drive.file.closeDelimiter;
let meta = JSON.stringify(config.resource);
let body = `${d}Content-Type: application/json; charset=UTF-8\r\n\r\n${meta}`;
body += `${d}Content-Type: ${config.media.mimeType}\r\n\r\n`;
body += `${content}${cd}`;
return body;
};
/**
* Supports `drive-request-save` that is send from request saver element.
*/
window.addEventListener('drive-request-save', function(e) {
e.preventDefault();
var request = e.detail.request;
var fileName = e.detail.fileName;
var driveId;
if (request.driveId) {
driveId = request.driveId;
delete request.driveId;
}
var promise;
var config = {
resource: {
name: fileName + '.arc',
},
media: {
mimeType: 'application/json',
body: request
}
};
if (driveId) {
promise = window.drive.file.update(driveId, config);
} else {
config.resource.description = request.description || 'Advanced REST client export file.';
promise = window.drive.file.create(config);
}
e.detail.result = promise;
});
window.addEventListener('google-drive-data-save', function(e) {
e.preventDefault();
var content = e.detail.content;
var type = e.detail.contentType;
var fileName = e.detail.file;
var config = {
resource: {
name: fileName,
description: 'Advanced REST client data export file.'
},
media: {
mimeType: type || 'application/json',
body: content
}
};
e.detail.result = window.drive.file.create(config);
});
}());