-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathinspection.js
435 lines (387 loc) · 15.5 KB
/
inspection.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
var _ = require('lodash');
var defaultLog = require('winston').loggers.get('default');
var mongoose = require('mongoose');
var Actions = require('../helpers/actions');
var Utils = require('../helpers/utils');
var mime = require('mime-types');
var uploadDir = process.env.UPLOAD_DIRECTORY || './uploads/';
var MinioController = require('../helpers/minio');
var rp = require('request-promise-native');
exports.protectedOptions = function (args, res) {
res.status(200).send();
};
// Create a new inspection
exports.protectedPostInspection = async function (args, res) {
var obj = args.swagger.params.inspection.value;
defaultLog.info('Incoming new object:', obj);
var Inspection = mongoose.model('Inspection');
var inspection = new Inspection(obj);
inspection.startDate = obj.startDate;
inspection.endDate = obj.endDate;
if (obj.project) {
inspection.project = mongoose.Types.ObjectId(obj.project);
} else {
inspection.customInspectionName = obj.customInspectionName;
}
// Define security tag defaults
inspection.read = ['sysadmin', 'inspector'];
inspection.write = ['sysadmin', 'inspector'];
inspection.delete = ['sysadmin', 'inspector'];
inspection._createdBy = args.swagger.params.auth_payload.preferred_username;
inspection._createdDate = Date.now();
// This is an overwrite in case of error from mobile app,
// otherwise this will create a new record.
try {
let elementDocument = await Inspection.findOne({_schemaName: 'Inspection', inspectionId: inspection.inspectionId});
if (elementDocument) {
// We alrady had this - send it back to them.
return Actions.sendResponse(res, 200, elementDocument);
} else {
inspection.save()
.then(function (doc) {
Utils.recordAction('Post', 'Inspection', args.swagger.params.auth_payload.preferred_username, doc._id);
doc.status = 'Uploading';
return Actions.sendResponse(res, 200, doc);
})
.catch(function (err) {
console.log('Error in API:', err);
return Actions.sendResponse(res, 400, err);
});
}
} catch (err) {
console.log('Error in API:', err);
return Actions.sendResponse(res, 400, err);
}
};
// Create a new inspection element
exports.protectedPostElement = async function (args, res) {
var obj = args.swagger.params.inspection.value;
var inspId = args.swagger.params.inspId.value;
defaultLog.info('Incoming new object:', obj);
if (obj && obj.elementId && !mongoose.Types.ObjectId.isValid(obj.elementId)) {
return Actions.sendResponse(res, 400, { });
}
var InspectionElement = mongoose.model('InspectionElement');
var inspectionElement = new InspectionElement(obj);
// Define security tag defaults
inspectionElement.read = ['sysadmin', 'inspector'];
inspectionElement.write = ['sysadmin', 'inspector'];
inspectionElement.delete = ['sysadmin', 'inspector'];
inspectionElement._createdBy = args.swagger.params.auth_payload.preferred_username;
inspectionElement._createdDate = Date.now();
// If this exists already, don't bother to insert.. just return 200 OK
// Based on elementId (as per mobile uuid); We don't want to modify it in case there
// are other elements that exist in the elements array
var Inspection = mongoose.model('Inspection');
let elementDocument = await Inspection.findOne({_schemaName: 'InspectionElement', elementId: inspectionElement.elementId});
if (elementDocument) {
// We alrady had this - send it back to them.
return Actions.sendResponse(res, 200, elementDocument);
} else {
// Couldn't find that id, let it pass through.
let theDoc = null;
inspectionElement.save()
.then(function (doc) {
theDoc = doc;
var Inspection = mongoose.model('Inspection');
return Inspection.update(
{ _id: mongoose.Types.ObjectId(inspId) },
{
$push: {
elements: doc._id
}
},
{ new: true }
);
})
.then(function () {
Utils.recordAction('Post', 'InspectionElement', args.swagger.params.auth_payload.preferred_username, theDoc._id);
return Actions.sendResponse(res, 200, theDoc);
})
.catch(function (err) {
console.log('Error in API:', err);
return Actions.sendResponse(res, 400, err);
});
}
};
// Create element item
exports.protectedPostElementItem = async function (args, res) {
var upfile = args.swagger.params.upfile.value;
var guid = args.swagger.params.itemId.value;
var project = null;
if (args.swagger.params.project.value) {
project = args.swagger.params.project.value;
}
var elementId = args.swagger.params.elementId.value;
var type = args.swagger.params.type.value;
var timestamp = args.swagger.params.timestamp.value;
var caption = args.swagger.params.caption.value;
var text = args.swagger.params.text.value;
var geo = args.swagger.params.geo.value;
if (args.swagger.params.elementId && args.swagger.params.elementId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.elementId.value)) {
return Actions.sendResponse(res, 400, { });
}
if (args.swagger.params.project && args.swagger.params.project.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.project.value)) {
return Actions.sendResponse(res, 400, { });
}
if (args.swagger.params.itemId && args.swagger.params.itemId.value && !mongoose.Types.ObjectId.isValid(args.swagger.params.itemId.value)) {
return Actions.sendResponse(res, 400, { });
}
var ext, tempFilePath = null;
if (upfile) {
ext = mime.extension(args.swagger.params.upfile.value.mimetype);
tempFilePath = uploadDir + guid + '.' + ext;
var fs = require('fs');
fs.writeFileSync(tempFilePath, args.swagger.params.upfile.value.buffer);
console.log('wrote file successfully.', tempFilePath);
console.log(MinioController.BUCKETS.DOCUMENTS_BUCKET,
project,
upfile.originalname,
tempFilePath);
MinioController.putDocument(MinioController.BUCKETS.DOCUMENTS_BUCKET,
project,
upfile.originalname,
tempFilePath)
.then(async function (minioFile) {
console.log('putDocument:', minioFile);
// remove file from temp folder
fs.unlinkSync(tempFilePath);
console.log('unlink');
var InspectionItem = mongoose.model('InspectionItem');
var doc = new InspectionItem();
// Set the mobile itemId
doc.itemId = guid;
// Define security tag defaults
if (project) {
doc.project = mongoose.Types.ObjectId(project);
}
doc._addedBy = args.swagger.params.auth_payload.preferred_username;
doc._createdDate = new Date();
doc.read = ['sysadmin', 'inspector'];
doc.write = ['sysadmin', 'inspector'];
doc.delete = ['sysadmin', 'inspector'];
doc.type = type;
doc.caption = caption;
doc.timestamp = timestamp;
doc.geo = JSON.parse(geo);
doc.internalURL = minioFile.path;
doc.internalExt = minioFile.extension;
doc.internalSize = upfile.size;
doc.internalMime = upfile.mimetype;
let itemDocument = await InspectionItem.findOne({_schemaName: 'InspectionItem', itemId: doc.itemId});
if (itemDocument) {
// We already had this - send it back to them.
return Actions.sendResponse(res, 200, itemDocument);
} else {
var savedDocument = null;
doc.save()
.then(function (d) {
defaultLog.info('Saved new document object:', d._id);
Utils.recordAction('Post', 'InspectionItem', args.swagger.params.auth_payload.preferred_username, d._id);
savedDocument = d;
return;
}).then(function () {
// Push this into the inspection elements' items' array for things.
var InspectionElement = mongoose.model('InspectionElement');
return InspectionElement.update(
{ _id: mongoose.Types.ObjectId(elementId) },
{
$push: {
items: doc
}
},
{ new: true }
);
}).then(function (theInspection) {
console.log('updated insp:', theInspection);
return theInspection;
}).then(function () {
return Actions.sendResponse(res, 200, savedDocument);
})
.catch(function (error) {
console.log('error:', error);
// the model failed to be created - delete the document from minio so the database and minio remain in sync.
MinioController.deleteDocument(MinioController.BUCKETS.DOCUMENTS_BUCKET, doc.project, doc.internalURL);
return Actions.sendResponse(res, 400, error);
});
}
});
} else {
// Just a text element.
var InspectionItem = mongoose.model('InspectionItem');
var doc = new InspectionItem();
doc.itemId = guid;
// Define security tag defaults
if (project) {
doc.project = mongoose.Types.ObjectId(project);
}
doc._addedBy = args.swagger.params.auth_payload.preferred_username;
doc._createdDate = new Date();
doc.read = ['sysadmin', 'inspector'];
doc.write = ['sysadmin', 'inspector'];
doc.delete = ['sysadmin', 'inspector'];
doc.text = JSON.parse(text);
doc.markModified('text');
doc.type = type;
doc.geo = JSON.parse(geo);
doc.markModified('geo');
let itemDocument = await InspectionItem.findOne({_schemaName: 'InspectionItem', itemId: doc.itemId});
if (itemDocument) {
// We alrady had this - send it back to them.
return Actions.sendResponse(res, 200, itemDocument);
} else {
var savedDocument = null;
doc.save()
.then(function (d) {
defaultLog.info('Saved new document object:', d._id);
Utils.recordAction('Post', 'InspectionItem', args.swagger.params.auth_payload.preferred_username, d._id);
savedDocument = d;
return;
}).then(function () {
// Push this into the inspection elements' array for things.
var InspectionElement = mongoose.model('InspectionElement');
return InspectionElement.update(
{ _id: mongoose.Types.ObjectId(elementId) },
{
$push: {
items: doc
}
},
{ new: true }
);
}).then(function (theInspection) {
console.log('updated insp:', theInspection);
return theInspection;
}).then(function () {
return Actions.sendResponse(res, 200, savedDocument);
})
.catch(function (error) {
console.log('error:', error);
// the model failed to be created - delete the document from minio so the database and minio remain in sync.
MinioController.deleteDocument(MinioController.BUCKETS.DOCUMENTS_BUCKET, doc.project, doc.internalURL);
return Actions.sendResponse(res, 400, error);
});
}
}
};
exports.protectedElementItemGet = function (args, res) {
var self = this;
self.scopes = args.swagger.params.auth_payload.realm_access.roles;
self.thumbnail = args.swagger.params.thumbnail && args.swagger.params.thumbnail.value === 'true' ? true : false;
if (args.swagger.params.filename && args.swagger.params.filename.value) {
self.filename = args.swagger.params.filename.value;
}
console.log('self.thumbnail:', self.thumbnail);
console.log('self.filename:', self.filename);
defaultLog.info('args.swagger.params:', args.swagger.params.auth_payload.realm_access.roles);
// Build match query if on elemId route
var query = {};
if (args.swagger.params.itemId && args.swagger.params.itemId.value) {
query = Utils.buildQuery('_id', args.swagger.params.itemId.value, query);
}
// Set query type
_.assignIn(query, { '_schemaName': 'InspectionItem' });
console.log('QE:', query);
Utils.runDataQuery('InspectionItem',
args.swagger.params.auth_payload.realm_access.roles,
query,
['internalURL', 'documentFileName', 'internalMime', 'internalExt'], // Fields
null, // sort warmup
null, // sort
null, // skip
null, // limit
false) // count
.then(function (data) {
if (data && data.length === 1) {
var blob = data[0];
var fileMeta;
// check if the file exists in Minio
return MinioController.statObject(MinioController.BUCKETS.DOCUMENTS_BUCKET, blob.internalURL)
.then(function (objectMeta) {
if (!objectMeta) {
// ObjectMeta was null, drop through all the chains below.
return null;
}
fileMeta = objectMeta;
// get the download URL
return MinioController.getPresignedGETUrl(MinioController.BUCKETS.DOCUMENTS_BUCKET, blob.internalURL);
}, function () {
return Actions.sendResponse(res, 404, {});
})
.then(function (docURL) {
if (!docURL) {
// ObjectMeta was null
return Actions.sendResponse(res, 404, {});
}
Utils.recordAction('Download', 'InspectionItem', args.swagger.params.auth_payload.preferred_username, args.swagger.params.docId && args.swagger.params.docId.value ? args.swagger.params.docId.value : null);
// stream file from Minio to client
// res.setHeader('Content-Length', fileMeta.size);
res.setHeader('Content-Type', fileMeta.metaData['content-type']);
res.setHeader('Content-Disposition', 'attachment;filename="' + self.filename + '"');
if (!self.thumbnail) {
console.log('Getting full');
return rp(docURL).pipe(res);
} else {
var axios = require('axios');
// Setup a downloader function.
const download = url => axios({
method: 'get',
url,
responseType: 'stream',
}).then(response => response.data);
// Download the file and pipe the generated thumbnail to it, streaming to the client.
// TODO: Doesn't take into account orientation.
return download(docURL)
.then(response => response.pipe(transform({
height: parseInt(100, 10),
// quality: q && parseInt(q, 10),
width: parseInt(100, 10),
})).pipe(res))
.catch(err => {
console.log('ERR:', err);
res.status(404).send();
});
}
});
} else {
return Actions.sendResponse(res, 404, {});
}
});
};
const isNumeric = n => !isNaN(parseFloat(n)) && isFinite(n);
const transform = ({
blur,
cropMode,
height,
width,
quality,
}) => {
var sharp = require('sharp');
const sharpObj = sharp();
// Width and height set...
if (Number.isInteger(height) && Number.isInteger(width)) {
sharpObj.resize(width, height);
// Only width set...
} else if (isNumeric(width)) {
sharpObj.resize(width);
// Only height set...
} else if (isNumeric(height)) {
sharpObj.resize(null, height);
}
// Blur
if (isNumeric(blur)) {
// Clamp between 0.3 and 1000
sharpObj.blur(Math.min(1000, Math.max(blur, 0.3)));
}
// Crop mode
if (sharp.gravity[cropMode]) {
sharpObj.crop(sharp.gravity[cropMode]);
} else if (sharp.strategy[cropMode]) {
sharpObj.crop(sharp.strategy[cropMode]);
}
// JPEG quality
sharpObj.jpeg({
quality: isNumeric(quality) ? Math.max(1, Math.min(100, quality)) : 80,
});
return sharpObj;
};