Fetch remote images

This commit is contained in:
Mattias Erming 2014-08-16 14:19:15 -07:00
parent 0fcdbeadec
commit ac156544f0
4 changed files with 42 additions and 11 deletions

View File

@ -1,5 +1,4 @@
#!/usr/bin/env node
process.chdir(__dirname);
var config = require("./config.json");

View File

@ -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",

View File

@ -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);
});
});
});
}

View File

@ -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);