-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
146 lines (128 loc) · 3.98 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
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
const fetch = require('node-fetch');
const download = require('image-downloader');
const moment = require('moment');
const fs = require('fs');
// get todays date and sets it to object passed into this
function setCurrentDate(){
return moment().format('YYYY-MM-DD');
}
// helper function for fetchAPI
async function loadAPIAsync(url, options){
return (await fetch(url, options)).json();
}
// fetches an API and returns mutated object that was passed into this function
async function fetchAPI(data, url, options){
try {
response = await loadAPIAsync(url, options);
data.imageURL = response.assets[0].image_url;
data.title = response.assets[0].name;
data.externalLink = response.assets[0].external_link;
data.description = response.assets[0].description;
data.id = response.assets[0].id;
} catch(e) {
console.error(`Error caught fetching from API: ${e}`);
}
return data;
}
// downloadImageFromURL helper function
async function downloadImageAsync(options){
return (await download.image(options));
}
// image downloader setup and application
async function downloadImageFromURL(url, folderDestination, data){
if(typeof url === ('undefine') || url === ''){
console.error("No URL passed to image download function");
return data;
}
const imageDownloaderOptions = {
url: `${url}`,
dest: folderDestination,
timeout: parseInt(process.env.TIMEOUT, 10)
}
try {
response = await downloadImageAsync(imageDownloaderOptions);
data.imageFileName = `${response.filename}.jpg`;
data.imageFilePath = response.filename;
} catch(e){
console.error(`Error caught while downloading image from URL: ${e}`);
}
return data;
}
// helper function for createMDXFile
async function createFileAsync(destination, content){
return (await fs.writeFile(`${destination}/index.mdx`, content, err => {
if(err) {
console.error(`Error occurred while file was being written in async function: ${err}`);
return;
}
}));
}
// create .mdx file
async function createMDXFile(data, folderDestination){
if(typeof data === ('undefined') || data === {}){
console.error("Data passed to create .mdx file is undefined or empty.");
return data;
}
const mdxFileContent =
`---\n
cover: "./${data.title}"\n
date: "${data.date}"\n
title: "${data.title}"\n
areas:\n
-${data.externalLink}\n
---\n
${data || data.description ? data.description : `No description provided.`}
`;
try {
await createFileAsync(folderDestination, mdxFileContent);
data.mdxFileName = `index.mdx`;
data.mdxFilePath = `${folderDestination}/${data.mdxFileName}`;
} catch(e) {
console.error(`Error caught trying to create .mdx file: ${e}`);
}
return data;
}
// nodemailer function called to send email with career route form
async function sendEMailAsync(data, transporter, callback){
let mailOptions = {
from: process.env.EMAIL,
to: process.env.EMAIL,
subject: "Here is your new NFT post!",
html: `
<div style="margin-left: 1em;">
<h1>Congrats on the new NFT!</h1>
<p style="font-size: 1.15em">Edgar, attached is your NFT image and data for your site</p>
<br>
<p style="font-size: 1.15em">Thank you!</p>
</div>`,
attachments: [
{
filename: data.imageFileName,
path: data.imageFilePath
},
{
filename: data.mdxFileName,
path: data.mdxFilePath
}
]
};
let info = await transporter.sendMail(mailOptions, function(err, info){
if(err){
console.log("Error while sending mail from transporter function: " + err);
} else {
console.log("Message Sent!");
}
});
try {
callback(info)
} catch(e){
console.error(`Error in callback of sending mail function: ${e}`);
}
}
module.exports = {
setCurrentDate: setCurrentDate,
fetchAPI: fetchAPI,
downloadImageFromURL: downloadImageFromURL,
createMDXFile: createMDXFile,
sendEMailAsync: sendEMailAsync
}