Skip to content

Commit

Permalink
Better handle file vs urls
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienheureux committed Jan 21, 2022
1 parent a8c2ab4 commit 240514a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions wagtail_wordpress_import/block_builder_defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,17 @@ def document_exists(name):
def get_absolute_src(src, domain_prefix=None):
src = src.lstrip("/")
url = urlparse(src)
known_file_extensions = ["jpg", "jpeg", "gif", "png", "webp"]

if not url.scheme and domain_prefix:
first_url_part = url.path.split("/")[0]
if (
"." in first_url_part
and first_url_part.split(".")[1].lower() not in known_file_extensions
):
return "{}://{}".format(urlparse(domain_prefix).scheme, url.path)
return "{}/{}".format(domain_prefix, src)

if not url.netloc and domain_prefix:
domain_prefix_scheme = urlparse(domain_prefix).scheme
return "{}://{}".format(domain_prefix_scheme, url.path)
elif not url.scheme and domain_prefix:
return domain_prefix + "/" + src
return src


Expand Down
2 changes: 1 addition & 1 deletion wagtail_wordpress_import/test/tests/test_block_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def test_get_absolute_src_without_base_url(self):
def test_get_abolute_src_slashes_at_start(self):
self.assertEqual(
get_absolute_src("//folder/fakeimage.jpg", "http://www.example.com"),
"http://example.com/folder/fakeimage.jpg",
"http://www.example.com/folder/fakeimage.jpg",
)

def test_get_abolute_src_slashes_at_start_external_domain(self):
Expand Down

0 comments on commit 240514a

Please sign in to comment.