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 #!/usr/bin/env node
process.chdir(__dirname); process.chdir(__dirname);
var config = require("./config.json"); var config = require("./config.json");

View File

@ -26,10 +26,12 @@
"commander": "^2.3.0", "commander": "^2.3.0",
"connect": "~2.19.6", "connect": "~2.19.6",
"lodash": "~2.4.1", "lodash": "~2.4.1",
"mkdirp": "^0.5.0",
"moment": "~2.7.0", "moment": "~2.7.0",
"read": "^1.0.5", "read": "^1.0.5",
"slate-irc": "~0.6.0", "slate-irc": "~0.6.0",
"socket.io": "~1.0.6" "socket.io": "~1.0.6",
"superagent": "^0.18.2"
}, },
"devDependencies": { "devDependencies": {
"grunt": "~0.4.5", "grunt": "~0.4.5",

View File

@ -1,5 +1,9 @@
var _ = require("lodash"); var _ = require("lodash");
var Msg = require("../../models/msg"); 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) { module.exports = function(irc, network) {
var client = this; var client = this;
@ -20,15 +24,40 @@ module.exports = function(irc, network) {
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
var msg = new Msg({ fetchImage(image, function(name) {
type: Msg.Type.THUMB, var msg = new Msg({
from: data.from, type: Msg.Type.THUMB,
text: "http://placehold.it/320x320" // image from: data.from,
}); text: "thumbs/" + name
chan.messages.push(msg); });
client.emit("msg", { chan.messages.push(msg);
chan: chan.id, client.emit("msg", {
msg: 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() var app = http()
.use(index) .use(index)
.use(http.static("client")) .use(http.static("client"))
.use(http.static(process.env.HOME + "/.shout/cache"))
.listen(config.port); .listen(config.port);
sockets = io(app); sockets = io(app);