-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.api.js
86 lines (76 loc) · 1.78 KB
/
file.api.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
const { google } = require("googleapis"),
fetch = require("node-fetch");
const util = require("./util");
const team_tlvl = util.client.team_tlvl;
const tlvl = util.client.tlvl;
const getDriveFileByID = async (fileId) => {
try {
const { access_token } = await tlvl.authorize();
const url = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
const file = await fetch(url, {
method: "get",
headers: {
Authorization: `Bearer ${access_token}`,
},
});
return file;
} catch (error) {
console.error(error);
}
};
const getAwsFileByURL = async (url) => {
try {
const file = await fetch(url);
return file;
} catch (error) {
console.error(error);
}
};
const getUploadParams = async (input, destination) => {
try {
let file;
if (input[1] === "drive") {
file = await getDriveFileByID(input[2]);
} else {
file = await getAwsFileByURL(input[2]);
}
const params = {
file,
fileName: input[0],
destination,
};
return params;
} catch (error) {
console.error(error);
}
};
const uploadFileToDrive = async ({
file,
fileName,
destination = "13_svHlRFMI2jo5m8Z_jyw5k0RprFlUdE",
}) => {
try {
const fileMetadata = {
name: fileName,
parents: [destination],
};
const media = {
mimeType: file.headers.get("content-type"),
body: file.body,
};
const drive = google.drive({ version: "v3", auth: team_tlvl });
const res = await drive.files.create({
resource: fileMetadata,
media: media,
});
return `https://drive.google.com/file/d/${res.data.id}/view`;
} catch (error) {
console.error(error);
}
};
module.exports = {
getDriveFileByID,
getAwsFileByURL,
getUploadParams,
uploadFileToDrive,
};