Created
August 5, 2012 17:51
-
-
Save wookiehangover/3266276 to your computer and use it in GitHub Desktop.
Uploads Formidable file objects to S3, save to redis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uploads Formidable file objects to S3, save to redis | |
// | |
// TODO - add image processing task | |
var fs = require('fs'); | |
var s3 = require('knox').createClient(); | |
var redis = require('redis'); | |
var client = redis.createClient(); | |
function update( data, cb ){ | |
var hash = [ 'upload:'+ data.id ]; | |
for(var i in data){ | |
if( data.hasOwnProperty(i) ){ | |
hash.push(i); | |
hash.push(data[i]); | |
} | |
} | |
client.hmset(hash, function(err, status){ | |
cb(err, status, data); | |
}); | |
}; | |
exports.process = function( file, data ){ | |
return new Process( file, data ); | |
}; | |
function Process( file, data ){ | |
this.file = file; | |
this.data = data; | |
var stream = fs.createReadStream( file.path ); | |
this.hash({ id: data.id }, stream ); | |
this.upload( file.name, stream ); | |
} | |
Process.prototype.hash = function( data, stream ){ | |
var self = this; | |
var buf = ''; | |
stream.on('data', function(data){ | |
buf += data; | |
}); | |
stream.on('end', function(){ | |
var fileHash = crypto.createHash('md5'); | |
fileHash.update( buf ); | |
data.hash = fileHash.digest('hex').slice(0,8); | |
update( data, function(err){ | |
if( err ){ | |
console.error('Error hashing:'+ err); | |
console.trace(); | |
} | |
}); | |
}); | |
}; | |
Process.prototype.upload = function( path, stream ){ | |
var self = this; | |
var headers = { | |
'Content-Length': this.file.size, | |
'Content-Type': this.file.type | |
}; | |
s3.putStream(stream, path, headers, function(err, res){ | |
if( err ){ | |
console.error('Error uploading: '+ err); | |
console.trace(); | |
return; | |
} | |
res.on('end', function(){ | |
self.data.url = res.req.url; | |
self.data.status = 'uploaded'; | |
self.data.modifiedAt = +new Date(); | |
update( self.data, function(err){ | |
client.publish('uploads', JSON.stringify(self.data)); | |
client.zadd('uploads:global', self.data.createdAt, 'upload:'+ self.data.id ); | |
}); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The only thing that's not self-explanatory is the call to
update
on line 61: update is a light wrapper around redis.hmset()