Skip to content

Commit a1bdd13

Browse files
authored
Merge pull request #2046 from kili-technology/feature/lab-4399-aau-when-i-export-from-the-ui-or-the-sdk-there-is-no-double
fix(LAB-4399): fix to ensure if there is already an extension to not add a second one
2 parents 4cce161 + 61cbe65 commit a1bdd13

2 files changed

Lines changed: 45 additions & 4 deletions

File tree

src/kili/use_cases/asset/media_downloader.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Helpers for the asset queries."""
2-
2+
import mimetypes
33
import warnings
44
from collections.abc import Callable
55
from concurrent.futures import ThreadPoolExecutor
@@ -209,8 +209,12 @@ def get_download_path(
209209
) -> Path:
210210
"""Build the path to download a file the file in local."""
211211
extension = get_file_extension_from_headers(url, http_client)
212-
filename = external_id
213-
if extension is not None and not filename.endswith(extension):
212+
filename = str(external_id)
213+
current_suffix = Path(filename).suffix.lower()
214+
has_known_extension = (
215+
current_suffix != "" and mimetypes.types_map.get(current_suffix) is not None
216+
)
217+
if extension is not None and not has_known_extension:
214218
filename = filename + extension
215219
local_dir_path = local_dir_path / filename
216220
return local_dir_path.resolve()

tests/unit/test_media_downloader.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import pytest
77

88
from kili.adapters.http_client import HttpClient
9-
from kili.use_cases.asset.media_downloader import MediaDownloader
9+
from kili.domain.asset import AssetExternalId
10+
from kili.use_cases.asset.media_downloader import MediaDownloader, get_download_path
1011
from kili.utils.tempfile import TemporaryDirectory
1112

1213

@@ -198,3 +199,39 @@ def test_download_media_jsoncontent_none(mocker, content, jsoncontent, should_ca
198199
http_client.get.assert_called()
199200
else:
200201
http_client.get.assert_not_called()
202+
203+
204+
def test_get_download_path_no_double_extension(mocker):
205+
mocker.patch(
206+
"kili.use_cases.asset.media_downloader.get_file_extension_from_headers",
207+
return_value=".jpg",
208+
)
209+
http_client = mocker.MagicMock(spec=HttpClient)
210+
path = get_download_path(
211+
"https://example.com/img", AssetExternalId("photo.jpeg"), Path("/tmp"), http_client
212+
)
213+
assert path.name == "photo.jpeg" # not "photo.jpeg.jpg"
214+
215+
216+
def test_get_download_path_with_valid_extension(mocker):
217+
mocker.patch(
218+
"kili.use_cases.asset.media_downloader.get_file_extension_from_headers",
219+
return_value=".jpg",
220+
)
221+
http_client = mocker.MagicMock(spec=HttpClient)
222+
path = get_download_path(
223+
"https://example.com/img", AssetExternalId("photo.aaa"), Path("/tmp"), http_client
224+
)
225+
assert path.name == "photo.aaa.jpg" # not "photo.aaa"
226+
227+
228+
def test_get_download_path_with_extension(mocker):
229+
mocker.patch(
230+
"kili.use_cases.asset.media_downloader.get_file_extension_from_headers",
231+
return_value=".jpg",
232+
)
233+
http_client = mocker.MagicMock(spec=HttpClient)
234+
path = get_download_path(
235+
"https://example.com/img", AssetExternalId("photo"), Path("/tmp"), http_client
236+
)
237+
assert path.name == "photo.jpg" # not "photo"

0 commit comments

Comments
 (0)