-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
51 lines (47 loc) · 1.44 KB
/
index.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
'use strict';
/**
* Module exports.
* @public
*/
exports.getMediaLink = getMediaLink;
/**
* Get the media link (video url and thumb url) of the given google drive document id.
* @param {string} docId
* @returns
*/
async function getMediaLink(docId) {
try {
const reqThumbnail = fetch('https://drive.google.com/thumbnail?sz=w320&id=' + docId, {
redirect: 'manual'
}).catch((x) => {
return null;
});
const directUrl = 'https://drive.usercontent.google.com/download?id=' + docId + '&export=download&confirm=1';
const reqMedia = await fetch(directUrl, {
method: 'head'
}).catch(x => {
return createFailedResponse(x.status, x.message);
});
if (reqMedia.ok) {
const thumbResponse = await reqThumbnail;
const thumbSource = (thumbResponse && thumbResponse.headers && thumbResponse.headers.get('location')) || '';
return createSuccessResponse(directUrl, thumbSource);
} else {
return createFailedResponse(reqMedia.status, reqMedia.statusText);
}
} catch (error) {
throw ('Error while fetching the media link.' + error);
}
}
function createFailedResponse(status, error) {
return {
satusCode: status,
error: error
}
}
function createSuccessResponse(videoSource, thumbSource) {
return {
src: videoSource,
thumbnail: thumbSource
};
}