Skip to content

Commit

Permalink
updating to use templated layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Breed committed Nov 27, 2011
1 parent a282c46 commit c9af2c0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/app.js
Expand Up @@ -6,6 +6,14 @@ var
handlebars = require('handlebars'),
s3 = require('./s3');


function getTemplate( filename ) {
var file = path.resolve( './templates', filename + '.hbs' );
return fs.readFileSync( file, 'utf8' );
}

var uploadLayout = handlebars.compile( getTemplate( 'upload' ) );

var app = new strata.Builder();

var root = path.resolve( './public' );
Expand All @@ -18,19 +26,16 @@ app.get('/', function(env, callback){
callback( 200, {}, fs.readFileSync('./public/index.html', 'utf8') );
});

// POST /
// Uploads a file to the server.
app.post("/upload", function (env, callback) {
var req = new strata.Request(env);

req.params(function (err, params) {
if (err && strata.handleError(err, env, callback)) {
return;
return;
}

var photo = params.photo;

var error;
var photo = params.photo;

if (!photo) {
error = 'Param "photo" is required';
Expand All @@ -49,29 +54,26 @@ app.post("/upload", function (env, callback) {
if ( err )
callback( 403, {}, 'something terrible happened whilst reading the file');

// pass file on to our s3 bucket
var upload = s3.put( '/test/' + photo.name, {
'Content-Length': buf.length,
'Content-Type': photo.type
});

// render repsonse page on success
upload.on('response', function( res ){
if ( 200 !== res.statusCode )
return callback( 403, {}, 'something terrible has happened');

var content = "";

content += "The photo was uploaded successfully. Here are its properties:";
content += "<ul>";
var content = {};

["path", "name", "type", "size"].forEach(function (prop) {
content += "<li>" + prop + ": " + photo[prop] + "</li>";
content[prop] = photo[prop];
});

content += "<li>url: <a href='"+ upload.url +"'>"+ upload.url +"</a></li>";

content += "</ul>";
content.url = upload.url;

callback(200, {}, content);
callback(200, {}, uploadLayout( content ) );
});

upload.end( buf );
Expand Down
17 changes: 17 additions & 0 deletions templates/upload.hbs
@@ -0,0 +1,17 @@
<!doctype html public "⚓ ⚓ ⚓">
<meta charset=utf8>
<h2>Upload Successful</h2>
<dl>
<dt>url:
<dd><a href={{url}}>{{url}}</a>
<dt>name:
<dd>{{name}}
<dt>type:
<dd>{{type}}
<dt>size:
<dd>{{size}}
{{#if url}}
<dt>image:
<dd><a href={{url}}><img src={{url}} alt={{name}}></a>
{{/if}}
</dl>

0 comments on commit c9af2c0

Please sign in to comment.