Handle images with unknown size in prefetch

This commit is contained in:
Niko Bews 2017-06-21 08:51:14 +03:00
parent ead4a7f791
commit 57fd0562a2

View File

@ -93,11 +93,12 @@ function fetch(url, cb) {
return; return;
} }
var length = 0; var length = 0;
var limit = 1024 * 10; var limit = Helper.config.prefetchMaxImageSize * 1024;
req req
.on("response", function(res) { .on("response", function(res) {
if (!(/(text\/html|application\/json)/.test(res.headers["content-type"]))) { if (!(/(image\/.+)/.test(res.headers["content-type"]))) {
res.req.abort(); // if not image, limit download to 10kb, since we need only meta tags
limit = 1024 * 10;
} }
}) })
.on("error", function() {}) .on("error", function() {})
@ -113,14 +114,13 @@ function fetch(url, cb) {
return; return;
} }
var body; let type;
var type; let size = parseInt(req.response.headers["content-length"], 10) || length;
var size = req.response.headers["content-length"];
try { if (size < length) {
body = JSON.parse(data); size = length;
} catch (e) {
body = {};
} }
try { try {
type = req.response.headers["content-type"].split(/ *; */).shift(); type = req.response.headers["content-type"].split(/ *; */).shift();
} catch (e) { } catch (e) {
@ -128,7 +128,6 @@ function fetch(url, cb) {
} }
data = { data = {
text: data, text: data,
body: body,
type: type, type: type,
size: size size: size
}; };