forked from chahakshahcs5/nicepage-template-scrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdrive.js
62 lines (60 loc) · 1.47 KB
/
gdrive.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
import { google } from "googleapis";
import dotenv from "dotenv";
dotenv.config();
const client_secret = process.env.CLIENT_SECRET;
const client_id = process.env.CLIENT_ID;
const redirect_uris = JSON.parse(process.env.REDIRECT_URIS);
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
const token = {
access_token: process.env.ACCESS_TOKEN,
refresh_token: process.env.REFRESH_TOKEN,
token_type: process.env.TOKEN_TYPE,
expiry_date: parseInt(process.env.EXPIRY_DATE),
};
oAuth2Client.setCredentials(token);
const drive = google.drive({ version: "v3", auth: oAuth2Client });
export const uploadFile = async (name, folderId, body, mimeType) => {
const driveId = process.env.DRIVE_ID;
const fileMetadata = {
name,
parents: [folderId],
driveId,
};
const media = { mimeType, body };
const uploadRes = await drive.files.create(
{
requestBody: fileMetadata,
media: media,
fields: "id",
supportsTeamDrives: true,
},
{}
);
await drive.permissions.create(
{
fileId: uploadRes.data.id,
requestBody: {
role: "reader",
type: "anyone",
},
supportsTeamDrives: true,
},
{}
);
const result = await drive.files.get(
{
fileId: uploadRes.data.id,
fields: "webViewLink, webContentLink",
supportsTeamDrives: true,
},
{}
);
return {
viewUrl: result.data.webViewLink,
downloadUrl: result.data.webContentLink,
};
};