Skip to content

Commit

Permalink
amazon to store images
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Schmidt committed May 25, 2014
1 parent 3fb7d77 commit 8262cfd
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ logs
.DS_Store

github.json
aws.json
uploads

# Runtime data
Expand Down
16 changes: 16 additions & 0 deletions helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var github = {};
var aws = {};

try {
github = require('./github.json');
Expand All @@ -9,12 +10,26 @@ try {
}
}


try {
aws = require('./aws.json');
} catch (e) {
aws = {
"ID" : "",
"Secret" : ""
}
}

var config = {
"github" : {
"GITHUB_CLIENT_ID" : process.env.GITHUB_CLIENT_ID ? process.env.GITHUB_CLIENT_ID : github.GITHUB_CLIENT_ID,
"GITHUB_CLIENT_SECRET" : process.env.GITHUB_CLIENT_SECRET ? process.env.GITHUB_CLIENT_SECRET : github.GITHUB_CLIENT_SECRET,
"callbackURL" : process.env.callbackURL ? process.env.callbackURL : "http://localhost:3000/users/auth/github/callback"
},
"aws" : {
"ID" : process.env.AWS_ID ? process.env.AWS_ID : aws.ID,
"Secret" : process.env.AWS_Secret ? process.env.AWS_Secret : aws.Secret
},
"mongourl" : process.env.mongodburl ? process.env.mongodburl : "mongodb://localhost:27017/odlthek"
}

Expand Down Expand Up @@ -98,6 +113,7 @@ var checkGadgetBooking = function(_gid,_start,_end,_callback) {

var helper = {
github : config.github,
aws : config.aws,
db : db,
ensureAuthenticated : ensureAuthenticated,
ensureAdmin : ensureAdmin,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"passport": "~0.2.0",
"passport-github": "*",
"express-session":"~1.2.0",
"multer":"~0.0.7"
"multer":"~0.0.7",
"knox": "*"
}
}
35 changes: 28 additions & 7 deletions routes/gadgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var gadgets = helper.db.collection('gadgets');
var bookings = helper.db.collection('bookings');
var users = helper.db.collection('users');

var knox = require('knox');

var router = express.Router();


Expand Down Expand Up @@ -134,13 +136,32 @@ router.get('/:id/edit',helper.ensureAuthenticated, helper.ensureAdmin, function(



router.post('/:id/upload', helper.ensureAuthenticated, helper.ensureAdmin, function(req, res) {
gadgets.findById(req.params.id,function(_err,_gadget){
_gadget.image = req.files.image.path.replace(/public\//,'');
gadgets.save(_gadget,function(_err,_result){
res.render('gadgets/edit', { title: _gadget.name , gadget : _gadget});
})
})
router.post('/:id/upload', function(req, res) {
var s3 = knox.createClient({
key: helper.aws.ID,
secret: helper.aws.Secret,
bucket: "odlthek2"
});


var s3Headers = {
'Content-Type': req.files.image.mimetype,

'Content-Length': req.files.image.size,
'x-amz-acl': 'public-read'
};

console.log(s3,s3Headers,req.files.image);

s3.putFile("./"+req.files.image.path, req.files.image.name, s3Headers, function(err, s3response){
gadgets.findById(req.params.id,function(_err,_gadget){
_gadget.image = s3response.req.url;
gadgets.save(_gadget,function(_err,_result){
res.render('gadgets/edit', { title: _gadget.name , gadget : _gadget});
})
})
});

});


Expand Down
2 changes: 1 addition & 1 deletion views/gadgets/detail.jade
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ block content
h1= gadget.model
.image
if(gadget.image)
img(src="../"+gadget.image,style="width: 300px;")
img(src=gadget.image,style="width: 300px;")
else
img(src='/img/dummy_device.gif',style="width: 300px;")
.detailcontent
Expand Down
2 changes: 1 addition & 1 deletion views/gadgets/edit.jade
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ block content
input(type="submit")

if(gadget.image)
img(src="/"+gadget.image,style="width: 100px;")
img(src=gadget.image,style="width: 100px;")
else
img(src='/img/device.jpg',style="width: 100px;")
br
Expand Down
2 changes: 1 addition & 1 deletion views/gadgets/list.jade
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ block content
a(onclick="openBookingLayer('/bookings/"+gadget._id+"/new')") Book
.cont
if(gadget.image)
img(src="/"+gadget.image)
img(src=gadget.image)
else
img(src='/img/dummy_device.gif')
if(gadget.bookings&&gadget.bookings.current&&gadget.bookings.current.status == "handout")
Expand Down
2 changes: 1 addition & 1 deletion views/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ block content
- each gadget in gadgets
li.element
if(gadget.image)
img(src="/"+gadget.image)
img(src=gadget.image)
else
img(src='/img/dummy_device.gif')
div.click
Expand Down

0 comments on commit 8262cfd

Please sign in to comment.