Skip to content

Commit 2c88e56

Browse files
authored
fix: fix resolution in case of URL without path (#55)
Signed-off-by: Panos Vagenas <[email protected]>
1 parent 0cd8b94 commit 2c88e56

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

docling_core/utils/file.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def resolve_file_source(source: Union[Path, AnyHttpUrl, str]) -> Path:
4141
break
4242
# otherwise, use name from URL:
4343
if fname is None:
44-
fname = Path(http_url.path or "file").name
44+
fname = Path(http_url.path or "").name or "file"
4545
local_path = Path(tempfile.mkdtemp()) / fname
4646
with open(local_path, "wb") as f:
4747
for chunk in res.iter_content(chunk_size=1024): # using 1-KB chunks

test/test_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
import json
88

99
from pydantic import Field
10+
from requests import Response
1011

1112
from docling_core.utils.alias import AliasModel
13+
from docling_core.utils.file import resolve_file_source
1214

1315

1416
def test_alias_model():
@@ -47,3 +49,24 @@ class AliasModelGrandChild(AliasModelChild):
4749

4850
assert obj.model_dump_json() == json.dumps(data_alias, separators=(",", ":"))
4951
assert obj.model_dump_json() != json.dumps(data, separators=(",", ":"))
52+
53+
54+
def test_resolve_file_source_url_wout_path(monkeypatch):
55+
expected_str = "foo"
56+
expected_bytes = bytes(expected_str, "utf-8")
57+
58+
def get_dummy_response(*args, **kwargs):
59+
r = Response()
60+
r.status_code = 200
61+
r._content = expected_bytes
62+
return r
63+
64+
monkeypatch.setattr("requests.get", get_dummy_response)
65+
monkeypatch.setattr(
66+
"requests.models.Response.iter_content",
67+
lambda *args, **kwargs: [expected_bytes],
68+
)
69+
path = resolve_file_source("https://pypi.org")
70+
with open(path) as f:
71+
text = f.read()
72+
assert text == expected_str

0 commit comments

Comments
 (0)