Only attempt loading img URL with filename and appropriate extension

updates #1228
This commit is contained in:
JC Brand 2018-10-13 21:12:48 +02:00
parent e425f25753
commit 2426f9b7c8
2 changed files with 58 additions and 29 deletions

19
dist/converse.js vendored
View File

@ -82094,13 +82094,24 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
*/ */
const __ = _converse.__; const __ = _converse.__;
const list = obj.textContent.match(URL_REGEX) || []; const list = obj.textContent.match(URL_REGEX) || [];
return Promise.all(_.map(list, url => new Promise((resolve, reject) => isImage(url).then(function (img) { return Promise.all(_.map(list, url => new Promise((resolve, reject) => {
const uri = new URI(url),
filename = uri.filename(),
lower_filename = filename.toLowerCase();
if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
return resolve();
}
if (lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') || lower_filename.endsWith('png') || lower_filename.endsWith('gif') || lower_filename.endsWith('svg')) {
return isImage(url).then(img => {
const i = new Image(); const i = new Image();
i.src = img.src; i.src = img.src;
i.addEventListener('load', resolve); // We also resolve for non-images, otherwise the i.addEventListener('load', resolve); // We also resolve for non-images, otherwise the
// Promise.all resolves prematurely. // Promise.all resolves prematurely.
i.addEventListener('error', resolve); i.addEventListener('error', resolve);
const __ = _converse.__;
_.each(sizzle(`a[href="${url}"]`, obj), a => { _.each(sizzle(`a[href="${url}"]`, obj), a => {
a.outerHTML = tpl_image({ a.outerHTML = tpl_image({
@ -82108,7 +82119,11 @@ var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_
'label_download': __('Download') 'label_download': __('Download')
}); });
}); });
}).catch(resolve)))); }).catch(resolve);
} else {
return resolve();
}
})));
}; };
u.renderFileURL = function (_converse, url) { u.renderFileURL = function (_converse, url) {

View File

@ -288,8 +288,18 @@
const list = obj.textContent.match(URL_REGEX) || []; const list = obj.textContent.match(URL_REGEX) || [];
return Promise.all( return Promise.all(
_.map(list, (url) => _.map(list, (url) =>
new Promise((resolve, reject) => new Promise((resolve, reject) => {
isImage(url).then(function (img) { const uri = new URI(url),
filename = uri.filename(),
lower_filename = filename.toLowerCase();
if (!_.includes(["https", "http"], uri.protocol().toLowerCase())) {
return resolve();
}
if (lower_filename.endsWith('jpg') || lower_filename.endsWith('jpeg') ||
lower_filename.endsWith('png') || lower_filename.endsWith('gif') ||
lower_filename.endsWith('svg')) {
return isImage(url).then(img => {
const i = new Image(); const i = new Image();
i.src = img.src; i.src = img.src;
i.addEventListener('load', resolve); i.addEventListener('load', resolve);
@ -297,6 +307,7 @@
// Promise.all resolves prematurely. // Promise.all resolves prematurely.
i.addEventListener('error', resolve); i.addEventListener('error', resolve);
const { __ } = _converse;
_.each(sizzle(`a[href="${url}"]`, obj), (a) => { _.each(sizzle(`a[href="${url}"]`, obj), (a) => {
a.outerHTML= tpl_image({ a.outerHTML= tpl_image({
'url': url, 'url': url,
@ -304,7 +315,10 @@
}) })
}); });
}).catch(resolve) }).catch(resolve)
) } else {
return resolve();
}
})
) )
) )
}; };