[FIX] web_editor: make loadImageInfo robust to absolute URLs

This commit makes the rpc call in the `loadImageInfo` method robust to
the case where an image src would be an absolute URL, by only
considering the relative part of the URL.

The images src are generally always relative but commit [1] wrongly put
absolute URLs to images in grid mode with the `_reloadLazyImages`
method. While this behavior was fixed by commit [2], the images that
were saved before this fix was available still have an absolute URL.
Hence the need to make `loadImageInfo` robust.

[1]: https://github.com/odoo/odoo/commit/cc406afcea7bf5846233a9f97a4a8ac5f618f3ec
[2]: https://github.com/odoo/odoo/commit/3528f2c3f9d3b30266f3421672986d202c724a76

task-3514519

closes odoo/odoo#136951

X-original-commit: af5ddabb0285daa9e57bc01cf21628f2f94bc18f
Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
This commit is contained in:
Soukéina Bojabza
2023-09-29 09:21:54 +00:00
parent 569fdb3028
commit bd3e0030d0
@@ -447,11 +447,24 @@ export async function loadImageInfo(img, rpc, attachmentSrc = '') {
return;
}
const {original} = await rpc('/web_editor/get_image_info', {src: src.split(/[?#]/)[0]});
// Check that url is local.
const isLocal = original && new URL(original.image_src, window.location.origin).origin === window.location.origin
&& !/\/web\/image\/\d+-redirect\//.test(original.image_src);
if (isLocal && original.image_src) {
// Only consider the "relative" part of the URL. Needed because some
// relative URLs were wrongly converted to absolute URLs at some point and
// user domains could have been changed meanwhile.
let relativeSrc;
try {
const srcUrl = new URL(src);
relativeSrc = srcUrl.pathname;
} catch {
relativeSrc = src;
}
const {original} = await rpc('/web_editor/get_image_info', {src: relativeSrc.split(/[?#]/)[0]});
// If src was an absolute "external" URL, we consider unlikely that its
// relative part matches something from the DB and even if it does, nothing
// bad happens, besides using this random image as the original when using
// the options, instead of having no option.
// The "redirect" check is for when it is a redirect image attachment due to
// an external URL upload.
if (original && original.image_src && !/\/web\/image\/\d+-redirect\//.test(original.image_src)) {
img.dataset.originalId = original.id;
img.dataset.originalSrc = original.image_src;
img.dataset.mimetype = original.mimetype;