Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow image src without scheme and with external domain #144

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

fabienheureux
Copy link
Contributor

@fabienheureux fabienheureux commented Jan 21, 2022

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
Copy link
Contributor Author

fabienheureux commented Jan 21, 2022

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
@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
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

Copy link
Collaborator

@nimasmi nimasmi left a comment

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
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
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
Contributor 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants