-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
129 lines (106 loc) · 2.98 KB
/
Copy pathdb.js
File metadata and controls
129 lines (106 loc) · 2.98 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
/**
* Created by jamiecho on 6/14/16.
*/
/* Manipulate FileSystem */
var fs = require('fs');
var path = require('path');
var async = require('async');
/* Initialize Mongoose */
var mongoose = require('mongoose');
mongoose.connect('mongodb://sangue:jcho5985@ds037185.mlab.com:37185/ycho');
/* Initialize GridFS */
var gridfs = require('gridfs-stream');
gridfs.mongo = mongoose.mongo;
/* Establish Connection */
var con = mongoose.connection;
var gfs = null;
con.on('error', function (err) {
console.log('ERROR!!');
throw err;
});
con.once('open', function () {
console.log('MONGODB Connection established');
//initialize gridfs
gfs = gridfs(con.db);
});
/* Tag Info Schema */
var ObjectId = mongoose.Schema.Types.ObjectId;
var tagSchema = mongoose.Schema({ //todo : parser fisher/tag info
personInfo : String,
tagInfo : String,
photo : String
});
var tagModel = mongoose.model('tag',tagSchema);
/* Create Temporary Directory */
var tmp_dir = path.join(__dirname, 'tmp');
if (!fs.existsSync(tmp_dir))
fs.mkdirSync(tmp_dir);
function imwrite(photo) {
//copies from ephemeral filesystem
var writestream = gfs.createWriteStream({
filename: photo.name
});
return fs.createReadStream(photo.path).pipe(writestream);
}
function imread(name) {
//copies to epheemral filesystem
var readstream = gfs.createReadStream({
filename: name
});
var writestream = fs.createWriteStream(path.join(tmp_dir, name));
return readstream.pipe(writestream);
}
function write(personInfo,tagInfo,photo,callback){
console.log(personInfo,tagInfo,photo);
async.parallel([
function(callback){
console.log("writing image");
return imwrite(photo).on('finish',callback);
},
function(callback){
console.log('Writing to tagmodel');
var entry = new tagModel({personInfo:personInfo,tagInfo:tagInfo,photo:photo.name});
entry.save(callback);
}
], callback);
}
function read(id, callback){
//callback(res)
async.waterfall([
function(callback){
tagModel.findById(id, function(err,res){
if(err) throw err;
callback(null,res);
})
},
function(res,callback){
imread(res.photo).on('close',function(file){
callback(null,res);
});
}
], callback);
}
function find(query, callback){
if(query)
tagModel.find(query,callback);
else
return tagModel.find({},callback);
}
exports.imwrite = imwrite;
exports.imread = imread;
exports.write = write;
exports.read = read;
exports.find = find;
exports.delete = function(id, callback){
tagModel.findById(id,function(err,doc){
console.log(doc);
gfs.remove({filename : doc.photo},function(err){
if(err){
console.log(err);
}else{
doc.remove();
console.log("REMOVE SUCCESS");
}
});
});
};