diff --git a/index.js b/index.js index f681cca0..87f4ec37 100755 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ #!/usr/bin/env node - process.chdir(__dirname); var config = require("./config.json"); diff --git a/package.json b/package.json index efeda453..be173ee7 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,12 @@ "commander": "^2.3.0", "connect": "~2.19.6", "lodash": "~2.4.1", + "mkdirp": "^0.5.0", "moment": "~2.7.0", "read": "^1.0.5", "slate-irc": "~0.6.0", - "socket.io": "~1.0.6" + "socket.io": "~1.0.6", + "superagent": "^0.18.2" }, "devDependencies": { "grunt": "~0.4.5", diff --git a/src/plugins/irc-events/thumb.js b/src/plugins/irc-events/thumb.js index 4b2419e4..97a26614 100644 --- a/src/plugins/irc-events/thumb.js +++ b/src/plugins/irc-events/thumb.js @@ -1,5 +1,9 @@ var _ = require("lodash"); var Msg = require("../../models/msg"); +var config = require("../../../config.json"); +var fs = require("fs"); +var mkdirp = require("mkdirp"); +var http = require("http"); module.exports = function(irc, network) { var client = this; @@ -20,15 +24,40 @@ module.exports = function(irc, network) { if (typeof chan === "undefined") { return; } - var msg = new Msg({ - type: Msg.Type.THUMB, - from: data.from, - text: "http://placehold.it/320x320" // image - }); - chan.messages.push(msg); - client.emit("msg", { - chan: chan.id, - msg: msg + fetchImage(image, function(name) { + var msg = new Msg({ + type: Msg.Type.THUMB, + from: data.from, + text: "thumbs/" + name + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); }); }); }; + +function fetchImage(url, callback) { + var path = process.env.HOME + "/.shout/cache/thumbs"; + var name = new Date().getTime().toString() + mkdirp(path, function(e) { + if (e) { + console.log(e); + } + var stream = fs.createWriteStream(path + "/" + name); + stream.on("error", function(e) { + // .. + }); + http.get(url, function(res) { + res.on("data", function(chunk) { + stream.write(chunk); + }); + res.on("end", function() { + stream.end(); + callback(name); + }); + }); + }); +} diff --git a/src/server.js b/src/server.js index 61a49b2b..f17ab85a 100644 --- a/src/server.js +++ b/src/server.js @@ -33,6 +33,7 @@ module.exports = function(port, public) { var app = http() .use(index) .use(http.static("client")) + .use(http.static(process.env.HOME + "/.shout/cache")) .listen(config.port); sockets = io(app);