Assign preview.thumb only after it is processed

This commit is contained in:
Pavel Djundik 2019-12-13 11:43:13 +02:00
parent 51360711c9
commit bbf92f1aa0
2 changed files with 18 additions and 18 deletions

View File

@ -112,7 +112,7 @@ Chan.prototype.dereferencePreviews = function(messages) {
message.previews.forEach((preview) => { message.previews.forEach((preview) => {
if (preview.thumb) { if (preview.thumb) {
storage.dereference(preview.thumb); storage.dereference(preview.thumb);
preview.thumb = null; preview.thumb = "";
} }
}); });
} }

View File

@ -61,7 +61,7 @@ module.exports = function(client, chan, msg) {
preview.type = "error"; preview.type = "error";
preview.error = "message"; preview.error = "message";
preview.message = err.message; preview.message = err.message;
handlePreview(client, chan, msg, preview, null); emitPreview(client, chan, msg, preview);
}); });
return cleanLinks; return cleanLinks;
@ -86,35 +86,32 @@ function parseHtml(preview, res, client) {
$('meta[property="og:description"]').attr("content") || $('meta[property="og:description"]').attr("content") ||
$('meta[name="description"]').attr("content") || $('meta[name="description"]').attr("content") ||
""; "";
preview.thumb = let thumb =
$('meta[property="og:image"]').attr("content") || $('meta[property="og:image"]').attr("content") ||
$('meta[name="twitter:image:src"]').attr("content") || $('meta[name="twitter:image:src"]').attr("content") ||
$('link[rel="image_src"]').attr("href") || $('link[rel="image_src"]').attr("href") ||
""; "";
// Make sure thumbnail is a valid and absolute url // Make sure thumbnail is a valid and absolute url
if (preview.thumb.length) { if (thumb.length) {
preview.thumb = normalizeURL(preview.thumb, preview.link) || ""; thumb = normalizeURL(thumb, preview.link) || "";
} }
// Verify that thumbnail pic exists and is under allowed size // Verify that thumbnail pic exists and is under allowed size
if (preview.thumb.length) { if (thumb.length) {
fetch(preview.thumb, {language: client.config.browser.language}) fetch(thumb, {language: client.config.browser.language})
.then((resThumb) => { .then((resThumb) => {
if ( if (
resThumb === null || resThumb !== null &&
!imageTypeRegex.test(resThumb.type) || imageTypeRegex.test(resThumb.type) &&
resThumb.size > Helper.config.prefetchMaxImageSize * 1024 resThumb.size <= Helper.config.prefetchMaxImageSize * 1024
) { ) {
preview.thumb = ""; preview.thumbActualUrl = thumb;
} }
resolve(resThumb); resolve(resThumb);
}) })
.catch(() => { .catch(() => resolve(null));
preview.thumb = "";
resolve(null);
});
} else { } else {
resolve(res); resolve(res);
} }
@ -201,7 +198,7 @@ function parse(msg, chan, preview, res, client) {
preview.maxSize = Helper.config.prefetchMaxImageSize * 1024; preview.maxSize = Helper.config.prefetchMaxImageSize * 1024;
} else { } else {
preview.type = "image"; preview.type = "image";
preview.thumb = preview.link; preview.thumbActualUrl = preview.link;
} }
break; break;
@ -250,7 +247,11 @@ function parse(msg, chan, preview, res, client) {
} }
function handlePreview(client, chan, msg, preview, res) { function handlePreview(client, chan, msg, preview, res) {
if (!preview.thumb.length || !Helper.config.prefetchStorage) { const thumb = preview.thumbActualUrl || "";
delete preview.thumbActualUrl;
if (!thumb.length || !Helper.config.prefetchStorage) {
preview.thumb = thumb;
return emitPreview(client, chan, msg, preview); return emitPreview(client, chan, msg, preview);
} }
@ -265,7 +266,6 @@ function handlePreview(client, chan, msg, preview, res) {
return removePreview(msg, preview); return removePreview(msg, preview);
} }
preview.thumb = "";
return emitPreview(client, chan, msg, preview); return emitPreview(client, chan, msg, preview);
} }