Skip to content

Allow image src without scheme and with external domain - #144

Open
fabienheureux wants to merge 6 commits into
wagtail:mainfrom
fabienheureux:feature/image-external-domain
Open

Allow image src without scheme and with external domain#144
fabienheureux wants to merge 6 commits into
wagtail:mainfrom
fabienheureux:feature/image-external-domain

Conversation

@fabienheureux

@fabienheureux fabienheureux commented Jan 21, 2022

Copy link
Copy Markdown
Member

Description

If images have a src attribute like //example.com/image.jpg, the imported url does not care about the domain and prepend the wagtail domain, hence leading to a 404 when downloading the image


  • Testing
    • CI passes
    • If necessary, tests are added for new or fixed behaviour
    • These changes do not reduce test coverage
  • Documentation.
    • Documentation changes are not necessary because they do not change how the importer usage

@fabienheureux fabienheureux changed the title Allow image if not scheme and external domain [WIP] Allow image if not scheme and external domain Jan 21, 2022
@fabienheureux fabienheureux changed the title [WIP] Allow image if not scheme and external domain Allow image if not scheme and external domain Jan 21, 2022
@fabienheureux

fabienheureux commented Jan 21, 2022

Copy link
Copy Markdown
Member Author

Sorry for the noise, I pushed without running tests locally, again 🙄, they now pass.

@nickmoreton do you think you could take a look ? I am not very proud of this approach, especially the hardcoded file extensions, but I did not see how could we better handle these various cases.

@fabienheureux fabienheureux changed the title Allow image if not scheme and external domain Allow image src without scheme and with external domain Jan 21, 2022
@fabienheureux
fabienheureux force-pushed the feature/image-external-domain branch from 915fada to 240514a Compare January 21, 2022 11:25
@nickmoreton nickmoreton self-assigned this Jan 24, 2022
@nickmoreton
nickmoreton requested review from nickmoreton and nimasmi and removed request for nickmoreton January 24, 2022 16:14
@nickmoreton

Copy link
Copy Markdown
Collaborator

@fabienheureux Thanks we really appreciate the pull request.

@nimasmi I tested it out and is working OK. Would you take a look please and confirm happy to merge. Thanks

@nimasmi nimasmi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fabienheureux. I'm always in favour of urlparse over string manipulation. It looks like we should remove some further custom string parsing.

first_url_part = url.path.split("/")[0]
if (
"." in first_url_part
and first_url_part.split(".")[1].lower() not in known_file_extensions

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue: In the following case, the unrecognised local file extension is interpreted as a domain:

>>> get_absolute_src("image.bmp", "https://www.example.com")
'https://image.bmp'

urlparse results have a .hostname attribute that you can use in this situation. You will have to remove the .lstrip("/") line though.

>>> from urllib.parse import urlparse
>>> urlparse("image.bmp").hostname
>>> urlparse("example.com/image.bmp").hostname
>>> urlparse("//example.com/image.bmp").hostname
'example.com'

get_absolute_src("//example.com/fakeimage.jpg", "http://www.example.com"),
"http://example.com/fakeimage.jpg",
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Add a test case for an unrecognised extension on the local domain: get_absolute_src("/fakeimage.badextension", "http://www.example.com")

@fabienheureux

Copy link
Copy Markdown
Member Author

@nickmoreton I am cleaning up old PRs.

Can I close this one ?

I hope I could have take @nimasmi comment into account but never found the time and I won't use this plugin in a near future.

@nickmoreton nickmoreton added the bug Something isn't working label May 9, 2025
@nickmoreton

Copy link
Copy Markdown
Collaborator

@nickmoreton I am cleaning up old PRs.

Can I close this one ?

I hope I could have take @nimasmi comment into account but never found the time and I won't use this plugin in a near future.

I'm hoping to get around to considering this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants