-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-database.js
56 lines (40 loc) · 1.65 KB
/
image-database.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
/**
var imdb = require("image-database")
var catalog = imdb.find("/test/", function(){
});
var otherstuff = imdb.find("/stuff/", function(){
});
*/
module.id = "image-database"
function ImageDatabase(imageRootPath, delimiter, done){
var self = this
// valid types list
this.types = ["jpg","png","gif"]
// image root directory ./test/images for example
this.rootDir = imageRootPath || __dirname
// collection property which will be returned through callback
this.collection = {}
this.collectionBuilder = require("./collection-builder")
var dirReader = require("./directory-reader")
dirReader.get({ rootDir : this.rootDir, delimiter : delimiter }, function(files){
self.collection = self.collectionBuilder.build(files, delimiter, done)
})
}
// ImageDatabase.prototype.find = function(collectionFilter) {
// return this.collectionBuilder.find(this.collection, collectionFilter)
// };
ImageDatabase.prototype.getCollectionFilterGroups = function(collectionFilter){
if(collectionFilter.substr(0,1) === "/") collectionFilter = collectionFilter.substr(1)
if(collectionFilter.substr(collectionFilter.length-1,1) === "/") collectionFilter = collectionFilter.substr(0,collectionFilter.length-1)
return collectionFilter.split("/")
}
ImageDatabase.prototype.find = function(collectionFilter, collection){
if(collection == null) collection = this.collection
var filters = this.getCollectionFilterGroups(collectionFilter)
if(filters.length > 1 && collection[filters[0]] != null){
return this.find(filters.splice(1).join("/"), collection[filters[0]])
}
if(collection[filters[0]] != null) return collection[filters[0]]
return false;
}
module.exports = ImageDatabase