Skip to content

Commit

Permalink
S3 uploading #likeAboss
Browse files Browse the repository at this point in the history
  • Loading branch information
samccone committed Mar 30, 2013
1 parent f5d8575 commit d19629c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
17 changes: 14 additions & 3 deletions controllers/frames.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var s3Upload = require('../util/images/upload_to_s3');

module.exports = {
find: find,
create: create
Expand All @@ -9,7 +11,16 @@ function find(query, cb) {
}

function create(obj, cb) {
var frame = new global.models.frame(obj);

frame.save(cb)
s3Upload({
path: obj.upload.path,
fileName: encodeURIComponent(obj.upload.name)
}, function(err, d) {
if (err) {
cb(err, null);
} else {
obj.url = d.client._httpMessage.url
var frame = new global.models.frame(obj);
frame.save(cb)
}
});
}
2 changes: 1 addition & 1 deletion router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = function(server, passport) {
controllers.Frames.create({
title: req.body.title,
caption: req.body.caption,
url: 'http://placehold.it/100x100'
upload: req.files.image
}, function(err, obj) {
if (err) {
res.render('frames/new', {errors: err});
Expand Down
13 changes: 13 additions & 0 deletions util/images/upload_to_s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var knox = require('knox');

var client = knox.createClient({
key: process.env['S3_KEY'],
secret: process.env['S3_SECRET'],
bucket: 'ffframe'
});

function upload(props, cb) {
client.putFile(props.path, props.fileName, {'x-amz-acl': 'public-read'}, cb);
}

module.exports = upload;

0 comments on commit d19629c

Please sign in to comment.