This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
urls.js
797 lines (756 loc) · 35.3 KB
/
urls.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
const util = require('util');
const fs = require('fs');
//////////////////////////////////
const express = require('express');
const Mustache = require('mustache');
const i18next = require('i18next');
const request = require('request');
// Custom functions for internationalization
const i18n_utils = require('./i18n/utils');
const {migrate, connection, Map, History, Category} = require("./db/modelsB.js");
const query = require("./db/query.js");
const dbapi = require("./db/api.js");
const sharp = require('sharp');
// Local units
const wizard = require('./units/wizard');
const data = require('./units/data');
const dbutils = require('./units/dbutils.js');
const templateutils = require('./units/templateutils.js');
const mail = require('./units/mail');
// Global settings
const config = require('./config');
const url = require('url');
const { logger } = require('./units/logger');
// load local config and check if is ok (testing db)
const localconfig = require('./localconfig');
const bodyParser = require('body-parser');
module.exports = function(app, apicache) {
function generateMapPage (req, res, dbMap, isHistory) {
/**
* Detect user language and serve the page accordingly
* @param {object} req: request to use to find user language
* @param {object} res: express response
* @param {object} dbMap: dictionary containing all relevant Map columns, used for template (only on aliased version)
**/
fs.readFile(util.format('%s/public/frontend/map.mustache', __dirname), function (err, fileData) {
if (err) {
throw err;
}
// get template content, server-side
let template = fileData.toString();
let [shortlang, translationData] = i18n_utils.seekLang(req, config.fallbackLanguage, 'frontend');
let i18nOptions = i18n_utils.geti18nOptions(shortlang);
i18nOptions.resources[shortlang] = {translation: translationData};
// console.log(i18nOptions);
// load i18n
i18next.init(i18nOptions, function(err, t) {
// i18n initialized and ready to go!
// document.getElementById('output').innerHTML = i18next.t('key');
// variables to pass to Mustache to populate template
var view = {
shortlang: shortlang,
langname: i18n_utils.getLangName(config.languages, shortlang),
map: config.map,
logo: templateutils.logo(),
languages: config.languages,
devMode: localconfig.devMode,
showHistory: dbMap.history,
isHistory: isHistory, // serve history or current page?
dbMap: dbMap,
// queryJsonStr: JSON.stringify(req.query, null, ' '),
i18n: function () {
return function (text, render) {
i18next.changeLanguage(shortlang);
return i18next.t(text);
}
}
};
// console.log(view);
const partials = {
urlqueryargs: templateutils.retainUrlQueryArguments(req)
};
var output = Mustache.render(template, view, partials);
res.send(output);
});
});
}
/**
* Return exposable fields from database.
* @param {Object} e JavaScript object from JSON
* @return {Object} JavaScript object of derived fields.
*/
function exposeMap(e) { // TODO legacy, to drop, replaced by dbutils.getCategoryWithMapsAsDict()
let tmpUrl = new URL(e.mapargs, 'http://localhost');
return {
href: util.format(config.mapPattern, e.path),
title: e.title,
icon: tmpUrl.searchParams.get("pinIcon"), // get pin icon classes
screenshot: util.format(config.screenshot.urlPattern, e.screenshot.split('/').pop()),
star: false // legacy favourite
};
}
// TODO: reuse wizard.getMapValues ??
/**
* Convert querystring ?q=ENCODED into an options JSON
* @param {Express response} res
* @param {string} enrichedQuery SPARQL query
* @return An Express send with the response will be sent.
*/
function querystring2json (res, enrichedQuery) {
dbutils.booleanize(enrichedQuery);
// find requested style in config styles list
const style = config.map.styles.find(el => el.tile === enrichedQuery.tile);
// if found add style to query otherwise add fallback style
enrichedQuery.currentStyle = style ? style : config.map.fallBackStyle;
// TODO - check if this property is not used anymore and remove
enrichedQuery.tile = enrichedQuery.currentStyle.tile;
enrichedQuery["languagechoices"] = typeof enrichedQuery["languagechoices"] !== "undefined" ? enrichedQuery["languagechoices"] : config.defaultLanguageChoices;
// stringify rend response
res.send(JSON.stringify(enrichedQuery, null, ''));
}
// javascript for wizard frontend
app.use('/wizard/js',express.static('./public/wizard/js'));
// javascript for frontend
app.use('/frontend/js',express.static('./public/frontend/js'));
// js for manual
app.use('/manual/js',express.static('./public/manual/js'));
// images for home page
app.use('/p/',express.static('./screenshots'));
// translated interfaces for the map wizard
// do not cache (multilingual)
app.get('/admin/:action/:id/save', async function (req, res) {
/** Save changes of existing table to database (edit / update) **/
wizard.cuMap(req, res, 'edit');
});
app.get('/admin/:action/:id', async function (req, res) {
wizard.getWizardPath(req, res, req.params.action, parseInt(req.params.id))
});
app.get('/admin/:action', async function (req, res) {
wizard.getWizardPath(req, res, req.params.action)
});
// plain wizard must be the last WIZARD route
app.get('/wizard', async function (req, res) {
// wizard without action (e.g. wizard/edit) is equivalent to wizard/add
wizard.getWizard(req, res, 'add');
});
// full url map route, with exposed parameters
app.get('/m', function (req, res) {
// temporary url, do not pass dbMap
generateMapPage(req, res, {}, false);
});
// convert exposed parameters to JSON to be served in /m route
app.get('/a', async function (req, res) {
var enrichedQuery = req.query;
querystring2json(res, enrichedQuery);
});
// convert short url (path) to JSON to be served in /m route
app.get('/s/:path', function (req, res) {
Map.findOne({
where: {
path: req.params.path,
published: true
}
}).then(record => {
if (record) {
let enrichedQuery = dbutils.mapargsParse(record);
// add id from database, for check on Edit on public-wizard.js
enrichedQuery.id = record.id;
// serve json, enriched with config
querystring2json(res, enrichedQuery);
}
else {
res.status(400).send('Bad request');
}
});
});
/**
* Save the map to database (add)
* @param {Express request} req
* @param {Express response} res
* @return An Express send with a redirect to new map (or error message) will be sent.
*/
app.get('/wizard/generate', async function (req, res) {
wizard.cuMap(req, res, 'add');
});
/**
* Get an array of timestamps for the given map.
*/
app.get('/api/timestamp', apicache('12 hours'), function (req, res) {
query.historiesTimestamps(req.query.id, localconfig.historyOnlyDiff).then(hists => {
const timestamp = [];
for (let hist of hists) {
timestamp.push(new Date(hist.createdAt).getTime());
}
res.send(timestamp);
}).catch(err => {
res.status(400).send(err);
});
});
/**
* GeoJSON Features from Wikidata, with properties.time to be consumed
* by Leaflet TimeDimension to display a timeline.
* @param {Express request} req
* @param {Express response} res
* @return {GeoJSON string} send JSON of past results merged with direct
* Wikidata query results, inverse order (direct query before, past after)
*/
app.get('/api/timedata', apicache('12 hours'), async function (req, res) {
// all results from all timeshot and real-time, merged together
let sparqlResultsArray = [];
let sparqlResultsFirstShotArray = [];
let sparqlResultsChangedDuplicatesArray = [];
// let now = parseInt(Math.round(new Date().getTime() / 1000));
let now = new Date().getTime();
logger.debug(now);
// Extract past results from History
let sparqlJsonResultsArray = []; // all results
// make query for old results on History
query.timedata(req.query.id, localconfig.historyTimelineLimit, req.query.timestamp).then(async hists => {
/**
* Get only the times an element isn't changed.
* It's used to avoid duplicates pin when element change and to display
* correct counters.
* @param {Object} el Object from JSON, to get Wikidata id
* @param {[type]} changedTimes [description]
* @param {[type]} allTimes [description]
* @return {Array} array of unchanged times
*/
function getTimesBeforeFirstChange (el, changedTimes, allTimes) {
// previously stored changed times
let elChangedTimes = changedTimes[el.properties.wikidata];
let unchangedTimes = [];
if (typeof elChangedTimes !== 'undefined') {
let allTimeEndInd = allTimes.indexOf(elChangedTimes[0]);
unchangedTimes = allTimes.slice(0, allTimeEndInd);
}
else {
// never changed
unchangedTimes = allTimes;
}
// DEBUG
// if (process.env.DEBUG && elChangedTimes && el.properties.wikidata === 'Q3825655') {
// console.log('allTimes', allTimes);
// console.log('elChangedTimes', elChangedTimes);
// console.log('unchangedTimes', unchangedTimes);
// }
return unchangedTimes;
}
/**
* Get dates where a GeoJSON record will be available as circle,
* after it was displayed as pin.
* @param {Object} el Object from JSON element
* @param {Array} changedTimes Array of milliseconds integers
* about when the object is changed in timeshots
* @param {Array} allTimes all times from History
* @return {Array} Array populated with milliseconds
* where a circle will be displayed.
* Empty array if element will not be displayed as circle.
*/
function getOnlyNextTimes(el, changedTimes, allTimes) {
// when new element should be available as Circle on map?
let allTimeStartInd = 1 + allTimes.indexOf(el.properties.times[0]);
if (typeof allTimes[allTimeStartInd] !== 'undefined') {
// get all times the element is changed
let elChangedTimes = changedTimes[el.properties.wikidata];
// get the index inside the changedTimes array
let changeTimeInd = elChangedTimes.indexOf(el.properties.times[0]);
// get the next changed element time value in milliseconds
let changeTimeNext = elChangedTimes[changeTimeInd + 1];
// calculate the last available time for this item
// to be displayed as circle
let allTimeEndInd = typeof changeTimeNext !== 'undefined'
// If out of array, slice returns
// an empty array
? allTimes.indexOf(changeTimeNext)
// never changed after, keep all records
// from allTimeStartInd on
: allTimes.length;
// DEBUG
// if (el.properties.wikidata === 'Q3825655') {
// console.log('allTimes', allTimes);
// console.log('elChangedTimes', elChangedTimes);
// console.log('changeTimeNext', changeTimeNext);
// console.log('allTimeStartInd', allTimeStartInd);
// console.log('allTimeEndInd', allTimeEndInd);
// console.log('slice: ', allTimes.slice(allTimeStartInd, allTimeEndInd));
// }
return allTimes.slice(allTimeStartInd, allTimeEndInd);
}
else {
// never available, is already the last element
return [];
}
}
// store all timeshot date from all Histories for this map
let allTimes = [];
let changedTimes = {};
// copy first valid timeshot History
let n;
for (n = 0; n < hists.length; n++) {
if (!hists[n].error) {
sparqlResultsFirstShotArray = JSON.parse(hists[n].json).data;
console.log('First timeshot is id: ' + hists[n].id);
break;
}
}
// get only differences between sparqlResultsFirstShotArray and other Histories
if (hists.slice(n+1)) {
// Past timeshots //////////////////////////////////////////////
for (histInd in hists.slice(n+1)) {
let hist = hists[histInd];
logger.debug('id', hist.id, 'mapId', hist.mapId, 'published', hist.map.published, 'histInd (array)', histInd); // DEBUG
// Ignore broken records, but keep them in History table
if (!hist.error) {
let localRes = JSON.parse(hist.json);
let localResData = localRes.data;
// times accepts a) An array of times (in milliseconds)
// @see https://github.com/socib/Leaflet.TimeDimension#options
let thisHistoryTime = new Date(hist.createdAt).getTime();
for (elkInd in localResData) {
let elk = localResData[elkInd];
// add only if is changed compared to before record OR is first History element (to use with intersect / union on TimeDimension)
if (elk.hasOwnProperty('postProcess')) {
elk.properties.times = [thisHistoryTime];
sparqlResultsArray.push(elk);
// save a map with wikidata id and changed times for later use
if (typeof changedTimes[elk.properties.wikidata] === 'undefined') {
changedTimes[elk.properties.wikidata] = [];
}
changedTimes[elk.properties.wikidata].push(thisHistoryTime);
}
// add to global time array when time is different from previous record
if (!allTimes.length || thisHistoryTime !== allTimes[allTimes.length - 1]) {
// run only on first element of histories
allTimes.push(thisHistoryTime);
}
}
}
}
}
// now as last time in array
// allTimes.push(now);
// first results: assign times only if are unchanged on all timeshots
for (fstelInd in sparqlResultsFirstShotArray) {
// el.properties.time = now;
sparqlResultsFirstShotArray[fstelInd].properties.times = getTimesBeforeFirstChange(sparqlResultsFirstShotArray[fstelInd], changedTimes, allTimes);
}
// create an array with old items changed at least one time, to be displayed as circle
// TODO:
for (htel of sparqlResultsArray) {
let newObj = JSON.parse(JSON.stringify(htel));
// display as a circle, removing postProcess
delete newObj.postProcess;
// get times after the change and create a new record with it
newObj.properties.times = getOnlyNextTimes(htel, changedTimes, allTimes);
// it should be available in the future? If no times specified, no (last element of timeshots)
if (newObj.properties.times.length) {
sparqlResultsChangedDuplicatesArray.push(newObj);
}
}
// output: First results [circle], all results of changed elements first time changed [pin], changed in the past [circle]
res.send(sparqlResultsFirstShotArray.concat(sparqlResultsArray).concat(sparqlResultsChangedDuplicatesArray));
});
});
const sparqlQuery2response = async (sparql, res) => {
let sparqlJsonResult = await data.getJSONfromQuery(sparql, "urls.js");
if (sparqlJsonResult.error) {
// error
console.log('error:', sparqlJsonResult.errormsg); // Print the error if one occurred
res.status(sparqlJsonResult.errorcode).send(sparqlJsonResult.errormsg);
}
else {
res.send(sparqlJsonResult.data);
}
};
/**
*
* Do not cache.
*
* @param {Express request} req
* @param {Express response} res
* @return {GeoJSON string}
*/
app.get('/api/dataid/:id', async function (req, res) {
if (req.params.id) {
const map = await Map.findOne({
where: { id: req.params.id },
});
if (map) {
let ob = dbutils.mapargsParse(map);
const result = await data.getJSONfromUnencodedQueryLocally(ob.query, res);
res.send(result);
} else {
// Requested an invalid id
res.status(400).send("Invalid id");
return;
}
} else {
res.status(400).send("Invalid query");
return;
}
});
/**
* Almost real-time results.
*
* Always cache (SPARQL query based).
*
* @param {Express request} req
* @param {Express response} res
* @return {GeoJSON string}
*/
app.get('/api/data', apicache('12 hours'), async function (req, res) {
if (req.query.q) {
await sparqlQuery2response(req.query.q, res);
} else {
res.status(400).send("Invalid query");
return;
}
});
// serve JS common to frontend and backend
app.use('/themes/wmch-interactive-maps-v3-theme/css/',express.static('./themes/wmch-interactive-maps-v3-theme/css'));
app.use('/themes/wmch-interactive-maps-v3-theme/js/',express.static('./themes/wmch-interactive-maps-v3-theme/js'));
app.use('/css/',express.static('./public/css'));
app.use('/images/',express.static('./public/images'));
app.use('/js/',express.static('./public/js'));
app.use('/fonts/',express.static('./public/fonts'));
/**
* Display Landing page
* @param {Express request} req
* @param {Express response} res
* @return Express send with HTML.
*/
app.get('/', async function (req, res) {
fs.readFile(util.format('%s/public/frontend/index.mustache', __dirname), function (err, fileData) {
if (err) {
throw err;
}
// get template content, server-side
let template = fileData.toString();
let [shortlang, translationData] = i18n_utils.seekLang(req, config.fallbackLanguage, 'frontend');
let i18nOptions = i18n_utils.geti18nOptions(shortlang);
i18nOptions.resources[shortlang] = {translation: translationData};
// console.log(i18nOptions);
// load i18n
i18next.init(i18nOptions, async function(err, t) {
// i18n initialized and ready to go!
// document.getElementById('output').innerHTML = i18next.t('key');
// load categories
const categoriesWithPublishedMaps = await query.categoriesWithPublishedMaps();
// load published maps data
// variables to pass to Mustache to populate template
var view = {
shortlang: shortlang,
logo: templateutils.logo(),
categories: categoriesWithPublishedMaps
.map(category => dbutils.getCategoryWithMapsAsDict(category))
.map(categoryWithMaps => {
categoryWithMaps.more = categoryWithMaps.maps.length > config.seeAll.rowlength;
categoryWithMaps.maps = categoryWithMaps.maps.map((map, mapInd) => {
map.seeAllHidden = mapInd >= config.seeAll.rowlength
map.uicon = map.icon.split(' ').map(name => `sui-${name}`).join(' ')
return map
});
return categoryWithMaps;
}),
langname: i18n_utils.getLangName(config.languages, shortlang),
baseurl: localconfig.url + "/",
languages: config.languages,
devMode: localconfig.devMode,
// TODO add maps
author: config.map.author,
i18n: function () {
return function (text, render) {
i18next.changeLanguage(shortlang);
return i18next.t(text);
}
}
};
const menuTemplate = await templateutils.readMustachePartials('public/frontend/menu.mustache');
const partials = {
menu: menuTemplate,
urlqueryargs: templateutils.retainUrlQueryArguments(req),
card: await templateutils.readMustachePartials('public/frontend/card.mustache')
};
var output = Mustache.render(template, view, partials);
res.send(output);
});
});
});
app.use('/wizard/man/_media/',express.static('./i18n_man/_media/'));
app.get('/wizard/man/:manpage', function (req, res) {
fs.readFile(util.format('%s/public/manual/manual.mustache', __dirname), function (err, fileData) {
if (err) {
throw err;
}
// get template content, server-side
let template = fileData.toString();
let [shortlang, translationData] = i18n_utils.seekLang(req, config.fallbackLanguage, 'manual');
let i18nOptions = i18n_utils.geti18nOptions(shortlang);
i18nOptions.resources[shortlang] = {translation: translationData};
// load i18n
i18next.init(i18nOptions, function(err, t) {
// read MarkDown file
fs.readFile(util.format('%s/i18n_man/%s/%s.md', __dirname, req.params.manpage, shortlang), async function (manerror, fileData) {
// i18n initialized and ready to go!
// document.getElementById('output').innerHTML = i18next.t('key');
// variables to pass to Mustache to populate template
var view = {
isHelpPage: true,
shortlang: shortlang,
adminMenuExtra: localconfig.adminMenuExtra,
logo: templateutils.logo(),
langname: i18n_utils.getLangName(config.languages, shortlang),
baseurl: localconfig.url + "/",
languages: config.adminLanguages,
devMode: localconfig.devMode,
credits: config.map.author,
manual: manerror ? i18next.t('page.notFound') : wizard.manRender(fileData),
// translations
i18n: function () {
return function (text, render) {
i18next.changeLanguage(shortlang);
return i18next.t(text);
}
}
};
// console.log(view);
const menuTemplate = await templateutils.readMustachePartials('public/wizard/menu.mustache');
const partials = {menu: menuTemplate};
var output = Mustache.render(template, view, partials);
res.send(output);
});
});
});
});
// Proxy to convert (redirect) url provided by Wikimedia
// to a scaled image. Cache: 5 minutes.
// apicache, in milliseconds, has a max of int 32 bit
// max: 2147483647 = 0.81 months
/**
* Proxy to convert (redirect) url provided by Wikimedia to a scaled image
* with long cache. Module apicache has a max of int 32 bit milliseconds
* max: 2147483647 = 0.81 months
* @param {Express request} req
* @param {Express response} res
* @return Express send binary, scaled and EXIF-rotated image.
*/
app.get(/thumb\/(.+)$/, apicache(2147483647), function(req, res) {
try {
var popupMaxWidth = 480;
// test url: http://commons.wikimedia.org/wiki/Special:FilePath/Kantonales%20naturhistorisches%20Museum%20%28Geb%C3%A4ude%29%202013-09-17%2017-08-17.jpg
// test name = Kantonales%20naturhistorisches%20Museum%20%28Geb%C3%A4ude%29%202013-09-17%2017-08-17.jpg
// generated path: /thumb/Kantonales%20naturhistorisches%20Museum%20(Geb%C3%A4ude)%202013-09-17%2017-08-17.jpg
var commonsRedirectPrefix = 'http://commons.wikimedia.org/wiki/Special:FilePath/';
var commonsRedirectUrl = commonsRedirectPrefix + encodeURIComponent(req.params[0]);
let options = {
url: commonsRedirectUrl,
method: 'HEAD'
};
// Seguo il redirect con una richiesta
request(options, function (error, response, body) {
if (error) {
console.log('/get/thumb/: redirect error');
console.log('error:', error); // Print the error if one occurred
res.status(500).send('/get/thumb/: redirect error');
}
else {
// Ottengo URL immagine originale
var imageurl = response.request.href;
// Rotazione e riscalamento
console.log(response.request.href);
const transformer = sharp()
.rotate() // auto-rotate on EXIF
.resize(popupMaxWidth, popupMaxWidth)
// .png({ interlaced: true })
// .crop(sharp.strategy.entropy)
.on('error', function(err) {
console.log(err);
});
try {
// @see http://sharp.pixelplumbing.com/en/stable/api-resize/#crop
// Scrivi nello stream l'immagine riscalata
req.pipe(request(imageurl)).pipe(transformer).pipe(res);
}
catch (e) {
res.status(500).send('/get/thumb/: pipe error');
}
}
});
}
catch (e) {
console.log("Image thumb error");
console.log(e);
}
});
/**
* Serve Real-time map page (HTML) based on query on req (e.g. /v/slug).
* Do not cache (multilingual).
* @param {Express request} req
* @param {Express response} res
* @return Express send HTML.
*/
app.get('/v/:path', function (req, res) {
// no history needed here
Map.findOne({
where: {
path: req.params.path,
published: true
}
}).then(async (record) => {
if (record) {
generateMapPage(req, res, dbutils.getMapRecordAsDict(record), false);
}
else {
res.status(404).send(await templateutils.notFound(req));
}
});
});
/**
* Serve History timeline map page (HTML). Display current results with past
* results on a timeline (HTML) based on req (e.g. /v/slug).
* Do not cache (multilingual).
* @param {Express request} req
* @param {Express response} res
* @return Express send HTML.
*/
app.get('/h/:path', function (req, res) {
// let path = req.url.substring(1);
Map.findOne({
where: {
path: req.params.path,
published: true
}
}).then(async (record) => {
if (record) {
generateMapPage(req, res, dbutils.getMapRecordAsDict(record), true);
}
else {
res.status(404).send(await templateutils.notFound(req));
}
});
});
// Enable json for express (to get req.body to work)
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/admin/api/get/:name', dbapi.adminApiGetName);
/**
* Internal admin API to C-U- (not Read, not real Delete) maps.
* @param {Express request} req
* @param {Express response} res
* @return {[type]} [description]
*/
app.put('/admin/api/:action', dbapi.adminApiAction);
const yourMapPath = '/your-map';
app.post(yourMapPath, function (req, res) {
logger.debug(req.body)
mail.sendMail(req.body)
res.redirect(302, `${yourMapPath}?message=sent&l=${req.body.shortlang}`)
})
/**
* Display Landing page
* @param {Express request} req
* @param {Express response} res
* @return Express send with HTML.
*/
app.get(yourMapPath, async function (req, res) {
fs.readFile(util.format('%s/public/frontend/mailmap.mustache', __dirname), function (err, fileData) {
if (err) {
throw err;
}
// get template content, server-side
let template = fileData.toString();
let [shortlang, translationData] = i18n_utils.seekLang(req, config.fallbackLanguage, 'mailmap');
let i18nOptions = i18n_utils.geti18nOptions(shortlang);
i18nOptions.resources[shortlang] = {translation: translationData};
i18next.init(i18nOptions, async function(err, t) {
// variables to pass to Mustache to populate template
var view = {
shortlang: shortlang,
path: yourMapPath,
logo: templateutils.logo(),
langname: i18n_utils.getLangName(config.languages, shortlang),
baseurl: localconfig.url + "/your-map",
languages: config.languages,
devMode: localconfig.devMode,
author: config.map.author,
hasMessageSent: req.query.message === "sent", // ?message=sent
i18n: function () {
return function (text, render) {
i18next.changeLanguage(shortlang);
return i18next.t(text);
}
}
};
const menuTemplate = await templateutils.readMustachePartials('public/frontend/menu.mustache');
const partials = {
menu: menuTemplate,
urlqueryargs: templateutils.retainUrlQueryArguments(req),
required: ` <sup title="${i18next.t('required')}"><i class="ui red icon small asterisk"></i></sup>`
};
var output = Mustache.render(template, view, partials);
res.send(output);
});
});
});
/**
* Admin pages, listing all available maps.
* @param {Express request} req
* @param {Express response} res
* @return Express send of HTML.
*/
app.get('/admin', async function (req, res) {
fs.readFile(util.format('%s/public/wizard/admin.mustache', __dirname), async function (err, fileData) {
// cannot read template?
if (err) {
throw err;
}
// load categories
const categoriesWithMaps = await query.categoriesWithMaps();
// load all maps data
let jsonRes = [];
if (categoriesWithMaps.length) {
// maps found, continue
// get template content, server-side
let template = fileData.toString();
let [shortlang, translationData] = i18n_utils.seekLang(req, config.fallbackLanguage, 'admin');
let i18nOptions = i18n_utils.geti18nOptions(shortlang);
i18nOptions.resources[shortlang] = {translation: translationData};
// console.log(i18nOptions);
// load i18n
i18next.init(i18nOptions, async function(err, t) {
// i18n initialized and ready to go!
// document.getElementById('output').innerHTML = i18next.t('key');
// variables to pass to Mustache to populate template
var view = {
isAdminPage: true,
shortlang: shortlang,
adminMenuExtra: localconfig.adminMenuExtra,
langname: i18n_utils.getLangName(config.languages, shortlang),
languages: config.adminLanguages,
devMode: localconfig.devMode,
credits: config.map.author,
logo: templateutils.logo(),
categories: categoriesWithMaps.map(categoryWithMaps => dbutils.getCategoryWithMapsAsDict(categoryWithMaps)),
i18n: function () {
return function (text, render) {
i18next.changeLanguage(shortlang);
return i18next.t(text);
}
}
};
// console.log(view);
const menuTemplate = await templateutils.readMustachePartials('public/wizard/menu.mustache');
const partials = {menu: menuTemplate};
var output = Mustache.render(template, view, partials);
res.send(output);
});
}
else {
res.status(404).send(await templateutils.notFound(req, 'Cannot edit an empty Map table, add at least one map to continue'));
}
});
});
app.get('/*', async function (req, res) {
res.status(404).send(await templateutils.notFound(req))
});
}