generated from GDGAlgiers/discord.js-bot-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
105 lines (98 loc) · 2.76 KB
/
utils.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
const { google } = require("googleapis");
const PizZip = require("pizzip");
const Docxtemplater = require("docxtemplater");
const path = require("path");
const fs = require("fs");
const dotenv = require("dotenv").config();
const filesContent = require("./src/template/files_content.js");
const oauth2Client = new google.auth.OAuth2(
process.env.GOOGLE_DRIVE_CLIENT_ID,
process.env.GOOGLE_DRIVE_CLIENT_SECRET,
process.env.GOOGLE_DRIVE_REDIRECT_URI
);
oauth2Client.setCredentials({
refresh_token: process.env.GOOGLE_DRIVE_REFRESH_TOKEN,
});
const drive = google.drive({
version: "v3",
auth: oauth2Client,
});
async function uploadFile(pv_name) {
const filePath = path.join("./", `${pv_name}.docx`);
try {
const response = await drive.files.create({
requestBody: {
name: `${pv_name}.docx`,
parents: [process.env.FOLDER_ID],
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
media: {
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
body: fs.createReadStream(filePath),
},
});
return response.data;
} catch (error) {
console.log(error);
}
}
async function generateUrl(fileId) {
try {
await drive.permissions.create({
fileId: fileId,
requestBody: {
role: "writer",
type: "anyone",
},
});
const result = await drive.files.get({
fileId: fileId,
fields: "webViewLink, webContentLink",
});
return result.data;
} catch (error) {
console.log(error.message);
}
}
const read_files = () => {
try {
return JSON.parse(fs.readFileSync("./pv_files.json"));
} catch (err) {
console.log(err);
return;
}
};
const write_files = (data) => {
try {
fs.writeFileSync("./pv_files.json", JSON.stringify(data, null, 2));
} catch (err) {
console.log(err);
return;
}
};
function renderFile(fileName) {
//load the template file
const template = fs.readFileSync(
path.resolve(__dirname, "src/template/Pv_template.docx"),
"binary"
);
const zip = new PizZip(template);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
linebreaks: true,
});
doc.render(filesContent[fileName]);
const buf = doc.getZip().generate({
type: "nodebuffer",
compression: "DEFLATE",
});
//generating the file
fs.writeFileSync(path.resolve(__dirname, `${fileName}.docx`), buf);
}
module.exports = {
uploadFile,
read_files,
write_files,
generateUrl,
renderFile,
};