Limit preview for large image files (fix #500)

This commit is contained in:
Olivier Lambert 2015-10-01 00:15:53 +02:00
parent 9572bbd65d
commit e5858744a2
2 changed files with 24 additions and 2 deletions

View File

@ -64,6 +64,17 @@ module.exports = {
// //
prefetch: false, prefetch: false,
//
// Prefetch URLs Image Preview size limit
//
// If prefetch is enabled, Shout will only display content under the maximum size.
// Default value is 512 (in kB)
//
// @type int
// @default 512
//
prefetchMaxImageSize: 512,
// //
// Display network // Display network
// //

View File

@ -52,6 +52,7 @@ module.exports = function(irc, network) {
}; };
function parse(msg, url, res, client) { function parse(msg, url, res, client) {
var config = Helper.getConfig();
var toggle = msg.toggle = { var toggle = msg.toggle = {
id: msg.id, id: msg.id,
type: "", type: "",
@ -61,6 +62,9 @@ function parse(msg, url, res, client) {
link: url link: url
}; };
if (!config.prefetchMaxImageSize) {
config.prefetchMaxImageSize = 512;
}
switch (res.type) { switch (res.type) {
case "text/html": case "text/html":
var $ = cheerio.load(res.text); var $ = cheerio.load(res.text);
@ -80,7 +84,12 @@ function parse(msg, url, res, client) {
case "image/gif": case "image/gif":
case "image/jpg": case "image/jpg":
case "image/jpeg": case "image/jpeg":
toggle.type = "image"; if (res.size < (config.prefetchMaxImageSize * 1024)) {
toggle.type = "image";
}
else {
return;
}
break; break;
default: default:
@ -116,6 +125,7 @@ function fetch(url, cb) {
if (err) return; if (err) return;
var body; var body;
var type; var type;
var size = req.response.headers["content-length"];
try { try {
body = JSON.parse(data); body = JSON.parse(data);
} catch (e) { } catch (e) {
@ -129,7 +139,8 @@ function fetch(url, cb) {
data = { data = {
text: data, text: data,
body: body, body: body,
type: type type: type,
size: size
}; };
cb(data); cb(data);
})); }));