Skip to content

Instantly share code, notes, and snippets.

@mdsaleemj
Last active November 21, 2018 12:40
Show Gist options
  • Save mdsaleemj/fc258a6022a0d61f632861f7682d851a to your computer and use it in GitHub Desktop.
Save mdsaleemj/fc258a6022a0d61f632861f7682d851a to your computer and use it in GitHub Desktop.
storing and downloading binary data in mongodb without grid-fs
//The below model is used to book a travel ride.
//The uploaded file is parsed in the formidable multipart body parser and stored in req.files.fileField.
//File is read from , in my code, req.files.file
var rideModel = function(){
var rideSchema = new Schema({
email : String,
corp_id : String,
name : String,
manager: String,
pickup_point : String,
drop_point : String,
reason : String,
priority : String,
travel_time : {type : Date ,default :Date.now },
ticket_id : String,
ticket_status : {
type :String, //ticket status can be OPEN,CANCELLED,APPROVED,REJECTED,
enum : ['OPEN','CANCELLED','APPROVED','REJECTED']
},
driver_details : {
'name' : String,
'phone_number': Number
},
ride_type : String, //single,group
ride_count : Number ,// 1 for single, > 1 for group
ride_attachment : {
name : String,
data : Buffer,
content_type : String,
size : String
},
comments : [{
content : String,
date : {type : Date, default : Date.now},
user_name : String
}]
});
//rideSchema.path('employee_corp_id').required(true);
return mongoose.model('Ride',rideSchema);
};
var rideDetails = {};
rideDetails.corp_id = body.corp_id;
rideDetails.name = body.name;
rideDetails.email = body.email;
rideDetails.manager = body.manager;
//phone_number is assigned above
rideDetails.pickup_point = body.pickup_point;
rideDetails.drop_point = body.drop_point;
rideDetails.reason = body.reason;
rideDetails.priority = body.priority || 'NORMAL';
rideDetails.travel_time = body.travel_time;
//console.log(req);
//if it is outing , you will check for file attachment.
if(req.files.file){
var uploadFile = req.files.file;
var data;
var path = uploadFile.path;
var name = uploadFile.name;
var type = uploadFile.type;
var size = uploadFile.size;
console.log(path);
var stream = fs.createReadStream(path);
stream.on('data',function(chunk){
//console.log(err);
//console.log(data);
data = data + chunk;
});
stream.on('end',function(){
//console.log(buffer);
var buffer = new Buffer(data);
rideDetails.ride_attachment ={};
rideDetails.ride_attachment.data = buffer;
rideDetails.ride_attachment.name = name;
rideDetails.ride_attachment.content_type = type;
rideDetails.ride_attachment.size = size;
console.log('buf len:'+buffer.length);
console.log('bin len:'+data.length);
console.log('size:' + size);
createAndSaveTicket();
});
}else {
createAndSaveTicket();
}
function createAndSaveTicket(){
var ride = new Ride(rideDetails);
//generate unique ticket id
var ticket_id = shortid.generate();
ride.set('ticket_id', ticket_id);
ride.set('ticket_status', 'OPEN');
console.log('New ticket_id: ' +ticket_id);
ride.save(function(err,doc){
if(err){
console.log(err);
res.status('404').json({status : 'false', error: 'Failure due to unknown reason' + err});
}else{
res.send({status : 'true', ticket_id : ticket_id, message : 'You have booked the ride successfully' });
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment