-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
279 lines (255 loc) · 7.28 KB
/
mongo.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
/*MONGODB vars:*/
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/test';
var Mongoose = require('./mongooses.js');
//TODO: check URL GEN... maybe better for smaller string
function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
};
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
var insertNewAlbum = function(ownerName, title, desc, key, isProtected, callback) {
var genURL = guidGenerator();
var savedAlbum;
Mongoose.Album.find({}, function(err, albums) {
albums.forEach(function(album) {
if (album.url === genURL) {
//the genURL is not unique
genURL = guidGenerator();
} else {
//the genURL is indeed unique
}
});
});
if(key) {
var AlbumGoose = new Mongoose.Album(
{ owner: ownerName,
title: title,
description: desc,
url: genURL,
key: key,
isProtected: isProtected
} );
} else {
var AlbumGoose = new Mongoose.Album(
{ owner: ownerName,
title: title,
description: desc,
url: genURL,
isProtected: isProtected
} );
}
AlbumGoose.save(function(err, album) {
if (err) return console.error(err);
savedAlbum = album;
callback(album.url);
});
console.log(album + "album saved");
console.log("callback guid: " + genURL);
/*db.collection('albums').insertOne( {
"owner" : ownerName,
"title" : title,
"description" : desc,
"images" : [{ }]
}, function(err, result) {
assert.equal(err, null);
console.log("New album inserted:\n owner: "+ownerName+";\n title: " + title +";\n descrption: "+desc);
callback();
});*/
};
var getAllImagesInAlbum = function(ownerName, album, callback) {
Mongoose.Album.findOne( { owner: ownerName, title:album}, function(err, album) {
var images = [];
callback(album.images);
return album.images;
} );
}
var getAlbum = function(ownerName, album, callback) {
Mongoose.Album.findOne( { owner: ownerName, title:album}, function(err, album) {
var images = [];
callback(album);
return album;
} );
}
var getAlbumByURL = function(url, callback) {
Mongoose.Album.findOne( { url: url }, function(err, album) {
console.log("URL: " + url + "\n\n" + album);
callback(album);
return album;
} );
}
var getAllUserAlbums = function(userName, callback) {
Mongoose.Album.find( {owner:userName}, function(err, albums) {
if(err) return console.error(err);
var albumTits = [];
for(i=0;i<albums.length;i++) {
console.log(albums[i]);
albumTits.push(albums[i].title);
}
return albumTits;
});
}
var checkAlbumProtection = function(guid, callback) {
Mongoose.Album.findOne( {url:guid}, function(err, album) {
if(err) return console.error(err);
if(album.key) {
console.log("album '"+guid+"' is protected");
return true;
} else {
return false;
}
});
}
var insertImageToAlbum = function(filename, owner, album, callback) {
var imageGoose = new Mongoose.Image( { date: Date.now(),
filename: filename,
meta: {
favs: 0
}
} );
Mongoose.Album.findOne( { owner:owner, title:album }, function (err, album) {
if (err)
return console.error(err);
console.log("FOUND ALBUM: " + album);
album.images.push(imageGoose);
album.save(function (err) {
if(err) {
console.error(err);
}
})
console.log(album);
});
imageGoose.save(function(err, image) {
if (err) return console.error(err);
});
console.log("images pushed succesfully");
}
var insertMongooseImagesToAlbum = function(images, guid, callback) {
var albumUpdated;
Mongoose.Album.findOne( { url:guid }, function (err, album) {
console.log("Album to be uploaded tooooooo: " + album);
if (err)
return console.error(image.filename + "had ERROR: "+err);
console.log("FOUND ALBUM: " + album);
images.forEach(function(image) {
album.images.push(image);
});
album.save(function (err) {
if(err) {
console.error(err);
}
});
albumUpdated = album;
});
images.forEach(function(image) {
image.save(function(err, image) {
if (err) return console.error(err);
console.log(image.filename + " pushed succesfully");
});
});
callback(albumUpdated);
}
var createAlbum = function(ownerName, title, desc, key, isProtected, monImages, callback) {
var genURL = guidGenerator();
var savedAlbum;
Mongoose.Album.find({}, function(err, albums) {
albums.forEach(function(album) {
if (album.url === genURL) {
//the genURL is not unique
genURL = guidGenerator();
} else {
//the genURL is indeed unique
}
});
});
if(key) {
var AlbumGoose = new Mongoose.Album(
{ owner: ownerName,
title: title,
description: desc,
url: genURL,
images: monImages,
key: key,
isProtected: isProtected
} );
} else {
var AlbumGoose = new Mongoose.Album(
{ owner: ownerName,
title: title,
description: desc,
url: genURL,
images: monImages,
isProtected: isProtected
} );
}
AlbumGoose.save(function(err, album) {
if (err) return console.error(err);
savedAlbum = album;
callback(album.url);
});
}
function QUERY_getAllUserAlbums(user) {
return Mongoose.Album.find( {owner: user} );
}
module.exports = {
DB_insertImageToAlbum : function (filename, owner, album) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertImageToAlbum(filename, owner, album, key, function() {
db.close();
console.log("db close");
});
});
},
DB_insertMongooseImagesToAlbum : function (image, guid) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertMongooseImagesToAlbum(image, guid, function() {
db.close();
console.log("db close");
});
});
},
DB_insertNewAlbum : function (ownerName, title, desc, key, isProtected) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertNewAlbum(ownerName, title, desc, key, isProtected, function() {
db.close();
console.log("db close");
});
});
},
DB_createAlbum : function(ownerName, title, desc, key, isProtected, images, callback) {
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
createAlbum(ownerName, title, desc, key, isProtected, images, function(url) {
db.close();
console.log("db close");
callback(url);
});
});
},
DB_getAllUserAlbums : function(userName, callback) {
return QUERY_getAllUserAlbums(userName).lean().exec(function(err, albums) {
var ret = [];
callback(albums);
console.log("MOGNOJS: " + albums);
return (albums);
});
},
DB_getAllImagesInAlbum : function(ownerName, album, callback) {
return getAllImagesInAlbum(ownerName, album, callback);
},
DB_getAlbumByName : function(ownerName, album, callback) {
return getAlbum(ownerName, album, callback);
},
DB_getAlbumByURL : function(url, callback) {
return getAlbumByURL(url, callback);
},
DB_checkAlbumProtection : function(url, callback) {
return checkAlbumProtection(url, callback);
}
}