-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
956 lines (828 loc) · 27.3 KB
/
server.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
const axios = require("axios");
const FormData = require("form-data");
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const multer = require("multer");
const cors = require("cors");
const dotenv = require("dotenv");
const taxonMapper = require("./taxonMapping");
const cron = require("node-cron");
const rateLimit = require("express-rate-limit");
const sanitize = require("sanitize-filename");
const cacheLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // Timeframe
max: 30, // Max requests per timeframe per ip
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
handler: (request, response, next, options) => {
writeErrorLog(
`Too many cache requests`,
`IP ${request.client._peername.address}`
);
return response.status(options.statusCode).send(options.message);
},
});
const idLimiter = rateLimit({
windowMs: 5 * 60 * 1000, // Timeframe
max: 9999, // Max requests per timeframe per ip
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
handler: (request, response, next, options) => {
writeErrorLog(
`Too many ID requests`,
`IP ${request.client._peername.address}`
);
return response.status(options.statusCode).send(options.message);
},
});
const apiLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // Timeframe
max: 30, // Max requests per timeframe per ip
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
handler: (request, response, next, options) => {
writeErrorLog(
`Too many misc API requests`,
`IP ${request.client._peername.address}`
);
return response.status(options.statusCode).send(options.message);
},
});
// Use the crypto library for encryption and decryption
const crypto = require("crypto");
const encryption_algorithm = "aes-256-ctr";
// Generate a secure, pseudo random initialization vector for encryption
const initVect = crypto.randomBytes(16);
let appInsights = require("applicationinsights");
// --- Reading env variables
dotenv.config({ path: "./config/config.env" });
dotenv.config({ path: "./config/secrets.env" });
// --- Setting files and locations
const logdir = "./log";
const taxadir = `${logdir}/taxa`;
const pictureFile = `${logdir}/taxonPictures.json`;
const uploadsdir = "./uploads";
// --- Get the taxon picture ids from file on start
var taxonPics = {};
if (fs.existsSync(pictureFile)) {
taxonPics = JSON.parse(fs.readFileSync(pictureFile));
}
// --- Getting the date as a nice Norwegian-time string no matter where the server runs
const dateStr = (resolution = `d`, date = false) => {
if (!date) {
date = new Date();
}
let iso = date
.toLocaleString("en-CA", { timeZone: "Europe/Oslo", hour12: false })
.replace(", ", "T");
iso = iso.replace("T24", "T00");
iso += "." + date.getMilliseconds().toString().padStart(3, "0");
const lie = new Date(iso + "Z");
const offset = -(lie - date) / 60 / 1000;
if (resolution === `m`) {
return `${new Date(date.getTime() - offset * 60 * 1000)
.toISOString()
.substring(0, 7)}`;
} else if (resolution === `s`) {
return `${new Date(date.getTime() - offset * 60 * 1000)
.toISOString()
.substring(0, 19)
.replace("T", " ")}`;
}
return `${new Date(date.getTime() - offset * 60 * 1000)
.toISOString()
.substring(0, 10)}`;
};
const writeErrorLog = (message, error) => {
if (!!error) {
fs.appendFileSync(
`${logdir}/errorlog_${dateStr(`d`)}.txt`,
`\n${dateStr(`s`)}: ${message}\n ${error}\n`
);
} else {
fs.appendFileSync(
`${logdir}/errorlog_${dateStr(`d`)}.txt`,
`${dateStr(`s`)}: ${message}\n`
);
}
};
// --- Make sure the taxon cache directory exists
if (!fs.existsSync(taxadir)) {
fs.mkdirSync(taxadir);
}
/** Filter for not logging requests for root url when success */
var filteringAiFunction = (envelope, context) => {
if (
envelope.data.baseData.success &&
envelope.data.baseData.name === "GET /"
) {
return false;
}
return true;
};
if (process.env.IKEY) {
appInsights.setup(process.env.IKEY).start();
appInsights.defaultClient.addTelemetryProcessor(filteringAiFunction);
}
const app = express();
const port = process.env.PORT;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var corsOptions = {
origin: "*",
};
app.use(cors(corsOptions));
app.use(function (req, res, next) {
if (req.secure) {
res.setHeader(
"Strict-Transport-Security",
"max-age=31536000; includeSubDomains; preload"
);
}
next();
});
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
let getPicture = (sciName) => {
// Special characters do not work in all cases
sciName = sciName.replaceAll("×", "x").replaceAll("ë", "e");
let pic = taxonPics[sciName];
if (pic) {
return `https://artsdatabanken.no/Media/${pic}?mode=128x128`;
}
return null;
};
let writelog = (req, json) => {
let application;
if (req.body.application) {
application = sanitize(req.body.application);
}
if (!fs.existsSync(`${logdir}/${application}_${dateStr(`d`)}.csv`)) {
fs.appendFileSync(
`${logdir}/${application}_${dateStr(`d`)}.csv`,
"Datetime," +
"Number_of_pictures," +
"Result_1_name,Result_1_group,Result_1_probability," +
"Result_2_name,Result_2_group,Result_2_probability," +
"Result_3_name,Result_3_group,Result_3_probability," +
"Result_4_name,Result_4_group,Result_4_probability," +
"Result_5_name,Result_5_group,Result_5_probability\n"
);
}
// TODO
// Add encrypted IP (req.client._peername.address)
let row = `${dateStr(`s`)},${
Array.isArray(req.files) ? req.files.length : 0
}`;
for (let i = 0; i < json.predictions[0].taxa.items.length; i++) {
const prediction = json.predictions[0].taxa.items[i];
row += `,"${prediction.name}","${prediction.groupName}",${prediction.probability}`;
}
row += "\n";
fs.appendFileSync(`${logdir}/${application}_${dateStr(`d`)}.csv`, row);
};
let getName = async (sciName, force = false) => {
let unencoded_jsonfilename = `${taxadir}/${sanitize(sciName)}.json`;
let jsonfilename = `${taxadir}/${encodeURIComponent(sciName)}.json`;
if (
fs.existsSync(unencoded_jsonfilename) &&
unencoded_jsonfilename !== jsonfilename
) {
fs.unlink(unencoded_jsonfilename, function (error) {
if (error)
writeErrorLog(
`Could not delete "${unencoded_jsonfilename}" while updating old filename`,
error
);
});
}
// --- Return the cached json if it exists, and it parses, and no recache is forced. In all other cases, try to delete that cache.
if (fs.existsSync(jsonfilename)) {
if (!force) {
try {
return JSON.parse(fs.readFileSync(jsonfilename));
} catch (error) {
writeErrorLog(`Could not parse "${jsonfilename}"`, error);
fs.unlink(jsonfilename, function (error) {
if (error)
writeErrorLog(
`Could not delete "${jsonfilename}" after JSON parse failed`,
error
);
});
}
} else {
fs.unlink(jsonfilename, function (error) {
if (error)
writeErrorLog(
`Could not delete "${jsonfilename}" while forcing recache`,
error
);
});
}
}
let nameResult = {
vernacularName: sciName,
groupName: "",
scientificName: sciName,
};
let name;
let retrievedTaxon = { data: [] };
try {
let url = encodeURI(
`https://artsdatabanken.no/api/Resource/?Take=10&Type=taxon&Name=${sciName}`
);
let taxon = await axios
.get(url, {
timeout: 3000,
})
.catch((error) => {
writeErrorLog(
`Failed to ${
!force ? "get info for" : "*recache*"
} ${sciName} from ${url}.`,
error
);
throw "";
});
let acceptedtaxon = taxon.data.find(
(t) => t.Name.includes(sciName) && t.AcceptedNameUsage
);
if (!!acceptedtaxon) {
retrievedTaxon.data = acceptedtaxon;
} else {
let hit = taxon.data.find((t) =>
t.ScientificNames.find((sn) =>
sn.HigherClassification.find((h) => h.ScientificName === sciName)
)
);
if (!hit) throw "No HigherClassification hit";
hit = hit.ScientificNames.find((sn) =>
sn.HigherClassification.find((h) => h.ScientificName === sciName)
);
hit = hit.HigherClassification.find((h) => h.ScientificName === sciName);
hit = hit.ScientificNameId;
url = `https://artsdatabanken.no/api/Resource/ScientificName/${hit}`;
taxon = await axios
.get(url, {
timeout: 3000,
})
.catch((error) => {
writeErrorLog(
`Failed to ${
!force ? "get info for" : "*recache*"
} ${sciName} from ${url}.`,
error
);
throw "";
});
url = `https://artsdatabanken.no/api/Resource/Taxon/${taxon.data.Taxon.TaxonId}`;
taxon = await axios
.get(url, {
timeout: 3000,
})
.catch((error) => {
writeErrorLog(
`Failed to ${
!force ? "get info for" : "*recache*"
} ${sciName} from ${url}.`,
error
);
throw "";
});
retrievedTaxon.data = taxon.data;
}
nameResult.scientificName =
retrievedTaxon.data.AcceptedNameUsage.ScientificName;
nameResult.scientificNameID =
retrievedTaxon.data.AcceptedNameUsage.ScientificNameId;
nameResult.vernacularName =
retrievedTaxon.data["RecommendedVernacularName_nb-NO"] ||
retrievedTaxon.data["RecommendedVernacularName_nn-NO"] ||
nameResult.scientificName ||
sciName;
if (retrievedTaxon.data.Description) {
const description =
retrievedTaxon.data.Description.find(
(desc) =>
desc.Language == "nb" ||
desc.Language == "no" ||
desc.Language == "nn"
) || retrievedTaxon.data.Description[0];
nameResult.infoUrl = description.Id.replace(
"Nodes/",
"https://artsdatabanken.no/Pages/"
);
} else {
nameResult.infoUrl =
"https://artsdatabanken.no/" + retrievedTaxon.data.Id;
}
url = encodeURI(`https://artsdatabanken.no/Api/${retrievedTaxon.data.Id}`);
name = await axios
.get(url, {
timeout: 3000,
})
.catch((error) => {
writeErrorLog(
`Failed to ${
!force ? "get info for" : "*recache*"
} ${sciName} from ${url}.`,
error
);
throw "";
});
} catch (error) {
writeErrorLog(
`Error in getName(${sciName}). Retry: ${encodeURI(
"https://ai.test.artsdatabanken.no/cachetaxon/" + sciName
)}.`,
error
);
return nameResult;
}
if (name && name.data.AcceptedName.dynamicProperties) {
let artsobsname = name.data.AcceptedName.dynamicProperties.find(
(dp) =>
dp.Name === "GruppeNavn" &&
dp.Properties.find((p) => p.Value === "Artsobservasjoner")
);
if (artsobsname && artsobsname.Value) {
nameResult.groupName = artsobsname.Value;
}
}
if (force || !fs.existsSync(jsonfilename)) {
let data = JSON.stringify(nameResult);
fs.writeFileSync(jsonfilename, data);
}
return nameResult;
};
// Check if there are old files to be deleted every X minute:
cron.schedule("30 * * * *", () => {
//console.log('Running cleanup every 30th minute');
// Loop over all files in uploads/
fs.readdir(`${uploadsdir}/`, (err, files) => {
if (files) {
files.forEach((file) => {
// gets timestamp from filename
let filename = file.split("_")[1];
// gets current timestamp
let timestamp = Math.round(new Date().getTime() / 1000);
// Check timestamp vs. time now
let time_between = timestamp - filename;
// Image Survival length, if change this - ensure to change in artsobs-mobile too...
let survival_length = 3600; // 1 hr in seconds
// If more than survival_length
if (time_between >= survival_length) {
// Delete the file
fs.unlink(`${uploadsdir}/${file}`, (err) => {
if (err) {
console.log("could not delete file");
}
console.log("The file has been deleted!");
});
}
});
}
});
});
function encrypt(file, password) {
// Create a new cipher using the algorithm, key, and initVect
const cipher = crypto.createCipheriv(
encryption_algorithm,
password,
initVect
);
// file is already a string - base64
const encrypted = Buffer.concat([cipher.update(file), cipher.final()]);
return encrypted;
}
const decrypt = (encrypted_content, password) => {
// Use the same things to create the decipher vector
const decipher = crypto.createDecipheriv(
encryption_algorithm,
password,
initVect
);
// Apply the deciphering
const decrypted = Buffer.concat([
decipher.update(encrypted_content),
decipher.final(),
]);
return decrypted.toString();
};
function makeRandomHash() {
// TODO check that this is not used. To do this, loop over uploads folder
// It would be shocking if it is used considering we use the current date as input, and
// clean out images every 30 minutes. But you never know.
let current_date = new Date().valueOf().toString();
let random = Math.random().toString();
return crypto
.createHash("sha1")
.update(current_date + random)
.digest("hex");
}
let saveImagesAndGetToken = async (req) => {
// Create random, unused id & password, password must be a certain length
let id = makeRandomHash();
let password = makeRandomHash().substring(0, 32);
let counter = 0;
for (let image of req.files) {
let timestamp = Math.round(new Date().getTime() / 1000);
// Turn image into base64 to allow both encryption and future transfer
let base64image = image.buffer.toString("base64");
// Perform encryption
let encrypted_file = encrypt(base64image, password);
// Save encrypted file to disk and put id & date (unix timestamp) in filename
let filename = id + "_" + counter + "_" + timestamp + "_";
// ensure uniqueness in case the other factors end up the same (unlikely)
counter += 1;
// Upload to uploads folder
fs.writeFile(`${uploadsdir}/${filename}`, encrypted_file, (error) => {
if (error) {
writeErrorLog(
`Failed to write file "${uploadsdir}/${filename}".`,
error
);
}
console.log("The file has been saved!");
});
}
return { id: id, password: password };
};
let simplifyJson = (json) => {
if (json.predictions[0].taxa) {
json.predictions = json.predictions[0].taxa.items.map((p) => {
let simplified = {
probability: p.probability,
taxon: p,
};
simplified.taxon.probability = undefined;
return simplified;
});
}
return json;
};
let refreshtaxonimages = async () => {
const pages = [342548, 342550, 342551, 342552, 342553, 342554];
let taxa = {};
for (let index = 0; index < pages.length; index++) {
let pageId = pages[index];
let url = encodeURI(`https://www.artsdatabanken.no/api/Content/${pageId}`);
let page = await axios
.get(url, {
timeout: 10000,
})
.catch((error) => {
writeErrorLog(
`Error getting "${url}" while running refreshtaxonimages`,
error
);
throw "";
});
if (!!page) {
page.data.Files.forEach((f) => {
// Unpublished files have no FileUrl
if (f.FileUrl) {
let name = f.Title.split(".")[0].replaceAll("_", " ");
let value = f.Id.split("/")[1];
taxa[name] = value;
}
});
}
}
taxonPics = taxa;
fs.writeFileSync(pictureFile, JSON.stringify(taxa));
return Object.keys(taxa).length;
};
let getId = async (req) => {
try {
const form = new FormData();
const formHeaders = form.getHeaders();
const receivedParams = Object.keys(req.body);
receivedParams.forEach((key, index) => {
form.append(key, req.body[key]);
});
var stream = require("stream");
for (const file of req.files) {
var bufferStream = new stream.PassThrough();
bufferStream.end(file.buffer);
form.append("image", bufferStream, {
filename: "" + Date.now() + "." + file.mimetype.split("image/").pop(),
});
}
let token;
if (
receivedParams.model &&
receivedParams.model.toLowerCase() === "global"
) {
token = process.env.SH_TOKEN; // Shared token
} else {
token = process.env.SP_TOKEN; // Specialized (Norwegian) token
}
let recognition;
recognition = await axios
.post(
`https://multi-source.identify.biodiversityanalysis.eu/v2/observation/identify/token/${token}`,
form,
{
headers: {
...formHeaders,
},
auth: {
username: process.env.NATURALIS_USERNAME,
password: process.env.NATURALIS_PASSWORD,
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
}
)
.catch((error) => {
writeErrorLog(
`Naturalis API v2 lookup with token ${token} failed`,
error
);
throw "";
});
if (
!recognition.data.predictions[0].taxa ||
!recognition.data.predictions[0].taxa.items
) {
throw `Naturalis API v2 lookup gave no predictions.\n${JSON.stringify(
recognition.data
)}`;
}
let taxa = recognition.data.predictions[0].taxa.items;
// get the best 5
taxa = taxa.slice(0, 5);
filteredTaxa = taxa.filter((taxon) => taxon.probability >= 0.02);
if (filteredTaxa.length) {
taxa = filteredTaxa;
} else {
taxa = taxa.slice(0, 2);
}
// Check against list of misspellings and unknown synonyms
taxa = taxa.map((pred) => {
pred.scientific_name =
taxonMapper.taxa[pred.scientific_name] || pred.scientific_name;
return pred;
});
// Get the data from the APIs (including accepted names of synonyms)
for (let pred of taxa) {
try {
let nameResult;
if (
req.body.application &&
req.body.application.toLowerCase() === "artsobservasjoner"
) {
pred.name = pred.scientific_name;
} else {
nameResult = await getName(pred.scientific_name);
pred.vernacularName = nameResult.vernacularName;
pred.groupName = nameResult.groupName;
pred.scientificNameID = nameResult.scientificNameID;
pred.name = nameResult.scientificName;
pred.infoUrl = nameResult.infoUrl;
}
pred.picture = getPicture(pred.scientific_name);
} catch (error) {
writeErrorLog(
`Error while processing getName(${
pred.scientific_name
}). You can force a recache on ${encodeURI(
"https://ai.test.artsdatabanken.no/cachetaxon/" +
pred.scientific_name
)}.`,
error
);
}
}
recognition.data.predictions[0].taxa.items = taxa;
// -------------- Code that checks for duplicates, that may come from synonyms as well as accepted names being used
// One known case: Speyeria aglaja (as Speyeria aglaia) and Argynnis aglaja
// if there are duplicates, add the probabilities and delete the duplicates
// for (let pred of recognition.data.predictions) {
// let totalProbability = recognition.data.predictions
// .filter((p) => p.name === pred.name)
// .reduce((total, p) => total + p.probability, 0);
// if (totalProbability !== pred.probability) {
// pred.probability = totalProbability;
// recognition.data.predictions = recognition.data.predictions.filter(
// (p) => p.name !== pred.name
// );
// recognition.data.predictions.unshift(pred);
// }
// }
// // sort by the new probabilities
// recognition.data.predictions = recognition.data.predictions.sort((a, b) => {
// return b.probability - a.probability;
// });
// -------------- end of duplicate checking code
recognition.data.application = req.body.application;
return recognition.data;
} catch (error) {
throw error;
}
};
app.get("/taxonimage/*", apiLimiter, (req, res) => {
try {
let taxon = decodeURI(req.originalUrl.replace("/taxonimage/", ""));
res.status(200).send(getPicture(taxon));
} catch (error) {
writeErrorLog(`Error for ${req.originalUrl}`, error);
res.status(500).end();
}
});
app.get("/taxonimages", apiLimiter, (req, res) => {
try {
res.status(200).json(taxonPics);
} catch (error) {
writeErrorLog(`Error for ${req.originalUrl}`, error);
res.status(500).end();
}
});
app.get("/taxonimages/view", apiLimiter, (req, res) => {
try {
let pics = Object.entries(taxonPics);
pics.sort();
let html = "<html><head><style>";
html += "img {border-radius: 50%}";
html += "img:hover {border-radius: 0}";
html += "</style></head><body>";
html += `<h1>Alle ${pics.length} "profilbilder"</h1>`;
html += "<table>";
pics.forEach((pic) => {
html += `<tr><td style="padding: 20px"><a href="https://artsdatabanken.no/Media/${pic[1]}" target="_blank"><img src="https://artsdatabanken.no/Media/${pic[1]}?mode=128x128"/></a></td>`;
html += `<td><h3><i>${pic[0]}</i></h3></td></tr>`;
});
html += "</body></html>";
res.status(200).send(html);
} catch (error) {
writeErrorLog(`Error for ${req.originalUrl}`, error);
res.status(500).end();
}
});
app.get("/cachetaxon/*", cacheLimiter, async (req, res) => {
try {
let taxon = decodeURI(req.originalUrl.replace("/cachetaxon/", ""));
let name = await getName(taxon, (force = true));
res.status(200).json(name);
} catch (error) {
writeErrorLog(`Error for ${req.originalUrl}`, error);
res.status(500).end();
}
});
app.get("/refreshtaxonimages", cacheLimiter, async (req, res) => {
try {
// Read the file first in case the fetches fail, so it can still be uploaded manually
if (fs.existsSync(pictureFile)) {
taxonPics = JSON.parse(fs.readFileSync(pictureFile));
}
let number = await refreshtaxonimages();
res.status(200).send(`${number} pictures found`);
} catch (error) {
res.status(500).end();
}
});
app.post("/", idLimiter, upload.array("image"), async (req, res) => {
// Future simple token check
// if (req.headers["authorization"] !== `Bearer ${process.env.AI_TOKEN}`) {
// res.status(401).end("Unauthorized");
// return true;
// }
try {
json = await getId(req);
// Write to the log
writelog(req, json);
if (req.body.application === undefined) {
json = simplifyJson(json);
json.predictions = [{}].concat(json.predictions);
}
json.predictions[0].probability = 1;
json.predictions[0].taxon = {
vernacularName: "*** Utdatert versjon ***",
name: "Vennligst oppdater Artsorakelet via app store, eller Ctrl-Shift-R på pc",
};
res.status(200).json(json);
// --- Now that the reply has been sent, let each returned name have a 5% chance to be recached if its file is older than 10 days
if (json.predictions[0].taxa) {
json.predictions[0].taxa.items.forEach((taxon) => {
if (Math.random() < 0.05) {
let filename = `${taxadir}/${encodeURIComponent(
taxon.scientific_name
)}.json`;
if (fs.existsSync(filename)) {
fs.stat(filename, function (err, stats) {
if ((new Date() - stats.mtime) / (1000 * 60 * 60 * 24) > 10) {
getName(taxon.scientific_name, (force = true));
}
});
}
}
});
}
} catch (error) {
writeErrorLog(`Error while running getId()`, error);
res.status(500).end();
}
});
app.post("/save", apiLimiter, upload.array("image"), async (req, res) => {
// image saving request from the orakel service
try {
json = await saveImagesAndGetToken(req);
res.status(200).json(json);
} catch (error) {
writeErrorLog(`Failed to save image(s)`, error);
}
});
app.get("/", apiLimiter, (req, res) => {
let v = "Gitless";
const gitfile = ".git/FETCH_HEAD";
if (fs.existsSync(gitfile)) {
v = fs.readFileSync(gitfile).toString().split("\t")[0];
}
fs.stat("./server.js", function (err, stats) {
res
.status(200)
.send(`<h3>Aiaiai!</h3><hr/> ${v}<br/>${dateStr("s", stats.mtime)}`);
});
});
app.get("/image/*", apiLimiter, (req, res) => {
// image request from the orakel service
// On the form /image/id&password
// Url used to arrive here from outside
let url = req.originalUrl.replace("/image/", "");
// Obtain password from the end of the url
let password = url.split("&")[1].toString();
// Obtain the image id's from the url
url = url.split("&")[0];
// Loop over all files in uploads/
fs.readdir(`${uploadsdir}/`, (err, files) => {
let image_list = [];
files.forEach((file) => {
// The id's in upload are of the format:
// sessionid_number_timestamp this to ensure unique id's
// to get all entries from one session, we use only the first of these
const fileid = file.split("_")[0];
if (fileid === url) {
// If the request has a match in the database (it should unless the user was too slow)
const image_to_fetch = `${uploadsdir}/${file}`;
// read the file
const file_buffer = fs.readFileSync(image_to_fetch);
// decrypt the file
let decrypted_file = decrypt(file_buffer, password);
// add the file to the return list
image_list.push(decrypted_file);
}
});
// generate json object to return at request
let json = { image: image_list };
try {
res.status(200).json(json);
} catch (error) {
writeErrorLog(
`Failed to return json of saved images:\n${filelist.toString()}`,
error
);
res.status(500).end();
}
});
});
app.get("/loglist/*", cacheLimiter, (req, res) => {
const token = process.env.SP_TOKEN;
let requestToken = req.originalUrl.replace("/loglist/", "");
if (requestToken !== token) {
res.status(403).send(`Nope`);
} else {
var json = [];
fs.readdir("./log", function (err, files) {
files.forEach(function (file, index) {
json.push(file);
});
res.status(200).json(json);
});
}
});
app.get("/getlog/*", idLimiter, (req, res) => {
const token = process.env.SP_TOKEN;
let [requestToken, filename] = req.originalUrl
.replace("/getlog/", "")
.split("/");
if (requestToken !== token) {
res.status(403).end();
} else {
const file = `./log/${decodeURI(filename)}`;
if (fs.existsSync(file)) {
res.download(file);
} else {
res.status(404).end();
}
}
});
// --- Path that Azure uses to check health, prevents 404 in the logs
app.get("/robots933456.txt", apiLimiter, (req, res) => {
res.status(200).send("Hi, Azure");
});
// --- Serve a favicon, prevents 404 in the logs
app.use("/favicon.ico", apiLimiter, express.static("favicon.ico"));
app.listen(port, console.log(`Server now running on port ${port}`));