-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMimeTypeApp.js
489 lines (465 loc) · 17.5 KB
/
MimeTypeApp.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/**
* ### Description
* MimeType converter with Google Apps Script.
* GitHub: https://github.com/tanaikech/MimeTypeApp
*
* MimeTypeApp v1.0.0
*
* Author: Kanshi Tanaike
* @class
*/
class MimeTypeApp {
/**
* @constructor
*/
constructor() {
/** @private */
this.fileIds = [];
/** @private */
this.blobs = [];
/** @private */
this.headers = { authorization: "Bearer " + ScriptApp.getOAuthToken() };
/** @private */
this.sleep = 2000;
}
/**
* Set file IDs.
*
* @param {Array} fileIds Array including file IDs.
* @return {MimeTypeApp} Return MimeTypeApp
*
*/
setFileIds(fileIds) {
this.fileIds = fileIds;
return this;
}
/**
* Set Blobs.
*
* @param {Array} blobs Array including blobs.
* @return {MimeTypeApp} Return MimeTypeApp
*
*/
setBlobs(blobs) {
this.blobs = blobs;
return this;
}
/**
* Get conversion list
*
* @return {Object} Return object including the mimeTypes that this script can convert.
*
*/
getConversionList() {
const { importFormats, exportFormats } = this.getFormats_();
const obj = Object.fromEntries(Object.entries(importFormats).map(([k, [v]]) => [k, exportFormats[v]]));
return { ...obj, ...exportFormats };
}
/**
* Get file IDs converted to the given mimeType.
*
* @param {Object} object Object including setting data.
* @param {String} object.mimeType Target mimeType.
* @param {String} object.folderId Destination folder ID. The default folder is "root." When the target MIME type is a Google Docs file (Document, Spreadsheet, Slide, etc.), the converted data is required to be created as a file. When this is set, the created files are put into this folder.
* @param {Boolean} object.onlyCheck The default is false. When set to true, it verifies whether the input file IDs or blobs can be converted to the target MIME type and returns the verified result.
* @return {Array<Object>} Return an array containing the blobs or file IDs of the converted files. When the target MIME type is a Google Docs file (Document, Spreadsheet, Slide, etc.), the destination data type must be a file. Therefore, the file ID is returned. When the target MIME type is not a Google Docs file, the blob can be returned as the destination data type. Hence, the blob is returned.
*/
getAs(object) {
const { mimeType, folderId = "root", onlyCheck = false } = object;
if (mimeType.includes("application/vnd.google-apps")) {
console.log(`In the case of "${mimeType}", an array including the file IDs of the converted data are returned as the output.`);
} else {
console.log(`In the case of "${mimeType}", an array including the blobs of the converted data are returned as the output.`);
}
const dstFolderId = folderId;
if ((!this.fileIds || this.fileIds.length == 0) && (!this.blobs || this.blobs.length == 0)) {
throw new Error("Set file IDs or Blobs.");
}
if (!mimeType) {
throw new Error("Set export mimeType.");
}
const { importFormats, exportFormats } = this.getFormats_();
const { routeFileIds, routeBlobs } = this.getRouteObj_({ mimeType, importFormats, exportFormats });
if (onlyCheck) {
const checked = [];
if (routeFileIds.length > 0) {
checked.push(...routeFileIds.map((e, i) => {
const temp = { fileId: this.fileIds[i] };
if (e.length > 0) {
temp.isPossible = true;
temp.conversionRoute = e.map(({ from, to }) => from || to);
} else {
temp.isPossible = false;
}
return temp;
}));
}
if (routeBlobs.length > 0) {
checked.push(...routeBlobs.map((e, i) => {
const temp = { blob: this.blobs[i] };
if (e.length > 0) {
temp.isPossible = true;
temp.conversionRoute = e.map(({ from, to }) => from || to);
} else {
temp.isPossible = false;
}
return temp;
}));
}
return checked;
}
if (this.fileIds.length > 0) {
const r1 = this.fileIds.map((e, i) => {
const tempFileIds = [];
const f = DriveApp.getFileById(e);
const filename = f.getName();
const srcMimeType = f.getMimeType();
console.log(`Start: Converting from (${filename}) "${srcMimeType}" to "${mimeType}".`);
if (routeFileIds[i] == 0) {
if (srcMimeType == mimeType) {
console.warn(`End: This conversion is not run because source mimeType and destination mimeType are the same.`);
return srcMimeType.includes("application/vnd.google-apps") ? e : f.getBlob().copyBlob();
}
console.warn(`End: This conversion cannot be achieved.`);
return null;
}
const [, ...r] = routeFileIds[i];
const res = r.reduce((o, { to, convert }, i, a) => {
if (convert == "import") {
o.currentId = this.import_({ srcFileId: o.currentId, filename, dstMimeType: to, dstFolderId });
if (i != a.length - 1) {
tempFileIds.push(o.currentId.toString());
}
} else if (convert == "export") {
if (i == a.length - 1) {
if (mimeType.includes("application/vnd.google-apps")) {
o.currentId = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "fileId" });
} else {
o.currentBlob = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "blob" });
}
} else {
o.currentId = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "fileId" });
tempFileIds.push(o.currentId.toString());
}
}
return o;
}, { currentId: e, currentBlob: null });
tempFileIds.forEach(e => this.deleteTemporalFile_(e));
console.log(`End: Done.`);
return mimeType.includes("application/vnd.google-apps") ? res.currentId : res.currentBlob;
});
return r1;
}
if (this.blobs.length > 0) {
const r2 = this.blobs.map((e, i) => {
const tempFileIds = [];
const filename = e.getName();
const srcMimeType = e.getContentType();
console.log(`Start: Converting from (${filename}) "${srcMimeType}" to "${mimeType}".`);
if (routeBlobs[i] == 0) {
if (srcMimeType == mimeType) {
console.warn(`End: This conversion is not run because source mimeType and destination mimeType are the same.`);
return e;
}
console.warn(`End: This conversion cannot be achieved.`);
return null;
}
const [, ...r] = routeBlobs[i];
const tempId = DriveApp.getFolderById(dstFolderId).createFile(e).getId();
tempFileIds.push(tempId);
const res = r.reduce((o, { to, convert }, i, a) => {
if (convert == "import") {
o.currentId = this.import_({ srcFileId: o.currentId, filename, dstMimeType: to, dstFolderId });
if (i != a.length - 1) {
tempFileIds.push(o.currentId.toString());
}
} else if (convert == "export") {
if (i == a.length - 1) {
if (mimeType.includes("application/vnd.google-apps")) {
o.currentId = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "fileId" });
} else {
o.currentBlob = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "blob" });
}
} else {
o.currentId = this.export_({ srcFileId: o.currentId, filename, finalMimeType: to, dstFolderId, returnValue: "fileId" });
tempFileIds.push(o.currentId.toString());
}
}
return o;
}, { currentId: tempId, currentBlob: null });
tempFileIds.forEach(e => this.deleteTemporalFile_(e));
console.log(`End: Done.`);
return mimeType.includes("application/vnd.google-apps") ? res.currentId : res.currentBlob;
});
return r2;
}
return null;
}
/**
* Get thumbnail images as blobs from file IDs.
*
* @param {Number} width Width of the exported thumbnail image. The default is 1000.
* @param {String} dstFolderId Destination folder ID. The default folder is "root".
* @return {Array<Object>} Return an array including the thumbnail blobs of the inputted files.
*
*/
getThumbnails(width = 1000) {
if (!this.fileIds || this.fileIds.length == 0) {
throw new Error("Set file IDs using the setFileIds method. Thumbnails can only be retrieved from file IDs.");
}
return this.fileIds.map(id => {
const file = DriveApp.getFileById(id);
const filename = file.getName();
const srcMimeType = file.getMimeType();
console.log(`Start: Thumbnail image is retrieved from "${filename}" (${srcMimeType}).`);
const url = `https://drive.google.com/thumbnail?sz=w${width}&id=${id}`;
let blob = null;
const res = UrlFetchApp.fetch(url, { headers: this.headers, muteHttpExceptions: true });
if (res.getResponseCode() == 200) {
blob = res.getBlob().copyBlob().setName(filename);
}
console.log(blob ? "End: Done." : "End: This conversion cannot be achieved.");
return blob;
});
}
/**
* Get file ID from files.
*
* @param {Object} object Object for this method.
* @param {String} object.srcFileId Source file ID.
* @param {String} object.filename Filename.
* @param {String} object.dstMimeType Final mimeType.
* @param {String} object.dstFolderId Destination folder ID.
* @return {String|Blob} Return file ID or Blob.
*
* @private
*/
import_(object) {
const { srcFileId, filename, dstMimeType, dstFolderId } = object;
const url = `https://www.googleapis.com/drive/v3/files/${srcFileId}/copy`;
const queryObj = { supportsAllDrives: true };
const payload = { name: filename, mimeType: dstMimeType, parents: [dstFolderId] };
const request = {
url: this.addQueryParameters_(url, queryObj),
method: "post",
contentType: "application/json",
payload: JSON.stringify(payload),
headers: this.headers,
muteHttpExceptions: true,
};
const res = this.fetch_(request);
const obj = this.fetchErrProc_(res);
return obj.id;
}
/**
* Get file ID or blob from Google Docs.
*
* @param {Object} object Object for this method.
* @param {String} object.srcFileId Source file ID.
* @param {String} object.filename Filename.
* @param {String} object.finalMimeType Final mimeType.
* @param {String} object.dstFolderId Destination folder ID.
* @param {String} object.returnValue Return value. "Blob" or "fileId"
* @return {String|Blob} Return file ID or Blob.
*
* @private
*/
export_(object) {
const { srcFileId, filename, finalMimeType, dstFolderId, returnValue = "blob" } = object;
const queryObj = { supportsAllDrives: true, fields: "exportLinks" };
const request = {
url: this.addQueryParameters_(`https://www.googleapis.com/drive/v3/files/${srcFileId}`, queryObj),
headers: this.headers,
muteHttpExceptions: true,
};
const res = this.fetch_(request);
const obj = this.fetchErrProc_(res);
const ress = this.fetch_({ url: obj.exportLinks[finalMimeType], headers: this.headers, muteHttpExceptions: true });
let rValue = null;
if (ress.getResponseCode() == 200) {
const blob = ress.getBlob().copyBlob().setName(filename);
if (returnValue == "blob") {
rValue = blob;
} else if (returnValue == "fileId") {
rValue = DriveApp.getFolderById(dstFolderId).createFile(blob).getId();
}
}
return rValue;
}
/**
* Get Route object.
*
* @param {Object} object Object including mimeType, exportFormats, importFormats.
* @return {Array<Object>} Return 2 objects of routeFileIds and routeBlobs.
*
* @private
*/
getRouteObj_(object) {
const { mimeType, importFormats, exportFormats } = object;
const route = { routeFileIds: [], routeBlobs: [] };
if (this.fileIds.length > 0) {
const r = this.fileIds.map(id => {
const srcMimeType = DriveApp.getFileById(id).getMimeType();
return this.getRoute_({ importFormats, exportFormats, srcMimeType, dstMimeType: mimeType });
});
route.routeFileIds.push(...r);
}
if (this.blobs.length > 0) {
const r = this.blobs.map(blob => {
const srcMimeType = blob.getContentType();
if (!srcMimeType) {
throw new Error("Please set content type to blob.");
}
return this.getRoute_({ importFormats, exportFormats, srcMimeType, dstMimeType: mimeType });
});
route.routeBlobs.push(...r);
}
return route;
}
/**
* Get Blobs converted to the given mimeType.
*
* @param {Object} object Object including exportFormats, importFormats, srcMimeType, dstMimeType.
* @return {Array<Object>} Return an array including the route for converting mimeType.
*
* @private
*/
getRoute_(object) {
const { srcMimeType, dstMimeType } = object;
if (srcMimeType == dstMimeType) {
return [];
}
let exf = {};
let imf = {};
if (object.exportFormats[srcMimeType]) {
exf = "export";
imf = "import";
} else if (object.importFormats[srcMimeType]) {
exf = "import";
imf = "export";
} else {
return [];
}
const route = object[`${exf}Formats`][srcMimeType].reduce((ar, e) => {
if (e == dstMimeType) {
ar.push([{ from: srcMimeType }, { to: e, convert: exf }]);
}
if (object[`${imf}Formats`][e]) {
object[`${imf}Formats`][e].forEach(f => {
if (f == dstMimeType) {
ar.push([{ from: srcMimeType }, { to: e, convert: exf }, { to: f, convert: imf }]);
}
if (object[`${exf}Formats`][f]) {
object[`${exf}Formats`][f].forEach(g => {
if (g == dstMimeType) {
ar.push([{ from: srcMimeType }, { to: e, convert: exf }, { to: f, convert: imf }, { to: g, convert: exf }]);
}
if (object[`${imf}Formats`][g]) {
object[`${imf}Formats`][g].forEach(h => {
if (h == dstMimeType) {
ar.push([{ from: srcMimeType }, { to: e, convert: exf }, { to: f, convert: imf }, { to: g, convert: exf }, { to: h, convert: imf }]);
}
});
}
});
}
});
}
return ar;
}, []);
if (route.length == 0) {
return route;
}
const min = Math.min(...route.map(e => e.length));
return route.filter(e => e.length == min)[0];
}
/**
* Delete temporal file.
*
* @param {String} fileId File ID.
* @return {void}
*
* @private
*/
deleteTemporalFile_(fileId) {
const request = {
url: `https://www.googleapis.com/drive/v3/files/${fileId}?supportsAllDrives=true`,
method: "delete",
headers: this.headers,
muteHttpExceptions: true,
};
const ress = this.fetch_(request);
this.fetchErrProc_(ress);
}
/**
* Get formats for converting mimeType.
*
* @return {Object} Return object including mimeTypes.
*
* @private
*/
getFormats_() {
const queryObj = { fields: "importFormats,exportFormats" };
const request = {
url: this.addQueryParameters_("https://www.googleapis.com/drive/v3/about", queryObj),
headers: this.headers,
muteHttpExceptions: true,
};
const res = this.fetch_(request);
return this.fetchErrProc_(res);
}
/**
* For HTTP request.
*
* @param {UrlFetchApp.HTTPResponse}
* @return {Object}
*
* @private
*/
fetchErrProc_(object) {
const text = object.getContentText();
if ([200, 204].includes(object.getResponseCode())) {
return text && JSON.parse(text);
}
throw new Error(text);
}
/**
* For HTTP request.
*
* @param {Object} object Object including request.
* @return {UrlFetchApp.HTTPResponse}
*
* @private
*/
fetch_(object) {
return UrlFetchApp.fetchAll([object])[0];
}
/**
* ### Description
* This method is used for adding the query parameters to the URL.
* Ref: https://tanaikech.github.io/2018/07/12/adding-query-parameters-to-url-using-google-apps-script/
*
* @param {String} url The base URL for adding the query parameters.
* @param {Object} obj JSON object including query parameters.
* @return {String} URL including the query parameters.
*
* @private
*/
addQueryParameters_(url, obj) {
if (url === null || obj === null || typeof url != "string") {
throw new Error(
"Please give URL (String) and query parameter (JSON object)."
);
}
return (
(url == "" ? "" : `${url}?`) +
Object.entries(obj)
.flatMap(([k, v]) =>
Array.isArray(v)
? v.map((e) => `${k}=${encodeURIComponent(e)}`)
: `${k}=${encodeURIComponent(v)}`
)
.join("&")
);
}
}