Created
July 24, 2015 04:19
-
-
Save karthik20522/78953d2838f81a3a6cf2 to your computer and use it in GitHub Desktop.
gm_node.js
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'); | |
var path = require('path'); | |
var log = require('./libs/log')(module); | |
var size = require('request-image-size'); | |
var fDld = require('fast-download'); | |
var fs = require('fs'); | |
var app = express(); | |
var gm = require('gm'); | |
var uuid = require('node-uuid'); | |
app.get('/api', function (req, res) { | |
log.info("Received request", {mp: req.query.mp, url: req.query.url}); | |
var fileId = uuid.v1(); | |
new fDld(req.query.url, {chunksAtOnce: 10}, function(error, dl) { | |
if (error){throw error;} | |
dl.on('start', function(dl){ log.info('Download started: ' + req.query.url); }) | |
dl.on('error', function(error){ throw error; }); | |
dl.on('end', function() { | |
log.info('Download completed'); | |
gm(fileId) | |
.size({bufferStream: true}, function(err, size) { | |
if (err) throw err; | |
log.info("Resize start" + fileId); | |
this.resize(200,200);//size.width / 2, size.height / 2); | |
log.info("Resize done" + fileId); | |
this.write(fileId + '.jpg', function (err) { | |
if (!err) res.send(size); | |
else throw err; | |
}); | |
}); | |
}); | |
dl.pipe(fs.createWriteStream(fileId)); | |
}); | |
}); | |
app.listen(1337, function(){ | |
console.log('Express server listening on port 1337'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment