-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
113 lines (101 loc) · 3.42 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
const fetch = require("node-fetch");
const nodemailer = require("nodemailer");
const { getFile } = require("./swift");
/**
* Create html body to send in emails
* @param {*} obj Animal or Vehicle object to get info from
* @returns formatted html string email body
*/
const makeBody = async obj => {
let item = { ...obj }; // objects are passed by ref in js so we make a copy
const model = item._doc.make; // get the model name
const locationURL =
"https://www.google.com/maps/search/?api=1&query=" + item._doc.location.lat + "," + item._doc.location.lon;
const article = model === "Animal" ? "An" : "A";
var attachments = [];
const buildImages = async allFiles => {
let images = "";
for (let i = 0; i < allFiles.length; i++) {
let file = allFiles[i];
// get file from swift
var fileArr = file.split("/");
var fileName = fileArr[2];
var prefix = fileArr[0] + "/" + fileArr[1];
// get image and buffer it
const data = await getFile(prefix, fileName);
console.log("data: ", data); // todo: delete
const arrayBuffer = await data.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
attachments.push({
filename: fileName,
content: buffer,
cid: fileArr[1],
});
// add image to html
images += `<img src="cid:${fileArr[1]}" alt="${fileName}"/>`;
}
return images;
};
// return html with image and info
// build htmls images from files
let images = "";
if (item._doc.files) {
images = await buildImages(item._doc.files);
}
var htmlBody = `
<h1>${article} ${model} matching your description has been found with the following details</h1><br>
${images} <br>
<p>Location: <a href="${locationURL}">${item._doc.location.name}</a></p>
`;
return { htmlBody, attachments };
};
/**
* Send email to user
* @param {String} recipient email of the user
* @param {String} subject the email subject
* @param {String} bodyHtml html body
*/
const sendEmail = (recipient, subject, bodyHtml, attachments = {}) => {
const transporter = nodemailer.createTransport({
host: "smtp.isis.vanderbilt.edu",
port: 25,
secure: false,
tls: {
rejectUnauthorized: false, // https://stackoverflow.com/a/46752426/9044659
},
});
transporter.sendMail(
{
from: "noreply-nsf-scc@vanderbilt.edu",
to: recipient,
subject: subject,
html: bodyHtml,
attachments: attachments,
},
function (err) {
if (err) {
console.log(err);
} else {
console.log(`Message sent to ${recipient}.`);
}
}
);
};
/**
* reverse coordinate lookup to figure out an approximate street
* @param {*} lat latitude of coordinate
* @param {*} lon longitude of coordinate
* @returns the street from the lookup
*/
const getLocation = (lat, lon) => {
const url = "https://nominatim.openstreetmap.org/reverse?";
const params = new URLSearchParams({
lat,
lon,
format: "json",
});
return fetch(url + params)
.then(resp => resp.json())
.then(data => data.address.road);
};
module.exports = { getLocation, sendEmail, makeBody };