Skip to content

Commit cbfd469

Browse files
committed
close #32 by converting base64 images to binary in node
1 parent 8131dc9 commit cbfd469

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Diff for: app.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ if ('development' == app.get('env')) {
3232

3333
app.get('/', routes.index);
3434
app.get('/i/:id', routes.show);
35+
app.get('/raw/:id', routes.raw);
3536
app.post('/create', routes.create);
3637
app.get('/delete/:id', routes.delete);
3738
app.get('/static/sandbox/', function(req,res) { res.redirect('/sandbox/') });

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"express": "3.4.7",
1010
"jade": "*",
1111
"stylus": "*",
12+
"atob": "*",
1213
"mongodb": ">= 0.9.6-7",
1314
"mongoose" : "3.8.1"
1415
}

Diff for: routes/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ exports.show = function(req, res){
2323
})
2424
};
2525

26+
// serve up the image itself, converting it from base64 to binary
27+
exports.raw = function(req, res){
28+
Image.findOne({ _id: req.params.id }, 'src title desc author orig_src updated_at log', function (err, image) {
29+
if (err) return handleError(err);
30+
res.writeHead(200, { "Content-type": "image/png" });
31+
var atob = require('atob')
32+
res.end(atob(image.src.split(',')[1]), "binary");
33+
})
34+
};
35+
2636
exports.delete = function(req, res){
2737
if (req.params.pwd == "easytohack") { // very temporary solution
2838
Image.remove({ _id: req.params.id }, function (err, image) {
@@ -46,3 +56,5 @@ exports.create = function ( req, res ){
4656
res.redirect( '/' );
4757
});
4858
};
59+
60+

Diff for: views/show.jade

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
extends layout
22
block content
33

4+
- var log = []
5+
- if (image.log) log = JSON.parse(image.log)
6+
47
div.navbar.clearfix
58
div.navbar-inner
69
ul.nav
710
li
8-
a(href="/sandbox/?src="+image.src)
11+
a(href="/sandbox/?src=/raw/"+image._id+"&infragrammar="+log[log.length-1])
912
i.icon.icon-pencil
1013
| Open in Infragram Sandbox »
1114
li: a(onClick="$('#final').toggle();$('#original').toggle();") See original
@@ -26,8 +29,6 @@ block content
2629

2730
form.form
2831
label Current command
29-
- var log = []
30-
- if (image.log) log = JSON.parse(image.log)
3132
input(type="text" value=log[log.length-1])
3233
label Full command log
3334
textarea#log.span6(rows=10)= log.join('\r')

0 commit comments

Comments
 (0)