Fetch remote images
This commit is contained in:
parent
0fcdbeadec
commit
ac156544f0
1
index.js
1
index.js
@ -1,5 +1,4 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
process.chdir(__dirname);
|
||||
|
||||
var config = require("./config.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",
|
||||
|
@ -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,10 +24,11 @@ module.exports = function(irc, network) {
|
||||
if (typeof chan === "undefined") {
|
||||
return;
|
||||
}
|
||||
fetchImage(image, function(name) {
|
||||
var msg = new Msg({
|
||||
type: Msg.Type.THUMB,
|
||||
from: data.from,
|
||||
text: "http://placehold.it/320x320" // image
|
||||
text: "thumbs/" + name
|
||||
});
|
||||
chan.messages.push(msg);
|
||||
client.emit("msg", {
|
||||
@ -31,4 +36,28 @@ module.exports = function(irc, network) {
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user