-
Notifications
You must be signed in to change notification settings - Fork 10
/
driveAPI.js
351 lines (315 loc) · 11.5 KB
/
driveAPI.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const fs = require('fs');
const request = require('request');
const {google} = require('googleapis');
// If modifying these scopes, delete token.json.
const SCOPES = [
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.readonly',
];
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
const TOKEN_ROOT = 'secret/'
const TOKEN_PATH = TOKEN_ROOT + 'token.json';
const CACHE_ROOT = 'cache/';
const CACHE_PATH = CACHE_ROOT + 'cache.json';
const CREDENTIALS_PATH = 'secret/credentials.json';
// const TEAM_DRIVE_ID = '0AKS-uLyEVtEdUk9PVA';
class DriveAPI {
constructor() {
this.auth = null;
this.drive = null;
this.sheets = null;
this.cache = {};
/**
* The base API url for this service, used for <img src=""> in cached images
* Can (and should) be modified by client application for production deployment URL
*/
this.restEndpoint = 'https://google-drive-cms.herokuapp.com/api/v1/';
// this.restEndpoint = 'http://localhost/api/v1';
// Load client secrets from environment vars (e.g. Heroku deployment)
if (process.env.CLIENT_EMAIL && process.env.PRIVATE_KEY) {
console.log('Using environment vars for CLIENT_EMAIL and PRIVATE_KEY');
this.authorize({
client_email: process.env.CLIENT_EMAIL,
private_key: process.env.PRIVATE_KEY.replace(/\\n/gm, '\n'),
// https://stackoverflow.com/questions/62885253/environment-variables-doesnt-replace-n-with-new-line-in-javascript-and-throw
}, this.authCallback.bind(this))
} else {
// Load client secrets from file (recommended default)
console.log('Using secret/credentials.json for CLIENT_EMAIL and PRIVATE_KEY');
fs.readFile(CREDENTIALS_PATH, (err, content) => {
if (err) return this.missingCredentials()
// Authorize a client with credentials, then call the Google Drive API.
this.authorize(JSON.parse(content.toString()), this.authCallback.bind(this));
});
}
this.readCache();
}
missingCredentials() {
console.error(`Could not load ${CREDENTIALS_PATH}`);
// TODO: Service account setup info
console.log('- Start from the the Google Drive API node.js quickstart sample: https://developers.google.com/drive/api/v3/quickstart/nodejs#prerequisites');
console.log(`- Follow the steps there in order to 1) create a project, 2) enable the GDrive API, and 3) authorize credentials for a service account`);
console.log('- After you download the credentials JSON file from your Cloud Platform dashboard, place it in secret/credentials.json and run this script again');
console.log('- see ./credentials-sample.json for an example of the format of this file (but the credentials in that sample will not work!)');
process.exit(1)
}
authCallback(auth) {
console.log('Google authorization succeeded');
this.auth = auth;
this.drive = google.drive({version: 'v3', auth});
this.sheets = google.sheets({version: 'v4', auth});
// this.drive.files.list().then((res) => console.log('files: ', res.data));
}
authorize(credentials, callback) {
const { client_email, private_key } = credentials;
const jwtClient = new google.auth.JWT(client_email, null, private_key, SCOPES)
// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return this.getAccessToken(jwtClient, callback);
jwtClient.setCredentials(JSON.parse(token.toString()));
console.log('Token loaded from file');
callback(jwtClient);
});
}
getAccessToken(jwtClient, callback) {
jwtClient.authorize((err, tokens) => {
if (err) return console.log(err)
else {
if (!fs.existsSync(TOKEN_ROOT)) fs.mkdirSync(TOKEN_ROOT);
fs.writeFile(TOKEN_PATH, JSON.stringify(tokens), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(jwtClient);
}
})
}
readCache() {
fs.readFile(CACHE_PATH, (err, cache) => {
if (err) {
fs.mkdirSync(CACHE_ROOT);
return console.log('No cache found');
}
this.cache = JSON.parse(cache.toString());
console.log('Cache loaded from file');
});
}
writeCache() {
try {
fs.writeFileSync(CACHE_PATH, JSON.stringify(this.cache));
console.log('Cache stored to', CACHE_PATH);
} catch (e) {
console.error('Failed to write cache');
}
}
getSheet(id, range) {
return new Promise((resolve, reject) => {
this.sheets.spreadsheets.values.get({
spreadsheetId: id,
range: range,
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
// console.log(res.data.values);
const keys = res.data.values[0];
const transformed = [];
res.data.values.forEach((row, i) => {
if(i === 0) return;
const item = {};
row.forEach((cell, index) => {
item[keys[index]] = cell;
});
transformed.push(item);
});
resolve(transformed);
});
});
}
getCache(...params){
const hash = params.join('');
return this.cache[hash];
}
setCache(hash, version, json) {
json.version = version;
return this.cache[hash] = json;
}
getDoc(id, skipCache = false) {
return new Promise((resolve, reject) => {
this.drive.files.export({
fileId: id,
mimeType: "text/html",
fields: "data",
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
resolve({ html: this.rewriteToCachedImages(res.data) });
// Cache images
this.cacheImages(res.data);
});
});
}
cacheImages(html) {
const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,255}\.googleusercontent\.com\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
const images = html.match(URL_REGEX);
if (!images || images.length <= 0) {
return;
}
images.forEach(image => this.downloadImage(image));
}
rewriteToCachedImages(html) {
const IMG_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,255}\.googleusercontent\.com\b\/([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
return html.replace(IMG_REGEX, this.restEndpoint + '/getImage?id=$2');
}
downloadImage(url) {
const split = url.split('/');
const cache_filename = CACHE_ROOT + split[split.length - 1];
if(fs.existsSync(cache_filename)) return;
request.head(url, (err, res, body) => {
request(url).pipe(fs.createWriteStream(cache_filename))
.on('close', () => {
console.log('Cached image to', cache_filename);
})
.on('error', (err) => {
console.error('Error downloading', cache_filename)
});
});
};
listFiles(folderId, teamDriveId = undefined) {
return new Promise((resolve, reject) => {
this.drive.files.list({
orderBy: 'name',
pageSize: 20,
q: `'${folderId}' in parents and trashed != true`,
corpora: teamDriveId ? 'teamDrive' : undefined,
includeTeamDriveItems: !!teamDriveId,
supportsTeamDrives: !!teamDriveId,
teamDriveId,
fields: 'files(id, name, version)',
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
resolve(res.data.files);
});
});
}
getVersion(fileId) {
return new Promise((resolve, reject) => {
this.drive.files.get({
fileId,
supportsTeamDrives: true,
fields: 'version',
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
// console.log("version check for",fileId,"returned",res.data.version);
resolve(parseInt(res.data.version));
});
});
}
getSheetList(spreadsheetId){
return new Promise((resolve, reject) => {
this.sheets.spreadsheets.get({
spreadsheetId,
fields: "sheets(properties(title))"
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
resolve(res.data.sheets.map(obj => obj.properties.title));
});
});
}
transformSheetJson(values){
const keys = values[0];
const transformed = [];
values.forEach((row, i) => {
if(i === 0) return;
const item = {};
row.forEach((cell, index) => {
item[keys[index]] = cell;
});
transformed.push(item);
});
return transformed;
}
batchGetSheet(spreadsheetId) {
return this.getSheetList(spreadsheetId).then(sheets =>
new Promise((resolve, reject) => {
this.sheets.spreadsheets.values.batchGet({
spreadsheetId,
ranges: sheets,
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
// console.log(res.data.values);
const output = {};
res.data.valueRanges.forEach(valueRange => {
const sheetName = valueRange.range.split('!')[0];
output[sheetName] = this.transformSheetJson(valueRange.values);
});
resolve(output);
});
})
);
}
getResource(file, files) {
switch (file.mimeType) {
case 'application/vnd.google-apps.spreadsheet':
return this.batchGetSheet(file.id);
case 'application/vnd.google-apps.document':
return this.getDoc(file.id);
case 'application/vnd.google-apps.folder':
return Promise.resolve(files.filter(f => f.parents && f.parents.includes(file.id)));
default:
return Promise.reject(`Unknown file mime type: ${file.mimeType}`);
}
}
listAllFiles(teamDriveId = undefined) {
return new Promise((resolve, reject) => {
this.drive.files.list({
orderBy: 'name',
pageSize: 100,
q: `trashed != true`,
corpora: teamDriveId ? 'teamDrive' : undefined,
includeTeamDriveItems: !!teamDriveId,
supportsTeamDrives: !!teamDriveId,
teamDriveId,
fields: 'files(id, name, version, mimeType, parents)',
}, (err, res) => {
if (err) return reject('The API returned an error: ' + err);
resolve(res.data.files);
});
})
}
getCachePromise(file, files) {
const version = parseInt(file.version);
console.log('Pulling latest version for', file.name);
return this.getResource(file, files)
.then(resource => {
resource.version = version;
return this.cache[file.id] = resource;
})
.catch(error => {
console.error('Pull failed for', file.name);
console.error(error);
return this.cache[file.id];
});
}
getAll(teamDriveId = undefined) {
return this.listAllFiles(teamDriveId)
// @ts-ignore
.then(files => {
const too_old = files.filter(file => !this.cache[file.id] || this.cache[file.id].version < parseInt(file.version));
// Don't overload the API; do these in series if there are a lot
if (too_old.length > 4) {
console.log('Fetching', too_old.length, 'resources; throttling sequentially');
let promise = Promise.resolve();
too_old.forEach(file =>
promise = promise.then(() => this.getCachePromise(file, files))
)
return promise;
}
// In most cases we can run them in parallel and get faster results
else {
return Promise.all(too_old.map(file => this.getCachePromise(file, files)));
}
})
.then(() => this.cache);
}
}
module.exports = DriveAPI;