-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
487 lines (442 loc) · 16.1 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
const express = require('express');
const session = require('express-session');
const bodyParser = require('body-parser');
const Negotiator = require('negotiator');
const { stringify } = require('csv-stringify/sync');
const fs = require('fs');
const path = require('path');
const app = express();
const cors = require('cors');
require('dotenv').config();
app.use(cors());
app.use('/diffs', express.static('diffs'));
app.use('/data', express.static('data'));
app.use('/style', express.static('style'));
app.use('/img', express.static('img'));
app.use('/docs', express.static('docs'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
// Use the session middleware
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true,
}));
const base = process.env.BASE;
var sourcedir = process.env.DATADIR;
let jsonData = {};
app.locals.readFileContent = function(filePath) {
try {
return fs.readFileSync(filePath, 'utf8');
} catch (error) {
console.error("Error reading file:", error);
return "Error reading file";
}
};
function loadJsonData(directory) {
try {
const dataPath = path.join(directory, 'data.jsonld');
if (fs.existsSync(dataPath)) {
return JSON.parse(fs.readFileSync(dataPath, 'utf8'));
} else {
throw new Error(`data.jsonld not found in ${directory}`);
}
} catch (error) {
console.error(`Error loading data: ${error.message}`);
return null;
}
}
app.use((req, res, next) => {
if (req.query.source) {
const requestedDir = `data/${req.query.source}/`;
if (fs.existsSync(requestedDir)) {
const newData = loadJsonData(requestedDir);
if (newData) {
// Store the selected data in the user's session
req.session.selectedData = newData;
req.session.sourceDir = requestedDir;
console.log(`Data loaded from ${requestedDir}`);
} else {
console.warn(`Unable to load data from ${requestedDir}, using the current data.`);
}
} else {
console.warn(`Requested directory ${requestedDir} does not exist, using the current data.`);
}
// Remove sourceDir query parameter and redirect
const parsedUrl = new URL(req.url, `http://${req.headers.host}`);
parsedUrl.searchParams.delete('source');
return res.redirect(303, parsedUrl.pathname + parsedUrl.search);
} else {
// If sourceDir is not defined and req.session.selectedData is not already defined, load "example1" data directory by default
if (!req.session.selectedData) {
const defaultDir = process.env.DATADIR || 'data/example1/';
const defaultData = loadJsonData(defaultDir);
if (defaultData) {
req.session.selectedData = defaultData;
req.session.sourceDir = defaultDir;
console.log(`Default data loaded from ${defaultDir}`);
} else {
console.warn(`Unable to load default data from ${defaultDir}.`);
}
}
}
next();
});
function getMetadataFromGraph(jsonLdGraph) {
const metadata = {};
for (const key in jsonLdGraph) {
if (!['@context', 'url', 'tableSchema', 'dialect', 'rows', '@graph'].includes(key)) {
metadata[key] = jsonLdGraph[key];
}
}
return metadata;
}
function extractAfterLastSlashOrHash(inputString) {
if (!inputString || typeof inputString !== 'string') {
return inputString;
}
const lastIndexSlash = inputString.lastIndexOf('/');
const lastIndexHash = inputString.lastIndexOf('#');
const lastIndex = Math.max(lastIndexSlash, lastIndexHash);
if (lastIndex === -1) {
// Neither "/" nor "#" found, return the input string as is
return inputString;
}
return inputString.substring(lastIndex + 1);
}
function sendData(data,req,res) {
const jsonData = req.session.selectedData;
const negotiator = new Negotiator(req);
const preferredMediaType = negotiator.mediaType(['text/html', 'application/ld+json', 'text/csv', 'application/json']);
const language = negotiator.language(['en', 'fr']) || 'en';
if (!Array.isArray(data)) {
data = [data];
}
// Extract the metadata from the original JSON-LD graph
const metadata = getMetadataFromGraph(jsonData);
let labeledData = {};
switch (preferredMediaType) {
case 'application/ld+json':
const jsonLDResponse = {
'@context': jsonData['@context'],
...Object.fromEntries(Object.entries(metadata)),
'@graph': [
data
]
};
return res.json(jsonLDResponse);
case 'text/html':
data = updateIdsWithBase(data,process.env.BASE);
res.render('dataView', { inputData: data.map(t => getLabelledDataWithURIs(t, language,jsonData)), metadata, objectToString, sourcedir: req.session.sourceDir, base: process.env.BASE, renderCsvToTable});
break;
case 'text/csv':
data = updateIdsWithBase(data,process.env.BASE);
if (!req.query.simple) {
data = replacePrefixes(data);
}
labeledData = data.map(t => getLabelledData(t, language,jsonData));
try {
// Making sure labeledData is always an array
const dataArray = Array.isArray(labeledData) ? labeledData : [labeledData];
// Convert labeled data objects to array of objects for CSV export
const csvData = dataArray.map(item => {
const dataObject = {};
for (const [key, value] of Object.entries(item)) {
if (typeof value === 'object' && value !== null) {
let outputValue = value['@id'] || value['@value'] || value['schema:value'] || value['http://schema.org/value'] || value['schema:identifier'];
if (req.query.simple) {
outputValue = value['@value'] || value['schema:value'] || value['http://schema.org/value'] || value['schema:identifier'];
outputValue = extractAfterLastSlashOrHash(outputValue);
}
dataObject[key] = outputValue;
/*
tempObject = getLabelledData(value,language,jsonData);
for (const tempKey in tempObject) {
if (tempKey != '@id' && tempKey != 'rdf:type' && tempKey != 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type' && tempKey != "@value" && tempKey != 'schema:value' && tempKey !='http://schema.org/value') {
dataObject[tempKey] = tempObject[tempKey];
}
}
*/
} else {
let outputValue = value;
if (req.query.simple) {
outputValue = extractAfterLastSlashOrHash(value);
}
dataObject[key] = outputValue;
}
}
return dataObject;
});
const csvHeaders = Object.keys(csvData[0]).map(key => ({ key, label: key }));
for (const row of csvData) {
for (const header of csvHeaders) {
if (!row.hasOwnProperty(header)) {
row[header] = ''; // Set an empty string for missing headers
}
}
}
const output = stringify(csvData, {
header: true,
columns: csvHeaders,
delimiter: ', '
});
const filename = req.path.replace(/\//g, '');
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename=' + filename + '.csv');
res.send(output);
} catch (err) {
console.error(err);
return res.status(500).send('Internal Server Error');
}
break;
case 'application/json':
data = updateIdsWithBase(data,process.env.BASE);
if (!req.query.simple) {
data = replacePrefixes(data);
}
labeledData = data.map(t => getLabelledData(t, language,jsonData));
res.json(labeledData);
break;
default:
return res.status(406).send('Not Acceptable');
}
}
function objectToString(obj) {
if (typeof obj !== 'object') return obj;
if (obj['@value'] && obj['@type'] && Object.keys(obj).length === 2) {
return obj['@value']; // Return just the value if @value and @type are the only keys
}
if (obj['@id'] && Object.keys(obj).length === 1) {
// Only @id is present, return it as a link
return `<a href="${obj['@id']}">${obj['@id']}</a>`;
}
let str = '<ul>';
for (const key in obj) {
if (key !== '@id') {
str += `<li>${key}: ${objectToString(obj[key])}</li>`;
}
}
str += '</ul>';
if (obj['@id']) {
str = `<a href="${obj['@id']}">${str}</a>`; // wrap the entire content with a link if @id is present
}
return str;
}
function renderCsvToTable(filePath) {
try {
const csvContent = fs.readFileSync(filePath, 'utf8');
const rows = csvContent.split('\n').map(row => row.split(','));
let htmlTable = '<table class="csv-table">';
rows.forEach((row, index) => {
htmlTable += '<tr>';
const tag = (index === 0) ? 'th' : 'td'; // Use <th> for header row
row.forEach(cell => {
htmlTable += `<${tag}>${cell}</${tag}>`;
});
htmlTable += '</tr>';
});
htmlTable += '</table>';
return htmlTable;
} catch (error) {
return 'File not found or unable to read the file';
}
}
function expandURI(prefix, context) {
const [namespace, property] = prefix.split(':');
if (context[namespace]) {
return context[namespace] + property;
}
return prefix; // Return the original prefix if not found in context
}
function getLabelledDataWithURIs(data, language,jsonData) {
// Map the data to labels and values
const context = {
"schema": "http://schema.org/",
"dc": "http://purl.org/dc/terms/",
"dcat": "http://www.w3.org/ns/dcat#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"pay": "http://reference.data.gov.uk/def/payment#"
};
const labeledData = {};
const graph = jsonData['@graph'];
for (const key in data) {
let skip = false;
let label = key;
let value = data[key];
let uri = null;
let labellink = null;
let valuelink = null;
let obj = findObjectByKey(graph,key);
if (obj && obj['rdfs:label']) {
const labels = obj['rdfs:label'];
const labelObject = labels.find(label => label['@language'] === language) || labels.find(label => label['@language'] === 'en');
label = labelObject ? labelObject['@value'] : key;
uri = obj['@id'] ? obj['@id'] : null;
labellink = expandURI(uri,context);
} else {
label = key.split(":")[1] ? key.split(":")[1] : key;
if (label != key) {
labellink = expandURI(key,context);
}
}
if (typeof data[key] === 'object') {
if ('@value' in data[key]) {
value = data[key]['@value'];
} else if ('@id' in data[key]) {
uri = data[key]['@id'];
if (Object.keys(getLabelledDataWithURIs(data[key],language,jsonData)).length > 1) {
search = data[key];
value = search['schema:value'] || search['http://schema.org/value'] || search['schema:identifier'] || search['@id'];
valuelink = search['@id'] || null;
} else {
value = uri; // Fallback to URI if label not found
}
}
}
if (!skip) {
if (typeof value === 'string') {
if (value.split(":")[0].startsWith("http")) {
valuelink = value;
split = value.split("/");
value = split[split.length-1];
}
if(value.split(":")[1] && !value.split(":")[0].startsWith("http")) {
valuelink = expandURI(value,context);
value = value.split(":")[1];
}
}
labeledData[label] = { value, valuelink, labellink };
}
}
return labeledData;
}
function findObjectByKey(graph, keyToFind) {
for (const obj of graph) {
if (obj[keyToFind] && obj[keyToFind]['@id'] === keyToFind) {
return obj[keyToFind];
}
}
return null; // Return null if the '@id' matching the key is not found in any object
}
function getLabelledData(data, language,jsonData) {
// Map the data to labels and values
const context = jsonData['@context'];
const graph = jsonData['@graph'];
const labeledData = {};
for (const key in data) {
let label = key;
let value = data[key];
let uri = null;
let link = null;
let obj = findObjectByKey(graph,key);
if (obj && obj['rdfs:label']) {
const labels = obj['rdfs:label'];
const labelObject = labels.find(label => label['@language'] === language) || labels.find(label => label['@language'] === 'en');
label = labelObject ? labelObject['@value'] : key;
uri = obj['@id'] ? obj['@id'] : null;
link = expandURI(uri,context);
}
if (typeof data[key] === 'object') {
if ('@value' in data[key]) {
value = data[key]['@value'];
} else if ('@id' in data[key]) {
uri = data[key]['@id'];
//if (uri.startsWith(data['@id'] + "#")) {
if (Object.keys(getLabelledData(data[key],language,jsonData)).length > 1) {
value = getLabelledData(data[key],language,jsonData);
//nested
value = data[key];
//simple
//value = uri;
//} else if (obj && obj['rdfs:label']) {
// console.log('in here');
// const labelObject = obj['rdfs:label'].find(label => label['@language'] === language) || obj['rdfs:label'].find(label => label['@language'] === 'en');
// value = labelObject ? labelObject['@value'] : uri;
// link = expandURI(uri,context);
} else {
value = uri; // Fallback to URI if label not found
}
}
}
labeledData[label] = value;
}
return labeledData;
}
function replacePrefixes(data) {
context = {
"schema": "http://schema.org/",
"dc": "http://purl.org/dc/terms/",
"dcat": "http://www.w3.org/ns/dcat#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"pay": "http://reference.data.gov.uk/def/payment#"
};
function replacePrefix(str) {
const [prefix, suffix] = str.split(':');
if (context[prefix]) {
return context[prefix] + suffix;
}
return str;
}
// Recursively process data
function processNode(node) {
if (typeof node === 'string' && node.includes(':')) {
return replacePrefix(node);
} else if (Array.isArray(node)) {
return node.map(processNode);
} else if (typeof node === 'object' && node !== null) {
const newNode = {};
Object.keys(node).forEach(key => {
newNode[replacePrefix(key)] = processNode(node[key]);
});
return newNode;
}
return node;
}
return processNode(data);
}
function updateIdsWithBase(data, baseUrl) {
return data.map(item => {
const updatedItem = { ...item };
Object.keys(updatedItem).forEach(key => {
if (key === '@id' && !updatedItem[key].startsWith('http')) {
updatedItem[key] = baseUrl + updatedItem[key];
} else if (typeof updatedItem[key] === 'object' && updatedItem[key] !== null) {
updatedItem[key] = Array.isArray(updatedItem[key])
? updateIdsWithBase(updatedItem[key], baseUrl)
: updateIdsWithBase([updatedItem[key]], baseUrl)[0]; // Recursively update nested objects
}
});
return updatedItem;
});
}
app.get('*', async (req, res) => {
jsonData = req.session.selectedData;
const nodes = jsonData['@graph'] || [];
let requesturi = 'transactions/';
requesturi = req.path.slice(1);
// Define baseValue from contextArray
const baseValue = jsonData["@context"][1]['@base']; // your logic to get baseValue from contextArray
// Set a variable for the base URL
const baseUrl = baseValue === process.env.BASE ? '' : process.env.BASE;
let data = [];
if (requesturi.slice(-1) == "/") {
data = nodes.filter(node => {
return typeof node['@id'] === 'string' && node['@id'].startsWith(baseUrl + requesturi);
});
} else {
data = jsonData['@graph'].find(obj => obj['@id'] === `${baseUrl}${requesturi}`);
}
if (!data || data.length == 0) {
return res.status(404).send('Item not found');
}
sendData(data,req,res);
});
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || 3000;
app.listen(port, host, () => {
console.log(`Server running at http://${host}:${port}`);
});