Skip to content

Commit

Permalink
Updated for new tutorial on using Amazon S3 for images
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevalstar committed Oct 16, 2012
1 parent 1b30f2c commit 387c2cc
Show file tree
Hide file tree
Showing 20 changed files with 542 additions and 93 deletions.
2 changes: 1 addition & 1 deletion app.js
Expand Up @@ -13,7 +13,7 @@ var app = express();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.bodyParser({ keepExtensions: true, uploadDir: __dirname + "/tmp" }));
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.compress());
Expand Down
6 changes: 3 additions & 3 deletions htdocs/css/bootstrap/bootstrap.less
Expand Up @@ -39,8 +39,8 @@
@import "alerts.less"; // Note: alerts share common CSS with buttons and thus have styles in buttons.less

// Components: Nav
//@import "navs.less";
//@import "navbar.less";
@import "navs.less";
@import "navbar.less";
//@import "breadcrumbs.less";
@import "pagination.less";
@import "pager.less";
Expand All @@ -51,7 +51,7 @@
//@import "popovers.less";

// Components: Misc
//@import "thumbnails.less";
@import "thumbnails.less";
@import "labels-badges.less";
//@import "progress-bars.less";
//@import "accordion.less";
Expand Down
15 changes: 15 additions & 0 deletions htdocs/css/style.less
Expand Up @@ -206,6 +206,10 @@ body{
}
}

#disqus_identity, #disqus_permalink {
display: none;
}

