From 59ab5d87778988eec060d478229ab7a3d9b358f6 Mon Sep 17 00:00:00 2001 From: Nathan Spicer-Davis Date: Mon, 12 Apr 2021 23:51:40 -0400 Subject: [PATCH] Update extension regex to match URI fragments (#264) --- bdfr/resource.py | 2 +- bdfr/tests/test_resource.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bdfr/resource.py b/bdfr/resource.py index 12c9cc4..be6aaaf 100644 --- a/bdfr/resource.py +++ b/bdfr/resource.py @@ -64,7 +64,7 @@ class Resource: self.hash = hashlib.md5(self.content) def _determine_extension(self) -> Optional[str]: - extension_pattern = re.compile(r'.*(\..{3,5})(?:\?.*)?$') + extension_pattern = re.compile(r'.*(\..{3,5})(?:\?.*)?(?:#.*)?$') match = re.search(extension_pattern, self.url) if match: return match.group(1) diff --git a/bdfr/tests/test_resource.py b/bdfr/tests/test_resource.py index 0a8e145..de6030b 100644 --- a/bdfr/tests/test_resource.py +++ b/bdfr/tests/test_resource.py @@ -15,6 +15,8 @@ from bdfr.resource import Resource ('https://www.resource.com/test/example.jpg', '.jpg'), ('hard.png.mp4', '.mp4'), ('https://preview.redd.it/7zkmr1wqqih61.png?width=237&format=png&auto=webp&s=19de214e634cbcad99', '.png'), + ('test.jpg#test','.jpg'), + ('test.jpg?width=247#test','.jpg'), )) def test_resource_get_extension(test_url: str, expected: str): test_resource = Resource(MagicMock(), test_url)