-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
executable file
·206 lines (186 loc) · 6.38 KB
/
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const multer = require("multer");
const express = require("express");
const compression = require("compression");
const config = require("./config.json");
const { GoogleSpreadsheet } = require('google-spreadsheet');
const { JWT } = require('google-auth-library');
const NodeClam = require('clamscan');
const path = require('path');
const exif = require("exiftool");
const fs = require("fs");
const bodyParser = require("body-parser");
process.env["NODE_ENV"] = "production";
const ClamScan = new NodeClam().init({
removeInfected: true
});
const serviceAccountAuth = new JWT({
email: config.client_email,
key: config.private_key,
scopes: config.scopes
});
const doc = new GoogleSpreadsheet(config.sheet_id, serviceAccountAuth);
const docLoaded = doc.loadInfo();
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, config.storage);
},
filename: (req, file, cb) => {
const id = Date.now();
let ext = "";
if(file?.originalname) {
ext = path.extname(file.originalname)
}
const fname = `${id}${ext}`;
req.body.id = id;
req.body.ext = ext;
req.body.fname = fname;
cb(null, fname);
}
});
const upload = multer({ storage: storage });
const app = express();
//allow larger files
app.use(bodyParser.json({limit: "10gb"}));
//compress all HTTP responses
app.use(compression());
const server = app.listen(config.port, () => {
console.log(`Server listening on port ${config.port}`);
});
server.requestTimeout = 3000000
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST");
//pass to next layer
next();
});
function getHawaiiISOTimestamp(timestamp) {
let timestampString = ""
if(!timestamp) {
timestamp = new Date();
}
//ensure date object is valid
if(!isNaN(timestamp)) {
//subtract 10 hours so utc converted time will be in hawaii timezone
timestamp.setHours(timestamp.getHours() - 10);
//remove Z from iso string and replace with tz offset
timestampString = timestamp.toISOString().slice(0, -1) + "-10:00";
}
return timestampString;
}
function gpsStrToCoord(gpsStr) {
coord = "";
let coordPattern = /([0-9]+)\s+deg\s+([0-9]+)'\s+([0-9]+(?:.[0-9]+)?)/;
let match = gpsStr.match(coordPattern);
if(match) {
let deg = Number(match[1]);
let min = Number(match[2]);
let sec = Number(match[3]);
latN = deg + (min / 60.0) + (sec / 3600.0);
coord = latN.toString();
}
return coord;
}
function exifDatetimeToHawaiiISOTimestamp(exifTimestamp) {
timestamp = "";
let datetimePattern = /([0-9]{4}):([0-9]{2}):([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})(?:.[0-9]+)?((?:[-+][0-9]{2}:[0-9]{2})|Z)?/;
let match = exifTimestamp.match(datetimePattern);
if(match) {
let year = match[1];
let month = match[2];
let day = match[3];
let hour = match[4];
let min = match[5];
let sec = match[6];
let tz = match[7];
if(!tz) {
tz = "Z";
}
isoTimestamp = `${year}-${month}-${day}T${hour}:${min}:${sec}${tz}`;
timestamp = getHawaiiISOTimestamp(new Date(isoTimestamp));
}
return timestamp;
}
function parseMetadataFields(metadata) {
parsedMetadata = {};
let { gpsLatitude, gpsLongitude, createDate } = metadata;
if(gpsLatitude) {
parsedMetadata.fileLat = gpsStrToCoord(gpsLatitude);
}
if(gpsLongitude) {
parsedMetadata.fileLng = "-" + gpsStrToCoord(gpsLongitude);
}
if(createDate) {
parsedMetadata.fileTimestamp = exifDatetimeToHawaiiISOTimestamp(createDate);
}
return parsedMetadata;
}
app.post("/upload", upload.single("file"), async (req, res) => {
let { id, ext, fname, email, lat, lng, description, userTimestamp } = req.body;
const originalFname = req.file?.originalname;
if(id === undefined) {
id = Date.now();
}
const uploadTimestamp = getHawaiiISOTimestamp();
let metadata = {};
if(fname) {
let fpath = path.join(config.storage, fname);
try {
const clamscan = await ClamScan;
const {isInfected, file, viruses} = await clamscan.isInfected(fpath);
if(isInfected) {
//anything else?
console.log(`${file} is infected with ${viruses}! The file has been removed.`);
return res.status(415)
.send(
"The uploaded file was infected and could not be processed"
);
}
}
//if there was an error scanning the file just accept it
catch(err) {
console.error(`Failed to scan uploaded file ${fpath}. Virus scan failed with error: ${err}`);
}
try {
const imgbuffer = await fs.promises.readFile(fpath);
metadata = await new Promise((resolve, reject) => {
exif.metadata(imgbuffer, (err, metadata) => {
if(err) {
reject(err)
}
else {
//parse metadata to standard json object, stored in strange format that cannot be stringified
let metadataObj = {}
for(let tag in metadata) {
metadataObj[tag] = metadata[tag];
}
resolve(metadataObj);
}
});
});
}
catch(err) {
console.error(`Failed to get exif data for file ${fpath}. Failed with error: ${err}`);
}
}
let parsedFields = parseMetadataFields(metadata);
let { fileLat, fileLng, fileTimestamp } = parsedFields;
await docLoaded;
const sheet = doc.sheetsByIndex[0];
sheet.addRow({
id,
ext,
fname,
originalFname,
email,
userLat: lat,
userLng: lng,
fileLat,
fileLng,
description,
userTimestamp,
fileTimestamp,
uploadTimestamp,
metadata: JSON.stringify(metadata)
});
res.sendStatus(200);
});