forked from FabMo/FabMo-Engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
db.js
783 lines (710 loc) · 21.4 KB
/
db.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
var assert = require('assert');
var fs = require('fs-extra');
var crypto = require('crypto');
var log = require('./log').logger('db');
var config = require('./config');
var util = require('./util');
var ncp = require('ncp').ncp;
var process = require('process');
var cnctosvg = require("cnctosvg");
// Connect to TingoDB database that stores the files
var Engine = require('tingodb')();
job_queue = new util.Queue();
var db;
var files;
var jobs;
var users;
var thumbnails;
function notifyChange() {
var machine = require('./machine').machine;
if(machine) {
machine.emit('change', 'jobs');
}
}
Job = function(options) {
this.file_id = options.file_id || null;
this.name = options.name || "Untitled Job";
this.description = options.description || "";
this.created_at = Date.now();
this.started_at = null;
this.finished_at = null;
this.state = "pending";
this.order = null;
};
Job.prototype.clone = function(callback) {
var job = new Job({
file_id : this.file_id,
name : this.name,
description : this.description
});
job.save(callback);
};
Job.prototype.update_order = function (order, callback){
log.info("Upating " + this._id ? this._id : '<volatile job>');
this.order = order;
this.save(callback);
};
Job.prototype.start = function(callback) {
log.info("Starting job id " + this._id ? this._id : '<volatile job>');
this.state = 'running';
this.started_at = Date.now();
this.save(callback);
};
Job.prototype.finish = function(callback) {
log.info("Finishing job id " + this._id ? this._id : '<volatile job>');
this.state = 'finished';
this.finished_at = Date.now();
this.save(callback);
};
Job.prototype.fail = function(callback) {
log.info("Failing job id " + (this._id ? this._id : '<volatile job>'));
this.state = 'failed';
this.finished_at = Date.now();
this.save(callback);
};
Job.prototype.cancelOrTrash = function(callback) {
if(this.state === 'running') {
this.cancel(callback);
} else {
this.trash(callback);
}
}
Job.prototype.cancel = function(callback) {
if(this.state === 'running') {
log.debug("Cancelling running job id " + this._id);
this.state = 'cancelled';
this.finished_at = Date.now();
this.save(callback);
} else {
setImmediate(callback, new Error('Cannot cancel a job that is ' + this.state));
}
};
Job.prototype.trash = function(callback) {
if(this.state !== 'running') {
log.debug("Trashing job id " + this._id);
this.state = 'trash';
this.finished_at = Date.now();
this.save(callback);
} else {
setImmediate(callback, new Error('Cannot trash a job that is ' + this.state));
}
};
Job.prototype.save = function(callback) {
if(!this.file_id) {
log.info('Not saving this job because no file_id')
return callback(null, this);
}
delete this.pending_cancel;
jobs.save(this, function(err, record) {
if(!record) {
return;
}
notifyChange();
callback(null, this);
}.bind(this));
};
Job.prototype.delete = function(callback){
jobs.remove({_id : this._id},function(err){
if(err) {
callback(err)
} else {
callback();
notifyChange();
}
});
};
Job.getPending = function(callback) {
jobs.find({state:'pending'}).toArray(callback);
};
Job.getRunning = function(callback) {
jobs.find({state:'running'}).toArray(callback);
};
Job.getHistory = function(options, callback) {
var total = jobs.count({
state: {$in : ['finished', 'cancelled', 'failed']}
}, function(err, total) {
if(err) { return callback(err); }
jobs.find({
state: {$in : ['finished', 'cancelled', 'failed']}
}).skip(options.start || 0).limit(options.count || 0).sort({'created_at' : -1 }).toArray(function(err, data) {
if(err) {
callback(err);
}
callback(null, {
'total_count' : total,
'data' : data
});
});
});
};
Job.getAll = function(callback) {
jobs.find().toArray(function(array) {
callback(array);
});
};
Job.getFileForJobId = function(id, callback) {
Job.getById(id, function(err, document) {
if(err) {
callback(err);
} else {
if(document && document.file_id) {
File.getByID(document.file_id, function(err, file) {
if(err) {
callback(err);
} else {
if(file) {
callback(null, file);
} else {
callback(new Error("Could not find file in database."));
}
}
});
} else {
callback(new Error("Could not find job in database."))
}
}
});
}
// Return a file object for the provided id
Job.getById = function(id,callback)
{
jobs.findOne({_id: id},function(err,document){
if (!document){
callback("No such job ID: " + id, undefined);
return;
}
var job = document;
job.__proto__ = Job.prototype;
callback(null, job);
});
};
Job.getNext = function(callback) {
jobs.find({state:'pending'}).toArray(function(err, result) {
result.sort(function(a, b){
return a.order - b.order;
});
log.info('printing' + result[0]);
if(err) {
callback(err, null);
} else {
if(result.length === 0) {
return callback(Error('No jobs in queue.'));
}
var job = result[0];
job.__proto__ = Job.prototype;
callback(null, job);
}
});
};
Job.dequeue = function(callback) {
Job.getNext(function(err, job) {
if(err) {
callback(err);
} else {
job.start(callback);
}
});
};
Job.deletePending = function(callback) {
jobs.remove({state:'pending'},callback);
};
// The File class represents a fabrication file on disk
// In addition to the filename and path, file statistics such as
// The run count, last time run, etc. are stored in the database.
function File(filename,path, callback){
var that=this;
this.filename = filename;
this.path = path;
this.created = Date.now();
this.last_run = null;
this.run_count = 0;
this.hash = null;
fs.stat(path, function(err, stat) {
if(err) {
throw err;
}
that.size = stat.size;
});
}
File.hash = function(path, callback) {
var fd = fs.createReadStream(path);
var hash = crypto.createHash('md5');
hash.setEncoding('hex');
fd.on('end', function() {
hash.end();
callback(null, hash.read())
});
fd.pipe(hash);
}
// Save information about this file to back to the database
File.prototype.save = function(callback) {
var that = this;
files.findOne({path: that.path},function(err,document){
if (err){
callback(err);
}
else if(document){
// update the current entry in the database instead of creating a new one.
log.info('Updating document id ' + document._id);
files.update({_id : document._id},that,function(){
callback(that);
});
}
else{
log.info('Creating a new document.');
files.insert(that, function(err,records){
if(!err) {
callback(null, records[0]);
}
else {
callback(err);
}
});
}
});
};
// Delete this file (and associated thumbnail) from the database
File.prototype.delete = function(callback){
files.remove({_id : this._id},function(err){if(!err)callback();else callback(err);});
thumbnails.remove({file_id : this._id},function(err){if(!err)callback();else callback(err);});
};
// Update the "last run" time (use the current time)
File.prototype.saverun = function(){
files.update({_id : this._id}, {$set : {last_run : Date.now()}, $inc : {run_count:1}});
};
File.add = function(friendly_filename, pathname, callback) {
// Create a unique name for actual storage
var filename = util.createUniqueFilename(friendly_filename);
var full_path = path.join(config.getDataDir('files'), filename);
// Compute the hash for the file to be added
File.hash(pathname, function(err, hash) {
// Check to see if the hash is already found in the database
files.findOne({hash:hash},function(err,document) {
if(document) {
log.info("Using file with hash of " + hash + " which already exists in the database.");
var file = document;
file.__proto__ = File.prototype;
callback(null, file);
} else {
log.info("Saving a new file with a hash of " + hash + ".");
// Move the file
util.move(pathname, full_path, function(err) {
if(err) {
return callback(err);
}
// delete the temporary file, so that the temporary upload dir does not get filled with unwanted files
fs.unlink(pathname, function(err) {
if (err) {
// Failure to delete the temporary file is bad, but non-fatal
log.warn("failed to remove the job from temporary folder: " + err);
}
var file = new File(friendly_filename, full_path);
file.hash = hash;
file.save(function(err, file){
if(err) {
return callback(err);
}
log.info('Saved a file: ' + file.filename + ' (' + file.path + ')');
callback(null, file)
}.bind(this)); // save
}.bind(this)); // unlink
}); // move
}
});
})
} // add
// Return a list of all the files in the database
File.list_all = function(callback){
files.find().toArray(function(err,result){
if (err){
throw err;
}
callback(result);
});
};
// Return a file object for the provided id
File.getByID = function(id,callback)
{
files.findOne({_id: id},function(err,document){
if (!document) {
callback(true, undefined);
return;
}
var file = document;
file.__proto__ = File.prototype;
callback(null, file);
});
};
// Given a file and metadata, create a new file and job in the database
// callback with the job object if success.
var createJob = function(file, options, callback) {
File.add(options.filename || file.name, file.path, function(err, dbfile) {
if (err) { return callback(err); }
try {
var job = new Job({
file_id : dbfile._id,
name : options.name || file.name,
description : options.description
});
} catch(e) {
log.error(e);
return callback(e);
}
job.save(function(err, job) {
if(err) { return callback(err); }
callback(null, job);
});
});
}
User = function(username,password,isAdmin,created_at,_id) {
this._id = _id;
this.username = username;
this.password = password;
this.isAdmin = isAdmin || false;
this.created_at = created_at || Date.now();
};
User.prototype.validPassword= function(password){
var pass_shasum = crypto.createHash('sha256').update(password).digest('hex');
if(pass_shasum === this.password){
return true;
}else if(password === this.password){
return true;
}else{
return false;
}
};
// Delete this user from the database
User.prototype.delete = function(callback){
users.remove({_id : this._id},function(err){if(!err)callback();else callback(err);});
};
// verify user password and encrypt it.
User.verifyAndEncryptPassword = function(password,callback){
if(!/^([a-zA-Z0-9@*#]{5,15})$/.test(password) ){ //validatepassword
if(callback) callback('Password not valid, it should contain between 5 and 15 characters. The only special characters authorized are "@ * #".',null);
return undefined;
}
var pass_shasum = crypto.createHash('sha256').update(password).digest('hex'); // save encrypted password
if(callback)callback(null,pass_shasum);
return pass_shasum;
};
// Grant user admin status
User.prototype.grantAdmin = function(callback){
this.isAdmin = true;
this.save(callback);
};
// Revoke user admin status
User.prototype.revokeAdmin = function(callback){
this.isAdmin = false;
this.save(callback);
};
User.prototype.save = function(callback){
var user = this;
users.save(user, function(err, record) {
if(err){
log.err(err);
if(callback)callback(err,null);
return;
}
if(!record) {
if(callback)callback(err,null);
return;
}
if(callback)callback(null, this);
}.bind(this));
};
User.add = function(username,password,callback){
if(!/^([a-zA-Z0-9]{3,20})$/.test(username) ){ //validate username
callback('Username not valid, it should contain between 3 and 20 characters. Special characters are not authorized.',null);
return ;
}
User.verifyAndEncryptPassword(password,function(err,pass_shasum){
if (err){callback(err,password);return ;}
users.findOne({username:username},function(err,document) {
if(document){
callback('Username already taken !',null);
return ;
}else{
user = new User(username,pass_shasum);
user.save(callback);
//callback(null,user);
return ;
}
});
})
}
User.findOne = function(username,callback){
users.findOne({username:username},function(err,doc){
if(err){console.log(err);callback(err,null);}
if(doc){
user = new User(doc.username,doc.password,doc.isAdmin,doc.created_at,doc._id);
callback(err,user);
}else{
callback(err);
}
});
}
User.getAll = function(callback){
users.find({},{password:0},function(err,cursor){ // do not returns passwords.
if(err){console.log(err);callback(err,null);}
if(cursor){
var user_array = [];
cursor.toArray(function(err,users){
for(user in users){
user_array.push(new User(users[user].username,users[user].password,users[user].isAdmin,users[user].created_at,users[user]._id));
}
callback(null,user_array);
});
}else{
callback(err);
}
});
}
User.findById = function(id,callback){
users.findOne({_id:id},function(err,doc){
if(err){console.log(err);callback(err,null);return;}
if(doc){
user = new User(doc.username,doc.password,doc.isAdmin,doc.created_at,doc._id);
callback(err,user);
return;
}else{
callback("user doesn't exist!");
return;
}
});
}
// Creates a new Thumbnail which represents the thumbnails stored in the
// database. Thumbnails are 2D representation of the path a file is describing
// (in G-Code or OpenSBP).
//
// @param {Document} [thumbnailDocument] - The thumbnail stored in the database.
Thumbnail = function(thumbnailDocument) {
if(thumbnailDocument) {
this.file_id = thumbnailDocument.file_id;
this.version = thumbnailDocument.version;
this.image = thumbnailDocument.image;
} else {
this.file_id = "";
this.version = 0;
this.image = "";
}
}
// Checks if needs update.
// @return {boolean} If needs update.
Thumbnail.prototype.needUpdate = function() {
return this.version < cnctosvg.VERSION;
};
// Updates the thumbnail in the database.
// @param {function} callback({Error} err, {Thumbnail} thumbnail): if err is
// not null, thumbnail is old one else new one
Thumbnail.prototype.update = function(callback) {
var that = this;
File.getByID(that.file_id, function(err, file) {
if(err) {
callback(new Error("Cannot find file with id = " + that.file_id), that);
return;
}
var machine = require('./machine').machine;
machine.getGCodeForFile(file.path, function(err, gcode) {
if(err) {
callback(new Error("Cannot find G-Code for file with id = " + fileId), that);
return;
}
var gcodeString = gcode.toString("utf8");
var modifications = {
"image" : Thumbnail.createImage(gcodeString, file.filename),
"version" : cnctosvg.VERSION
};
var query = { "file_id" : that.file_id };
thumbnails.update(query, modifications, function(err, thumbnail) {
if(err) {
callback(new Error("Cannot update thumbnail with file_id = " + that.file_id), that);
return;
}
that.image = modifications.image;
that.version = modifications.version;
callback(null, that);
});
});
});
};
// Checks if needs update and returns himself to callback (the new or old
// version)
// @param {function} callback({boolean} err, {Thumbnail} thumbnail): err is
// always false, thumbnail is old one or news one
Thumbnail.prototype.checkUpdateAndReturn = function(callback) {
var that = this;
if(that.needUpdate()) {
that.update(function(err, thumbnail) {
callback(false, thumbnail);
});
} else {
callback(false, that);
}
};
// Creates image but does not add a thumbnail into the database
// @param {string} gcode
// @param {string} title
// @return {string} the image
Thumbnail.createImage = function(gcode, title) {
var colors = { G1 : "#000000", G2G3 : "#000000" };
var width = 100;
var height = 100;
var lineThickness = 2;
return cnctosvg.createSVG(gcode, colors, title, width, height, lineThickness, true);
};
// Generates the thumbnail and insert it in the database
// @param {function} callback({Error} err, {Thumbnail} thumbnail): if err is
// not null, thumbnail is undefined else new one
Thumbnail.generate = function(fileId, callback) {
thumbnails.findOne({ "file_id" : fileId }, function(err, thumbnail) {
if(!err && thumbnail) {
new Thumbnail(thumbnail).checkUpdateAndReturn(callback);
return;
}
File.getByID(fileId, function(err, file) {
if(err) {
callback(new Error("Cannot find file with id = " + fileId));
return;
}
var machine = require('./machine').machine;
machine.getGCodeForFile(file.path, function(err, gcode) {
if(err) {
callback(new Error("Cannot find G-Code for file with id = " + fileId));
return;
}
var gcodeString = gcode.toString("utf8");
var newThumbnail = new Thumbnail();
newThumbnail.file_id = fileId;
newThumbnail.version = cnctosvg.VERSION;
newThumbnail.image = Thumbnail.createImage(gcodeString, file.filename);
thumbnails.insert(newThumbnail, function(err, records) {
if(err) {
callback(new Error("Cannot insert thumbnail in database"));
} else {
callback(null, newThumbnail);
}
});
});
});
});
};
// Get the thumbnail, if no thumbnail in database: try to make one and return it
// @param {function} callback({Error} err, {Thumbnail} thumbnail): if err is
// not null, thumbnail is undefined else the found one
Thumbnail.getFromFileId = function(fileId, callback) {
thumbnails.findOne({ "file_id" : fileId }, function(err, thumbnail) {
if(err) {
callback(new Error("Cannot find thumbnail with file_id = " + fileId));
return;
}
if(!thumbnail) {
Thumbnail.generate(fileId, callback);
} else {
new Thumbnail(thumbnail).checkUpdateAndReturn(callback);
}
});
};
// Get the thumbnail, if no thumbnail in database: try to make one and return it
// @param {function} callback({Error} err, {Thumbnail} thumbnail): if err is
// not null, thumbnail is undefined else the found one
Thumbnail.getFromJobId = function(jobId, callback) {
Job.getById(jobId, function(err, job) {
if(err || !job) {
callback(new Error("Cannot find job with id = " + jobId));
} else {
Thumbnail.getFromFileId(job.file_id, callback);
}
});
}
checkCollection = function(collection, callback) {
collection.find().toArray(function(err, data) {
if(err) {
log.error("Error reading " + collection.collectionName + " from the database: " + err);
callback(err);
} else {
callback(null);
}
});
}
backupDB = function(callback) {
src = config.getDataDir('db');
dest = config.getDataDir('backup') + '/db'
ncp(src, dest, function(err) {
log.info('Backed up database to '+dest)
callback(err);
});
}
exports.configureDB = function(callback) {
db = new Engine.Db(config.getDataDir('db'), {});
files = db.collection("files");
jobs = db.collection("jobs");
users = db.collection("users");
thumbnails = db.collection("thumbnails");
users.find({}).toArray(function(err,result){ //init the user database with an admin account if it's empty
if (err){
throw err;
}
if(result.length === 0 ){
var pass_shasum = crypto.createHash('sha256').update("go2fabmo").digest('hex');
user = new User("admin",pass_shasum,true);
user.save();
}
});
async.parallel([
function(cb) {
checkCollection(files, cb);
},
function(cb) {
checkCollection(jobs, cb);
},
function(cb) {
checkCollection(thumbnails, cb);
}
],
function(err, results) {
if(err) {
log.error('There was a database corruption issue!')
var src = config.getDataDir('db')
var dest = config.getDataDir('debug') + '/bad-db-' + (new Date().getTime())
ncp(src, dest, function (err) {
if(err) {
log.error('The database could not be successfully backed up: ' + err);
callback(null);
} else {
log.debug('The database has been successfully copied to the debug directory for inspection.');
fs.remove(src, function (err) {
if(err) {
log.error('Could not delete the corrupted database:' + err);
callback(null);
} else {
log.debug('The corrupted database has been deleted. Shutting down the engine...');
process.exit(1);
}
});
}
});
} else {
log.info("Databases are clean.");
callback(null);
}
});
};
exports.cleanup = function(callback) {
jobs.update({state: 'running'}, {$set : {state:'failed', finished_at : Date.now()}}, {multi:true}, function(err, result) {
if(err) {
log.error('There was a problem cleaning the db: ' + err)
} else {
if(result > 1) {
log.warn("Found more than one (" + result + ") running job in the db. This is a database inconsistency!");
} else if(result == 1) {
log.info("Cleaned up a single failed job.");
}
}
});
setImmediate(callback, null);
};
exports.File = File;
exports.Job = Job;
exports.User = User;
exports.Thumbnail = Thumbnail;
exports.createJob = createJob;