-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathGFS.js
More file actions
712 lines (659 loc) · 21 KB
/
Copy pathGFS.js
File metadata and controls
712 lines (659 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
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
var fs = require('fs');
var path = require('path');
var uuidv4 = require('uuid/v4');
var express = require('express');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var helpers = require('../helpers.js');
var DEBUG = true;
var FSIndexSchema = new mongoose.Schema({
uuid: { type: String, index: true, unique: true },
owner: { type: String, unique: true },
created : { type: Date, default: Date.now },
updated : { type: Date, default: Date.now },
root: { type: mongoose.Schema.Types.Mixed }
}, {
toObject: {
transform: function(doc, ret){
delete ret.__v;
}
},
toJSON: {
transform: function(doc, ret){
delete ret.__v;
}
}
});
/* guarantee uniqueness */
FSIndexSchema.index({ onwer: 1 }, { unique: true });
// The root field needs to be serialized because of
// issues with keys containing "." (dot) character.
// mongodb prohibits the use of a dot character because of the
// ambiguity arising from the dotted-query syntax.
//
// https://docs.mongodb.com/manual/core/document/#field-names
FSIndexSchema.post('init', function(doc){
if (!this.root) this.root = {};
else this.root = JSON.parse(this.root);
});
FSIndexSchema.pre('validate', function(next){
this.root = JSON.stringify(this.root);
next();
});
FSIndexSchema.statics.getDefault = function(){
var self = this;
return self.findOne({ uuid: 'default-global-index' })
.then(function(found){
if (found) return found;
else return self.create({
uuid: 'default-global-index',
owner: 'none'
}).then(function(doc){
// Need to initialize the root property here
// because Mongoose does not invoke the post init hook upon create,
// resulting in doc.root being a JSON string, instead of an object. (i.e. "{}" not {})
doc.root = {};
return doc;
})
})
.catch(function(err){
console.log(err);
return Promise.reject(err);
})
}
FSIndexSchema.methods.read = function(abs_path, depth){
depth = (depth === 0) ? depth : (depth || 1);
var self = this;
var tokens = abs_path.split('/').slice(1); // For now we assume it starts with a forward-slash
if (tokens[tokens.length-1] === '') tokens.pop();
var obj_ref = helpers.getNestedProperty(this.root, tokens);
if (obj_ref){
if (typeof obj_ref === 'string'){
return this.model('FSHandle').findOne({ uuid: obj_ref })
.then(function(handle){
if (handle) return handle.read().then(function(data){
return {
type: 'file',
name: handle.name,
content: data // Currently only supports utf-8 strings...
}
});
else return Promise.reject(new Error("FSHandle reference exists in the FSIndex, but Document '"+obj_ref+"' cannot be found."))
})
}
else if (typeof obj_ref === 'object'){
if (depth > 0){
var completed = helpers.defer();
Promise.all(
Object.keys(obj_ref)
.map(function(name){
return self.read(path.join(abs_path, name), depth - 1)
.then(function(data){
return {
key: name,
value: data
}
})
})
)
.then(function(results){
var result = {
type: 'directory',
name: tokens[tokens.length-1],
content: helpers.deepCopy(obj_ref)
}
results.forEach(function(item){
result.content[item.key] = item.value;
})
completed.resolve(result);
})
.catch(function(err){
console.log(err);
completed.reject(err);
})
return completed.promise;
}
else {
return Promise.resolve({
type: 'directory',
name: tokens[tokens.length-1],
content: helpers.deepCopy(obj_ref)
})
}
}
else {
return Promise.reject(new Error("Mongoose returned an unexpected object"));
}
}
else return Promise.reject(new Error("Not Found"))
}
FSIndexSchema.methods.write = function(abs_path, content, mode){
var self = this;
var tokens = abs_path.split('/').slice(1); // For now we assume it starts with a forward-slash
if (tokens[tokens.length-1] === '') tokens.pop();
var name = tokens[tokens.length-1];
var parent_tokens = tokens.slice(0, tokens.length-1);
var parent = helpers.getNestedProperty(this.root, parent_tokens);
if (parent){
if (typeof parent === 'object'){
var preCommit = helpers.defer();
var commit = helpers.defer();
var existing = parent[name];
if (typeof existing === 'string'){
if (mode === 'append'){
this.model('FSHandle').findOne({ uuid: existing })
.then(function(handle){
if (handle) return handle.read()
else throw new Error("FSHandle reference exists in the FSIndex, but Document '"+existing+"' cannot be found.")
})
.then(function(data){
preCommit.resolve(content + data);
})
.catch(function(err){
console.log(err);
preCommit.reject(err);
})
}
else preCommit.resolve(content);
}
else if (typeof existing === 'object'){
preCommit.reject(new Error("Cannot overwrite a directory"));
}
else preCommit.resolve(content);
preCommit.promise
.then(function(write_content){
var uuid = uuidv4();
return Promise.all([
// Commit the FSHandle
self.model('FSHandle').create({
uuid: uuid,
name: name,
storage: 'db',
content: write_content,
owner: '',
sharing: 'public',
previous: existing
}),
// Commit the FSIndex
(function(resolve, reject){
// need to do it this way (deepCopy) because of the way mongoose "watches"
// the model's fields - https://github.com/Automattic/mongoose/issues/1204
// related FAQ - https://mongoosejs.com/docs/faq.html
var copy = helpers.deepCopy(self.root);
helpers.setNestedProperty(copy, tokens, uuid);
self.root = copy;
return self.save()
})()
])
})
.then(function(results){
// console.log(results[1]);
// skipping read to save round trip
commit.resolve({
type: 'file',
name: name,
content: results[0].content // Currently only supports utf-8 strings...
})
})
.catch(function(err){
// console.log(err);
commit.reject(err);
})
return commit.promise;
}
else if (typeof parent === 'string'){
return Promise.reject(new Error("Cannot create file inside a file"));
}
else {
return Promise.reject(new Error("Mongoose returned an unexpected object"));
}
}
else return Promise.reject(new Error('Parent directory "'+parent_tokens.join('/')+'" does not exist'));
}
FSIndexSchema.methods.makeDirectory = function(abs_path){
var self = this;
var tokens = abs_path.split('/').slice(1); // For now we assume it starts with a forward-slash
if (tokens[tokens.length-1] === '') tokens.pop();
var name = tokens[tokens.length-1];
var parent_tokens = tokens.slice(0, tokens.length-1);
var parent = helpers.getNestedProperty(this.root, parent_tokens);
if (parent){
if (typeof parent === 'object'){
if (parent[name]){
return Promise.reject(new Error(abs_path+" exists, cannot create directory."));
}
else {
// need to do it this way (deepCopy) because of the way mongoose "watches"
// the model's fields - https://github.com/Automattic/mongoose/issues/1204
// related FAQ - https://mongoosejs.com/docs/faq.html
var copy = helpers.deepCopy(self.root);
helpers.setNestedProperty(copy, tokens, {});
self.root = copy;
return self.save().then(function(data){
return {
type: 'directory',
name: name,
content: {}
}
})
}
}
else if (typeof parent === 'string'){
return Promise.reject(new Error("Cannot create file inside a file"));
}
else {
return Promise.reject(new Error("Mongoose returned an unexpected object"));
}
}
else return Promise.reject(new Error('Parent directory "'+parent_tokens.join('/')+'" does not exist'));
}
FSIndexSchema.methods.removePath = function(abs_path, rmdir){
// var self = this;
var tokens = abs_path.split('/').slice(1); // For now we assume it starts with a forward-slash
if (tokens[tokens.length-1] === '') tokens.pop();
var obj_ref = helpers.getNestedProperty(this.root, tokens);
if (obj_ref){
if (typeof obj_ref === 'string'){
// need to do it this way (deepCopy) because of the way mongoose "watches"
// the model's fields - https://github.com/Automattic/mongoose/issues/1204
// related FAQ - https://mongoosejs.com/docs/faq.html
var copy = helpers.deepCopy(this.root);
helpers.deleteNestedProperty(copy, tokens);
this.root = copy;
return this.save()
.then(function(){
return {
type: 'file',
path: abs_path
}
})
}
else if (typeof obj_ref === 'object'){
if (rmdir){
return Promise.resolve({
type: 'directory',
path: abs_path
})
}
else return Promise.reject(new Error("To delete a directory, need to set the second argument to true"));
}
else {
return Promise.reject(new Error("Mongoose returned an unexpected object"));
}
}
else return Promise.reject(new Error("Not Found"))
}
var FSHandleSchema = new mongoose.Schema({
uuid: { type: String, index: true, unique: true },
name: { type: String, default: 'New File' },
owner: { type: String },
sharing: { type: String },
storage: { type: String },
content: { type: String },
previous: { type: String } // this is used to track the history of the file
}, {
toObject: {
transform: function(doc, ret){
delete ret.__v;
}
},
toJSON: {
transform: function(doc, ret){
delete ret.__v;
}
}
})
FSHandleSchema.methods.read = function(){
var self = this;
if (this.storage === 'db') return Promise.resolve(this.content);
else if (this.storage === 'fs') return new Promise(function(resolve, reject){
// when storage option is 'fs',
// the path will be the cwd (current working directory) of the current process
fs.readFile(self.content, function(err, data){
// console.log(err, data);
if (err) reject(err);
else resolve(data.toString()); // Currently only supports utf-8 string
});
})
else return Promise.reject(new Error('Storage Error - Unsupported storage type'));
}
/** FSIndex is a Mongoose Model representing a directory tree */
var FSIndex = mongoose.model('FSIndex', FSIndexSchema);
/** FSHandle is a Mongoose Model representing a File */
var FSHandle = mongoose.model('FSHandle', FSHandleSchema);
/** This is a "userland" object, meant to be explicitly `require`d and used by a user.
* The API provided by this object mirrors the native 'fs' API
* so that it is trivial for a user to switch to using GFS instead.
* e.g.
* var fs = require('fs'); // this line is replaced by the line below
* var fs = require('things-js').gfs(globalScope); // globalScope needs to be passed in to link the Pubsub instance
* @constructor
*/
function GFS(mongoUrl){
var self = this;
this.mongoUrl = mongoUrl;
this.db = null;
mongoose.connect(this.mongoUrl, {
useNewUrlParser: true,
useCreateIndex: true
});
this.db = mongoose.connection;
mongoose.connection.on('connected', function(){
(DEBUG && console.log('Connected to mongodb at: ' + self.mongoUrl));
})
mongoose.connection.on('error', function(err){
(DEBUG && console.log('Mongoose connection error: ' + err));
})
}
/** Read from a file asynchronously; it has the same interface as fs.readFile
* @param {string} path - absolute path to the file (currently relative paths cannot be resolved)
* @param {Function|Object} arg1 - if given a function, it is used as the callback. if given an object, it is used as the options object
* @param {Function} arg2 - if arg1 is an object, this is used as the callback.
*/
GFS.prototype.readFile = function readFile(path, arg1, arg2){
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "readFile(path[,options], callback): You need to provide a callback function";
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.read(path);
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'file') callback(null, Buffer.from(instance.content));
else if (instance.type === 'directory') callback(new Error(path+' is a directory'));
else callback(new Error('Unknown FSHandle type'));
})
.catch(function(err){
// console.log(err);
callback(err);
})
}
/** Write to a file asynchronously; it has the same interface as fs.writeFile
* @param {string} path - absolute path to the file
* @param {string} data - text to write (currently only supports string)
* @param {Function|Object} arg1 - if given a function, it is used as the callback. if given an object, it is used as the options object
* @param {Function} arg2 - if arg1 is an object, this is used as the callback.
*/
GFS.prototype.writeFile = function writeFile(path, data, arg1, arg2){
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "writeFile(path, data[,options], callback): You need to provide a callback function";
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.write(path, data);
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'file') callback(null);
else callback(new Error('FSHandle write result returned incorrect FSHandle type'));
})
.catch(function(err){
console.log(err);
callback(err);
})
}
/** Append to a file asynchronously; it has the same interface as fs.appendFile
* @param {string} path - absolute path to the file
* @param {string} data - text to write (currently only supports string)
* @param {Function|Object} arg1 - if given a function, it is used as the callback. if given an object, it is used as the options object
* @param {Function} arg2 - if arg1 is an object, this is used as the callback.
*/
GFS.prototype.appendFile = function appendFile(path, data, arg1, arg2){
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "appendFile(path, data[,options], callback): You need to provide a callback function";
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.write(path, data, 'append');
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'file') callback(null);
else callback(new Error('FSHandle write result returned incorrect FSHandle type'));
})
.catch(function(err){
callback(err);
})
}
GFS.prototype.unlink = function(path, callback){
if (!(typeof path === 'string' || path instanceof Buffer)){
throw new TypeError("path must be a string or Buffer");
}
if (typeof callback !== 'function'){
console.log('[DEP0013] DeprecationWarning'); // Deprecation warning
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.removePath(path);
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'file') callback(null);
else callback(new Error('FSHandle write result returned incorrect FSHandle type'));
})
.catch(function(err){
callback(err);
})
}
GFS.prototype.mkdir = function(path, arg1, arg2){
var mode, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
mode = arg1;
callback = arg2;
}
else {
throw "mkdir(path[,mode], callback): You need to provide a callback function";
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.makeDirectory(path);
})
.then(function(instance){
// console.log('Write successful', instance);
if (instance.type === 'directory') callback(null);
else callback(new Error('FSIndex makeDirectory result returned incorrect data type'));
})
.catch(function(err){
console.log(err);
callback(err);
})
}
GFS.prototype.readdir = function(path, arg1, arg2){
var options, callback;
if (typeof arg1 === 'function'){
callback = arg1;
}
else if (typeof arg1 === 'object' && typeof arg2 === 'function'){
options = arg1;
callback = arg2;
}
else {
throw "readdir(path[,options], callback): You need to provide a callback function";
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.read(path);
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'directory') callback(null, Object.keys(instance.content));
else if (instance.type === 'file') callback(new Error(path+' is a file'));
else callback(new Error('Unknown FSHandle type'));
})
.catch(function(err){
// console.log(err);
callback(err);
})
}
GFS.prototype.rmdir = function(path, callback){
if (!(typeof path === 'string' || path instanceof Buffer)){
throw new TypeError("path must be a string or Buffer");
}
if (typeof callback !== 'function'){
console.log('[DEP0013] DeprecationWarning'); // Deprecation warning
}
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.removePath(path, true);
})
.then(function(instance){
// console.log(instance);
if (instance.type === 'directory') callback(null);
else callback(new Error('FSHandle write result returned incorrect FSHandle type'));
})
.catch(function(err){
callback(err);
})
}
/* To be implemented: */
GFS.prototype.exists = function(path, callback){ }
GFS.prototype.copyFile = function(src, arg1, arg2, arg3){ }
GFS.prototype.createReadStream = function(path, options){ }
GFS.prototype.createWriteStream = function(path, options){ }
GFS.prototype.rename = function(oldPath, newPath, callback){ }
// TODO: Create read and write stream for faster access of data.
// TODO: Implement GridFS on top of MongoDB for sharding and storing date and other binary formats
/** This is the function that is exported as the gfs module, used to bootstrap the file system.
* After a user `require`s this module, it can be used thus:
* var fs = require('things-js').gfs('mongodb://localhost/my-database');
*/
GFS.bootstrap = function(mongoUrl){
return new GFS(mongoUrl);
}
function createRouter(mongooseConnection){
/** FSIndex is a Mongoose Model representing a directory tree */
var FSIndex = mongooseConnection.model('FSIndex', FSIndexSchema);
/** FSHandle is a Mongoose Model representing a File */
var FSHandle = mongooseConnection.model('FSHandle', FSHandleSchema);
/** FSRouter can be mounted in an express app, serving the filesystem's RESTful API at the mounted path */
var FSRouter = express.Router();
FSRouter.use(express.json());
FSRouter.use(express.urlencoded({ extended: true }));
// FSRouter.use(function(req, res, next){
// res.header("Access-Control-Allow-Origin", "*");
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// })
FSRouter.route(':abs_path(*)')
.get(function(req, res, next){
console.log('[GFS] GET '+req.params.abs_path);
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.read(req.params.abs_path);
})
.then(function(instance){
// console.log(instance);
res.json(instance);
})
.catch(function(err){
console.log(err);
res.status(500).json(err.message);
})
})
.post(function(req, res, next){
console.log('[GFS] POST '+req.params.abs_path);
// console.log(req.body);
var writePath = path.join(req.params.abs_path, req.body.name);
FSIndex.getDefault()
.then(function(index){
// console.log(index);
if (req.body.type === 'file'){
return index.write(writePath, req.body.content);
}
else if (req.body.type === 'directory'){
return index.makeDirectory(writePath);
}
else {
return Promise.reject(new Error("Unrecognized data type"))
}
})
.then(function(instance){
res.json(instance);
})
.catch(function(err){
console.log(err);
res.status(500).json(err.message);
})
})
.delete(function(req, res, next){
console.log('[GFS] DELETE '+req.params.abs_path);
// var ids = req.query.ids.split(',');
FSIndex.getDefault()
.then(function(index){
// console.log(index);
return index.removePath(req.params.abs_path, true);
})
.then(function(instance){
res.json(instance);
})
.catch(function(err){
console.log(err);
res.status(500).json(err.message);
})
});
return FSRouter;
}
/** Return an express app serving the filesystem's RESTful API */
function createServer(port, db_url, onListening){
mongoose.connect(db_url, {
useNewUrlParser: true,
useCreateIndex: true
});
var db = mongoose.connection;
db.on('error', function(){
console.log('[DB] Connection ERROR');
});
db.once('open', function(){
console.log('[DB] Connection SUCCESS')
});
var app = express();
app.use('/', createRouter(db));
app.listen(port, function(){
console.log('GFS RESTful Server bound to port '+port);
if (onListening) onListening();
})
return app;
}
module.exports = {
devland: {
createRouter: createRouter,
createServer: createServer
},
bootstrap: GFS.bootstrap
}