Want to advertise here? Contact us!
by

Thank the author with a small contribution using PayPal.

0
241 Views
3 Badges
Top 1% !
Popular
Specified
Popularity: 567th place
Created
Modified Jun 25, 2013
This snippet is Public
No tags for this snippet yet.
LanguageJavaScript
SourceGithub

NodeJS Photo Upload with Azure Storage

NodeJS Photo Upload with Azure Storage: 
AzureStorage.js
post this code
<iframe id="embedFrame" style="width:600px; height:300px;" src="http://www.snip2code.com/Embed/8974/NodeJS-Photo-Upload-with-Azure-Storage"></iframe>
Click on the embed code to copy it into your clipboard Width:Height:
Leave empty to retrieve all the content First Line:Last Line:
exports.addCarPhoto = function(req, res) {
	
	var userID = req.user._id;
	
	var carsBaseURI = "http://sobrioapp.blob.core.windows.net/cars/";
	
	// setup photo meta data
	var type = req.files.photo.type;
	var filename = req.params.id + "-" + req.files.photo.name;
	var path = req.files.photo.path;
	
	// check if it has the right filetype
	if(type == "image/jpeg" || type == "image/jpg" || type == "image/png") {
		
		// initiate a blobService object for Azure
		var blobService = azure.createBlobService();
	
		var options = {
			contentType: type,
			metadata: { fileName: filename }
		}
		
		// Fire off in series so that the tmp photo isn't deleted before its fully uploaded
		async.series({
			block: function(callback) {
				blobService.createBlockBlobFromFile('cars', filename, path, options, callback);
			},
			unlink: function(callback) {
				fs.unlink(path, callback);
			}
		}, function(err, results) {
			
			// Update the user's car with the new photo
			var update = User.findOneAndUpdate({
				"_id": userID,
				"cars._id": req.params.id
			}, {
				$set: {
					'cars.$.photo': carsBaseURI + results.block[0].blob
				}
			});
			
			var promise = update.exec();
			
			promise.addBack(function(err, user) {
				
				if(err) {
					res.send(err);
				} else {
					res.send(user.cars);
				}
				
			});

		});
		
	}
	
	
	
}

If you want to be updated about similar snippets, Sign in and follow our Channels
Originally posted on https://gist.github.com/5855940