Created
May 19, 2012 13:27
-
-
Save lerouxb/2730854 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| var | |
| express = require('express'), | |
| gm = require('gm'), | |
| http = require('http'), | |
| request = require('request'), | |
| urlmod = require('url'), | |
| app = express.createServer(); | |
| app.use(express.logger('tiny')); | |
| app.use(express.errorHandler({ | |
| dumpExceptions: true, | |
| showStack: true | |
| })); | |
| function checkURL(options, callback) { | |
| // TODO: some kind of timeout would be nice | |
| var | |
| url = options.url, | |
| supportedTypes = options.supportedTypes, | |
| maxSize = options.maxSize; | |
| request.head(url, function(err, response, data) { | |
| if (err) { | |
| // Probably anything from a timeout to network error. | |
| console.error(err); | |
| callback({name: "ServerError"}); | |
| } else if (response.statusCode != 200) { | |
| // Assume not found for now. | |
| // What about other success statuses? | |
| callback({name: "NotFound"}); | |
| } else { | |
| var | |
| contentType = response.headers['content-type'], | |
| contentLength = response.headers['content-length']; | |
| if (supportedTypes.indexOf(contentType) == -1) { | |
| callback({name: "UnknownType"}); | |
| } else if (!contentLength || contentLength < maxSize) { | |
| // looks OK so far | |
| callback(null, { | |
| contentType: contentType, | |
| contentLength: contentLength | |
| }); | |
| } else { | |
| callback({name: "TooBig"}); | |
| } | |
| } | |
| }); | |
| }; | |
| function identifyImageURL(options, callback) { | |
| // TODO: | |
| // * some kind of timeout would be nice | |
| // * and maybe a whitelist of supported image formats | |
| // * perhaps we should also make sure that only websafe features like | |
| // colour profiles or modes are used | |
| var | |
| url = options.url, | |
| maxSize = options.maxSize; | |
| //maxSize = 0, // HACK | |
| total = 0; | |
| http.get(urlmod.parse(url), function(res) { | |
| res.on('data', function(chunk) { | |
| total += chunk.length; | |
| console.log(chunk.length, total); | |
| if (total > maxSize) { | |
| console.log('hanging up'); | |
| res.destroy(); | |
| } | |
| }); | |
| var image = gm(res); | |
| image.identify(function(err, data) { | |
| if (err) { | |
| if (total > maxSize) { | |
| callback({name: "TooBig"}); | |
| } else { | |
| callback({name: "ServerError"}); | |
| } | |
| } else { | |
| if (total > maxSize) { | |
| callback({name: "TooBig"}); | |
| } else { | |
| data.contentLength = total; | |
| callback(null, data); | |
| } | |
| } | |
| }); | |
| }); | |
| }; | |
| app.get('/check/image', function(req, res) { | |
| var | |
| url = req.query.url, | |
| maxSize = 5*1024*1024 // 5Mb | |
| checkURL({ | |
| url: url, | |
| supportedTypes: ['image/png', 'image/jpeg', 'image/gif'], | |
| maxSize: maxSize | |
| }, | |
| function(err, info) { | |
| //console.log("checkURL", err, data); | |
| if (err) { | |
| res.send({error: err.name}); | |
| } else { | |
| identifyImageURL({ | |
| url: url, | |
| maxSize: maxSize | |
| }, | |
| function(err, features) { | |
| //console.log("identifyImageURL", err, features); | |
| if (err) { | |
| res.send({error: err.name}); | |
| } else { | |
| data = { | |
| contentType: info.contentType, | |
| contentLength: features.contentLength, | |
| format: features.format, | |
| width: features.size.width, | |
| height: features.size.height | |
| }; | |
| res.send(data); | |
| } | |
| }); | |
| } | |
| }); | |
| }); | |
| app.listen(3000); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A version of https://gist.github.com/2730750 using node's http module. This one works.