-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputing.js
More file actions
615 lines (517 loc) · 21 KB
/
Copy pathcomputing.js
File metadata and controls
615 lines (517 loc) · 21 KB
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
/**
* Computing component.
*
* Or the glue between Grafterizer, Graftwerk and DataGraft.
*/
'use strict';
// jscs:disable requireCamelCaseOrUpperCaseIdentifiers
// Logging component for error and info messages
const logging = require('./logging');
// Request is a library to easely create HTTP requests
const request = require('request');
// BodyParser is used to parse JSON in the body of HTTP requests
const bodyParser = require('body-parser');
// Path is used to extract information from a file path
const path = require('path');
// Mime is used to get the mimetype from a file extension
const mime = require('mime');
// Parse content-disposition headers
const contentDisposition = require('content-disposition');
// Transform a stream to a string
const concat = require('concat-stream');
// Manipulate files in nodejs
const fs = require('fs');
// System constants to manipulate files
const constants = require('constants');
// Manage temporary files paths
const temp = require('temp');
// Initializing the jsonParser
const jsonParser = bodyParser.json({limit: '5mb'});
// The .nt mimefile is not defined by default
mime.define({
'application/n-triples': ['nt'],
'text/turtle': ['ttl'],
'application/rdf+xml': ['rdf'],
'application/n-quads': ['nq'],
'text/n3': ['n3'],
'application/trix': ['trix'],
'application/trig': ['trig'],
'application/ld+json': ['jsonld']
});
// Return the authorization token from the request
// The authorazation token must exist and is probably already
// checked by the authentication middleware
const getAuthorization = (req) => {
try {
return 'Bearer ' + req.oauthSession.token.access_token;
} catch (e) {
logging.error('Unable to get the authorization token from the session cookie store');
}
};
// Returns information about the file, using the content-disposition header
// Also returns default values as a failback (in CSV)
const getAttachmentInfos = (response) => {
const defaultInfos = {
type: 'csv',
name: 'output',
filename: 'output.csv',
mime: 'text/csv'
};
// If no headers are present or no content-disposition, the default informations
// are returned
if (!response.headers || !response.headers['content-disposition']) {
return defaultInfos;
}
// Parse the content disposition header, fallback to the default informations
// if it fails
let disposition;
try {
disposition = contentDisposition.parse(response.headers['content-disposition']);
} catch (e) {
return defaultInfos;
}
// If the filename is not present int he content-disposition header,
// returns the default information
if (!disposition.parameters || !disposition.parameters.filename) {
return defaultInfos;
}
// Compute the required informations from the parsed content-disposition header
const filename = disposition.parameters.filename;
const ext = path.extname(filename);
return {
type: ext.slice(1),
name: path.basename(filename, ext),
filename: path.basename(filename),
mime: mime.lookup(ext)
};
};
const downloadErrorText = '<h3>An error has occured.</h3>' + '<p><code></pre>{{OUTPUT}}</pre></code></p>' +
'<p><a href="http://project.dapaas.eu/dapaas-contact-us">Please contact us.</a></p>';
module.exports = (app, settings) => {
// Returns and display an error
const showAndLogError = (res, status, message, data) => {
// If the headers are already sent, it probably means the server has started to
// provide a message and it's better to just keep the same message instead of
// crashing trying to send already sent headers
if (!res.headersSent) {
res.status(status).json({
error: message,
data
});
}
logging.error(message, data);
};
// This regular expression is used to extract the ID
// from an upwizards distribution ID
const upwizardExtractIDRegex = /^upwizards--(\d+)$/;
// Return a request to download the raw distribution
// The stream can be transmitted directly to the client
// or forwarded to Grafterizer
const downloadRaw = (req, res) => {
// Loading the distribution id parameter
// from the query paramaters or the post body
const distribution = req.params.distribution || req.query.distribution || (req.body && req.body.distribution);
// If somehow the distribution ID is not provided,
// it's better to stop here
if (!distribution) {
showAndLogError(res, 400, 'The distribution ID is missing');
return;
}
// There is different kinds of files in Datagraft
// filestores files, and upwizards files.
// upwizards files are identified by a number
// if the distribution ID starts with upwizards--,
// it means it's an upwizards file. Otherwise, it's a filestores file
// It should not be possible to create a filestores file with an ID starting
// with upwizards--
const matchUpwizardId = distribution.match(upwizardExtractIDRegex);
// DataGraft query asking the attached file
return request.get({
// /attachement DataGraft's method redirects to the URL of the attachment
url: settings.datagraftUri + '/myassets/' + (matchUpwizardId ? 'upwizards' : 'filestores') + '/' +
encodeURIComponent(matchUpwizardId ? matchUpwizardId[1] : distribution) + '/attachment',
headers: {
// You need a valid authorization of course
Authorization: getAuthorization(req)
}
}).on('error', function (err) {
showAndLogError(res, 500, 'Unable to download the data distribution', err);
});
};
// Execute the transformation using Graftwerk
// The important thing to notice is that the filestores is
// directly transferred from DataGraft to Graftwerk
// This server never has the whole file in memory. It is only
// working on streams and buffers. The advantage is that it requires
// less memory, and it is faster as the file transfers are reduced.
// The cons are a sligthly more complex codebase and a small hack
// (see the warning)
const executeTransformation = (req, res, clojure, type, acceptType, successCallback, errorCallback) => {
// 'pipe' is the default type
if (type !== 'graft') {
type = 'pipe';
}
const stream = downloadRaw(req, res);
if (!stream) return;
// When DataGraft has returned an answer
stream.on('response', (response) => {
// If the requests has failed, the request is just transferred to the client
// so it can parse or display the DataGraft error
if (!response || response.statusCode !== 200) {
// if not deleted, these headers are forwarded from the service, which could cause CORS errors
delete response.headers['access-control-allow-credentials'];
delete response.headers['access-control-allow-origin'];
stream.pipe(res);
return;
}
// Load informations about the raw file
const streamInfos = getAttachmentInfos(response);
var knownLength;
if (response.headers && response.headers['content-length']) {
knownLength = parseInt(response.headers['content-length']);
} else {
// /!\ beware of the wild constant /!\
// This is a hack to send the file and the pipeline in a streaming mode
// when we don't know the size of the file.
// The value just have to be very big so the file is not cut.
// We cannot compute the real length of the file because we don't know
// it before we start streaming this request.
// It seems that graftwerk works fine using this hack, but it might
// change in the future.
knownLength = 10000000000;
}
// Graftwerk Request
const formData = {
pipeline: {
value: clojure,
// Graftwerk requires a Clojure transformation file
// so we create a virtual one
options: {
filename: 'pipeline.clj',
contentType: 'text/plain'
}
},
data: {
value: stream,
options: {
filename: streamInfos.filename,
contentType: streamInfos.mime,
knownLength: knownLength
}
},
command: req.query.command || ('my-' + type)
};
// If a specific page is required, it is transmitted to Graftwerk
if (typeof req.query.page !== 'undefined') {
formData.page = parseInt(req.query.page) || 0;
}
// The page size can also be configured
if (req.query.pageSize) {
formData['page-size'] = parseInt(req.query.pageSize) || 50;
}
// The cache is not enabled by default, but it is recommended
// for long and slow queries as the client HTTP timeout may occur
// long before Graftwerk returns the result.
const endpoint = (req.query.useCache || (req.body && req.body.useCache)) ?
settings.graftwerkCacheUri : settings.graftwerkUri;
const headers = {
// /!\ This is mandatory to be able to send the file in a streaming mode
'transfer-encoding': 'chuncked',
};
if (acceptType) {
headers.Accept = acceptType;
}
// Querying Graftwerk
const resultStream = request.post({
url: endpoint + '/evaluate/' + type,
headers,
formData
});
resultStream.on('error', (err) => {
showAndLogError(res, 500, 'Unable to transform the file using the original transformation', err);
stream.abort();
}).on('response', (response) => {
// If the response is non valid
if (!response || response.statusCode !== 200) {
// Fetch the response string
var outputError = concat({
encoding: 'string',
}, function (graftwerkOutput) {
// Display the error, using the callback or the default system
if (errorCallback) {
logging.error('Unable to transform the file with Graftwerk', graftwerkOutput);
errorCallback(graftwerkOutput);
} else {
showAndLogError(res, 500, 'Unable to transform the file with Graftwerk', graftwerkOutput);
}
});
resultStream.pipe(outputError);
logging.error('The transformed data is invalid', {
response: response ? response.statusCode : 'response empty'
});
return;
}
// Remove the access control headers, so they don't override the
// working ones
delete response.headers['access-control-allow-credentials'];
delete response.headers['access-control-allow-origin'];
// Run the callback
if (successCallback) {
var filename = streamInfos.name.replace(/[^a-zA-Z0-9_\-]/g, '') + '-processed';
successCallback(resultStream, response, filename, type);
} else {
// Or just pipe to the default output by default
resultStream.pipe(res);
}
});
});
};
const transformDistribution = (req, res, distribution, transformation, type, callbackSuccess, callbackError) => {
// Fetch the clojure code from DataGraft
request.get({
url: settings.datagraftUri + '/myassets/transformations/' +
encodeURIComponent(transformation) + '/configuration/code',
headers: {
Authorization: getAuthorization(req)
}
}, (error, response, clojure) => {
if (error) {
showAndLogError(res, 500, 'Unable to load the transformation code', error);
return;
}
var acceptMimeType = 'application/n-triples';
if (req.query.rdfFormat) {
acceptMimeType = mime.lookup(req.query.rdfFormat);
}
executeTransformation(req, res, clojure, type, (type === 'graft' ? acceptMimeType : 'application/csv'), callbackSuccess, callbackError);
});
};
const transformAndSaveTemporarilyDistribution = (req, res, distribution, transformation, type, callbackSuccess) => {
transformDistribution(req, res, distribution, transformation, type,
(resultStream, response, filename, type) => {
// If we are here and the status is not correct, we display the output
if (!response || response.statusCode !== 200) {
stream.pipe(res);
return;
}
// Create a temporary file to save the output from Graftwerk
// The problem is that Graftwerk doesn't send a content-length
// for RDF (only CSV), so we have to save it first before fowarding
// it to the next component
var tmpPath = temp.path('grafterizer-save', 's-');
/*jshint bitwise: false*/
var tmpWriteStream = fs.createWriteStream(tmpPath, {
flags: constants.O_CREAT | constants.O_TRUNC | constants.O_RDWR | constants.O_EXCL,
mode: '0600'
});
/*jshint bitwise: true*/
// Save in the temporary file
resultStream.pipe(tmpWriteStream);
// When the file has finished to be received from Graftwerk
tmpWriteStream.on('finish', () => {
callbackSuccess(tmpPath);
}).on('error', (err) => {
fs.unlink(tmpPath);
showAndLogError(res, 500, 'Error while transmitting the transformed data to the database', err);
});
});
};
// Download the raw distribution file content
// Graftwerk is not involved in the process
app.get('/preview_raw/:distribution', (req, res) => {
const stream = downloadRaw(req, res);
if (stream) stream.pipe(res);
});
// Transfrom the distribution with an empty transformation
// to parse the file and show the original content.
// Graftwerk is involved in the process, to parse the file
app.get('/preview_original/:distribution', (req, res) => {
executeTransformation(req, res,
// This transformation is very simple and is just used to parse the file
// using Graftwerk
'(defpipe my-pipe [data-file] (-> (read-dataset data-file)))',
'pipe');
});
// Transformation previewing
// The data distribution is loaded from DataGraft and previewed using Graftwerk
// The transformation code is sent by the client in the HTTP request body
// The posted document should be formatted using JSON (and not the often default form-data)
app.post('/preview/:distribution', jsonParser, (req, res) => {
// Loading the clojure code from the request body
const clojure = req.body && req.body.clojure;
// If the clojure code is missing, the request is aborted
if (!clojure) {
showAndLogError(res, 400, 'The clojure transformation code is missing');
return;
}
// If the client tries to send a number, an array or whatever
// it's probably better to abort the request now
if (typeof req.body.clojure !== 'string') {
showAndLogError(res, 400, 'The clojure transformation code is not a string');
return;
}
executeTransformation(req, res, clojure, req.body.transformationType);
});
// Execute and download the transformation on the distribution
// Unlike the other methods, the code is not sent by the client but fetched
// from DataGraft. To have a GET request with few parameters.
app.get('/transform/:distribution/:transformation', (req, res) => {
transformDistribution(req, res, req.params.distribution, req.params.transformation,
req.query.type, (stream, response, filename, type) => {
// Replace the headers to download a nice file
if (!req.query.useCache) {
delete response.headers['content-disposition'];
delete response.headers['content-type'];
delete response.headers.server;
if (type === 'graft') {
res.contentType(mime.lookup(req.query.rdfFormat));
res.setHeader('content-disposition', 'attachment; filename=' + filename + '.' + req.query.rdfFormat);
} else {
res.contentType('text/csv');
res.setHeader('content-disposition', 'attachment; filename=' + filename + '.csv');
}
}
stream.pipe(res);
},
// Show the error in a slightly improved way
(message) => {
res.status(500);
if (!req.query.raw) {
res.send(downloadErrorText.replace('{{OUTPUT}}', escape(message)));
} else {
res.json({
error: message
});
}
});
});
// Transform a distribution using a transformation
// Saves the output in a repository
app.post('/fillRDFrepo', jsonParser, (req, res) => {
var transformationUri = req.body.transformation;
var distributionUri = req.body.distribution;
var queriableDataStoreUri = req.body.queriabledatastore;
var isOntotext = !!req.body.ontotext;
if (!transformationUri) {
showAndLogError(res, 400, 'The transformation URI is missing');
return;
}
if (!distributionUri) {
showAndLogError(res, 400, 'The distribution URI is missing');
return;
}
if (!queriableDataStoreUri) {
showAndLogError(res, 400, 'The queriable data store URI is missing');
return;
}
if (isOntotext) {
// We need to fetch an ontotext key
request.get({
url: settings.datagraftUri + '/api_keys/first',
headers: {
Authorization: getAuthorization(req)
}
}, (err, response, body) => {
// Errors management
if (err) {
showAndLogError(res, 500, 'Unable to fetch an Ontotext Key from DataGraft', err);
return;
}
// Creation of the Basic authorization header
var authorization = 'Basic ' + (new Buffer(body).toString('base64'));
// Query a select count to check if the database is ready
// If this request fails, it's not necessary to execute the transformation
request.get({
url: queriableDataStoreUri,
qs: {
query: 'SELECT (count(*) as ?count) WHERE {?s ?p ?o . }'
},
headers: {
Authorization: authorization,
Accept: 'application/sparql-results+json',
}
}, (err, response, body) => {
// If we have an error, the repository is likely not ready yet
if (err) {
showAndLogError(res, 503, 'The repository is not accessible', err);
return;
}
// Execute the transformation
transformAndSaveTemporarilyDistribution(req, res, distributionUri, transformationUri, 'graft',
(tmpPath) => {
// Save the stream in the database
var saveStream = request.post({
url: queriableDataStoreUri + '/statements',
headers: {
Authorization: authorization,
'Content-Type': 'text/x-nquads;charset=UTF-8'
},
});
// When the received the response, the database has received the file
saveStream.on('response', (response) => {
// The file is deleted once it has been received
fs.unlink(tmpPath);
// Redirect the output from the database to the client
// With ontotext, it's only a HTTP 204 OK, but it might contain
// more information in the future.
saveStream.pipe(res);
}).on('error', (err) => {
fs.unlink(tmpPath);
showAndLogError(res, 500, 'Error while transmitting the transformed data to the database', err);
});
fs.createReadStream(tmpPath).pipe(saveStream);
});
});
});
} else {
showAndLogError(res, 501, 'Only Ontotext Queriable Data Stores are supported');
return;
}
});
// Transform a distribution using a transformation
// Saves the output in a wizard instance
app.post('/fillWizard', jsonParser, (req, res) => {
var transformationUri = req.body.transformation;
var distributionUri = req.body.distribution;
var wizardId = req.body.wizardId;
var transformationType = req.body.type;
if (!transformationUri) {
showAndLogError(res, 400, 'The transformation URI is missing');
return;
}
if (!distributionUri) {
showAndLogError(res, 400, 'The distribution URI is missing');
return;
}
if (!wizardId) {
showAndLogError(res, 400, 'The wizard ID is missing');
return;
}
if (!transformationType) {
showAndLogError(res, 400, 'The transformation type (pipe, graft) is missing');
return;
}
transformAndSaveTemporarilyDistribution(req, res, distributionUri, transformationUri, transformationType, (tmpPath) => {
// Save the stream in the upwizard object
var saveStream = request.put({
url: settings.datagraftUri + '/myassets/upwizards/save_transform/' +
encodeURIComponent(wizardId),
headers: {
Authorization: getAuthorization(req)
},
formData: {
'upwizard[type_of_transformed_file]': transformationType,
'upwizard[transformed_file]': fs.createReadStream(tmpPath)
}
});
saveStream.on('response', (response) => {
// The file is deleted once it has been received
fs.unlink(tmpPath);
saveStream.pipe(res);
}).on('error', (err) => {
fs.unlink(tmpPath);
showAndLogError(res, 500, 'Error while transmitting the transformed data back to Datagraft', err);
});
});
});
};