// Other
a.twitter{ background: url('../img/social/16px/twitter-2.png') no-repeat bottom left; padding-left: 19px; }
a.reddit{ background: url('../img/social/16px/reddit.png') no-repeat bottom left; padding-left: 19px; }
Expand All @@ -216,6 +220,17 @@ a.linkedin{ background: url('../img/social/16px/linkedin.png') no-repeat bottom
a.stackoverflow{ background: url('../img/social/16px/stackoverflow.png') no-repeat bottom left; padding-left: 19px; }
a.flickr{ background: url('../img/social/16px/flickr.png') no-repeat bottom left; padding-left: 19px; }

// Admin
.dropzone{
border: 2px dashed @colorGray;
color: @colorGray;
font-weight: bold;
padding: 0.5em 2em;
min-width: 20em;
text-align: center;
background: lighten(@colorGray, 20%);
}

/* Retina Displays */
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
background-image: url("../img/noisy_grid_@2X.png");
Expand Down
142 changes: 142 additions & 0 deletions htdocs/js/admin.js
@@ -0,0 +1,142 @@
/*
Welcome to my Javascript
Please vist mikevalstar.com for an explination
about how some of the below is done.
*/

jQuery.event.fixHooks.drop = { props: [ "dataTransfer" ] };

if(typeof MV == 'undefined') var MV = {};

MV.admin = {};

/*******************************
Image List builder for display
*******************************/
MV.admin.imageList = function(list, selectfunc){ this._ele = list; this.init(selectfunc); };
MV.admin.imageList.prototype = {
_ele: null,
_onselect: null,

init: function(selectfunc){
// display the image listing in the element
var imgListObj = this;
this._onselect = selectfunc || function(e){e.preventDefault();};

MV.admin.data.images.list(function(data){ imgListObj._fillContainer.call(imgListObj, data); });

// Define drop zone for uploading
if($('.dropimglead').length > 0){
$('.dropimglead').on( 'dragenter', function(e) {
e.preventDefault();
$(this).html('Drop Here');
}).on( 'dragleave', function(e) {
e.preventDefault();
$(this).html('Drag a file here to upload');
}).on( 'drop', function(e){
e.preventDefault();

$(this).html('Uploading file!!!');

// Send the image to the server
var self = this;
MV.admin.data.images.upload('lead', e.dataTransfer.files[0], function(results){
$(self).html('File Uploaded.').hide().fadeIn(2000);
var reset = function(){ $(self).html('Drag a file here to upload'); };
setTimeout(reset, 3000);
imgListObj.refresh();
});
});
}
},
refresh:function(){
var self = this;
MV.admin.data.images.list(function(data){ self._fillContainer.call(self, data); });
},
_fillContainer: function(data){
this._ele.html(''); // clear the gallary
if(data.images){
var list = $('<ul></ul>').addClass('thumbnails');
this._ele.append(list);
$(data.images).each(function(index, item){
list.append('<li class="span2"><a href="#" class="thumbnail" data-id="' + item.imageShort + '"><img class="retina" src="' + item.image + '" alt="' + item.imageShort + '" style="height: 150px; width: 150px;" /></a></li>');
});

this._ele.find('.thumbnail').click(this._onselect);

MV.imageReplace();
}
}
};

/*******************************
Data Getters and Setters
*******************************/
MV.admin.data = {
images: {
list: function(callback){
$.get('/Admin/AJAX/Images', callback);
},
upload: function(type, file, callback){
// using:
// http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
// http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/

if (!file || !file.type.match(/image.*/)) return; // TODO: present error if non-image

// send form data to image post url
var fd = new FormData();
fd.append("image", file);
fd.append("type", type);
$.ajax({
url: '/Admin/AJAX/Image',
data: fd,
processData: false,
contentType: false,
type: 'POST'
}).done(callback);
}
}
};


$(function(){
if($('#imageListFull').length == 1){
var bigList = new MV.admin.imageList($('#imageListFull'));
bigList.refresh();
}

$('.imageSelector').click(function(e){
e.preventDefault();
var self = this;
var selectList = new MV.admin.imageList($( $(this).attr('data-port') ), function(e){
e.preventDefault();
$($(self).attr('data-fill')).val($(this).attr('data-id')); // set the value of the textbox
$($(self).attr('data-port')).html(''); // hide the images
});
});

// convert textarea tabs to the tab character
$("textarea").keydown(function(e) {
if(e.keyCode === 9) { // tab was pressed
// prevent the focus lose
e.preventDefault();

// get caret position/selection
var start = this.selectionStart;
var end = this.selectionEnd;

var $this = $(this);
var value = $this.val();

// set textarea value to: text before caret + tab + text after caret
$this.val(value.substring(0, start)
+ "\t"
+ value.substring(end));

// put caret at right position again (add one for the tab)
this.selectionStart = this.selectionEnd = start + 1;
}
});
});
1 change: 1 addition & 0 deletions htdocs/js/jquery.tweet.js
@@ -1,5 +1,6 @@
// jquery.tweet.js - See http://tweet.seaofclouds.com/ or https://github.com/seaofclouds/tweet for more info
// Copyright (c) 2008-2011 Todd Matthews & Steve Purcell

(function($) {
$.fn.tweet = function(o){
var s = $.extend({
Expand Down
17 changes: 15 additions & 2 deletions htdocs/js/mikevalstar.js
Expand Up @@ -9,10 +9,20 @@ var disqus_shortname = 'mikevalstar'; // required: replace example with your for
var disqus_identifier = 'bp_0';
var disqus_url = 'http://mikevalstar.com/';

var MV = {};
if(typeof MV == 'undefined') var MV = {};

MV.content = {};

MV.imageReplace = function(){
if (window.devicePixelRatio >= 1.5) {
$('img.retina').each(function(index, item){
item = $(item);
if(!item.attr('src')) return; // no source on image
var path = item.attr('src');
var at_2x_path = path.replace(/\.\w+$/, function(match) { return "@2x" + match; });
item.attr('src', at_2x_path);
});
}
}

$(function(){
// Initialize twitter feed
Expand All @@ -36,4 +46,7 @@ $(function(){
});
}
});

// Retina image replacement
MV.imageReplace();
});
Binary file removed htdocs/usrimg/bp_104_hashbang.png
Binary file not shown.
61 changes: 61 additions & 0 deletions lib/AS3.js
@@ -0,0 +1,61 @@
var awssum = require('awssum');
var amazon = awssum.load('amazon/amazon');
var S3 = awssum.load('amazon/s3').S3;
var fs = require('fs');

/*********************
Abstraction class for amazon S3
*********************/

var AS3 = module.exports = {
_connect: function(){

var s3 = new S3({
'accessKeyId' : process.env.accessKeyId,
'secretAccessKey' : process.env.secretAccessKey,
'region' : amazon.US_EAST_1
});

return s3;
},

listImages:function(dir, callback){
s3 = this._connect();

var options = {
BucketName : 'mikevalstar',
MaxKeys : 200,
Prefix: 'img/' + dir
};

s3.ListObjects(options,function(err,data){
if(!err){
callback(err, data.Body.ListBucketResult);
}else{
callback(err);
}
});
},

uploadImage: function(source, dest, content_type, callback){
s3 = this._connect();

fs.stat(source, function(err, file_info) {
if (err) {
callback(err);
return;
}

var bodyStream = fs.createReadStream( source );
var params = {
BucketName : 'mikevalstar',
ObjectName : 'img/' + dest,
ContentType : content_type,
ContentLength : file_info.size,
Body : bodyStream
};

s3.PutObject(params, callback);
});
}
};

0 comments on commit 387c2cc

Please sign in to comment.