Created
October 25, 2011 12:16
-
-
Save tj/1312520 to your computer and use it in GitHub Desktop.
Declarative JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ [ 'rule', 'parse upload' ], | |
[ 'indent' ], | |
[ 'rule', 'with all images' ], | |
[ 'indent' ], | |
[ 'rule', 'scale image to 800x600' ], | |
[ 'rule', 'save image to disk' ], | |
[ 'rule', 'save image to gridfs' ], | |
[ 'outdent' ], | |
[ 'outdent' ], | |
[ 'eos' ] ] | |
[ 'root', | |
[ [ 'rule', 'parse upload' ], | |
[ 'block', | |
[ [ 'rule', 'with all images' ], | |
[ 'block', | |
[ [ 'rule', 'scale image to 800x600' ], | |
[ 'rule', 'save image to disk' ], | |
[ 'rule', 'save image to gridfs' ] ] ] ] ] ] ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
POST /upload | |
parse upload | |
with all images | |
scale image to 800x600 | |
save image to disk | |
save image to gridfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.post('/upload', function(req, res){ | |
// parse upload | |
var form = new formidable.IncomingForm; | |
form.keepExtensions = true; | |
form.parse(req, function(err, fields, files) { | |
if (err) return res.send(err); | |
// with all images | |
var vals = files["images"]; | |
for (var i = 0, len = vals.length; i < len; ++i){ | |
var val = vals[i]; | |
var image = val.path; | |
// scale image to 800x600 | |
gm(image) | |
.scale(800, 600) | |
// save image to disk | |
.write(image, "/tmp/whatever", function(err){ | |
// error handling here | |
}); | |
// save image to gridfs | |
grid.putFile(image, "whatever", function(err){ | |
// error handling | |
}) | |
} | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
compiler.block(/^with all (.*)/, function(body, name){ | |
return 'var vals = files["' + name + '"];\n' | |
+ 'for (var i = 0, len = vals.length; i < len; ++i){\n' | |
+ ' var val = vals[i];\n' | |
+ ' var image = val.path;\n' | |
+ body | |
+ '}'; | |
}); | |
compiler.block(/^(GET|POST|PUT|DELETE) (.*)/, function(body, method, path){ | |
return 'app.' + method.toLowerCase() + '("' + path + '", function(req, res, next){\n' | |
+ body | |
+ '})'; | |
}); | |
compiler.rule(/^scale (.*) to (\d+)x(\d+)/, function(path, w, h){ | |
return 'gm(' + path + ')\n' | |
+ ' .scale(' + w + ', ' + h + ')\n'; | |
}); | |
compiler.rule(/^save (.*) to disk/, function(path){ | |
return '.write(' + path + ', "/tmp/whatever", function(err){\n' | |
+ ' // error handling here'; | |
+ '\n})'; | |
}); | |
compiler.rule(/^save (.*) to gridfs/, function(path){ | |
return 'grid.putFile(' + path + ', "whatever", function(err){\n' | |
+ ' // error handling' | |
+ '\n})'; | |
}); | |
compiler.block(/^parse upload/, function(body){ | |
return 'var form = new formidable.IncomingForm;\n\ | |
form.keepExtensions = true;\n\ | |
form.parse(req, function(err, fields, files) {\n\ | |
if (err) return res.send(err);\n' | |
+ body | |
+ '\n});'; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment