mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 07:09:11 +02:00
Fix File helper extension handling (#4531)
Preserve explicit suffixes when building data-backed File resource URIs, while keeping the inferred-extension fallback for names without a suffix. Closes #4530 Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
This commit is contained in:
parent
16a09f0151
commit
74b8f1bc1c
2 changed files with 22 additions and 1 deletions
|
|
@ -421,7 +421,12 @@ class File:
|
|||
elif self.data is not None:
|
||||
raw_data = self.data
|
||||
if self._name:
|
||||
uri_str = f"file:///{self._name}.{self._mime_type.split('/')[1]}"
|
||||
extension = (
|
||||
""
|
||||
if Path(self._name).suffix
|
||||
else f".{self._mime_type.split('/')[1]}"
|
||||
)
|
||||
uri_str = f"file:///{self._name}{extension}"
|
||||
else:
|
||||
uri_str = f"file:///resource.{self._mime_type.split('/')[1]}"
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -461,6 +461,22 @@ class TestFile:
|
|||
if isinstance(resource.resource, BlobResourceContents):
|
||||
assert resource.resource.blob == base64.b64encode(test_data).decode()
|
||||
|
||||
def test_to_resource_content_with_data_and_name_without_extension(self):
|
||||
"""Test data-backed File URI with a custom name that needs an extension."""
|
||||
file = File(data=b"test file data", format="pdf", name="report")
|
||||
resource = file.to_resource_content()
|
||||
|
||||
assert resource.resource.mime_type == "application/pdf"
|
||||
assert str(resource.resource.uri) == "file:///report.pdf"
|
||||
|
||||
def test_to_resource_content_with_data_preserves_name_extension(self):
|
||||
"""Test data-backed File URI preserves a custom name with an extension."""
|
||||
file = File(data=b"test file data", format="pdf", name="report.pdf")
|
||||
resource = file.to_resource_content()
|
||||
|
||||
assert resource.resource.mime_type == "application/pdf"
|
||||
assert str(resource.resource.uri) == "file:///report.pdf"
|
||||
|
||||
def test_to_resource_content_with_text_data(self):
|
||||
"""Test conversion to ResourceContent with text data (TextResourceContents)."""
|
||||
test_data = b"hello world"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue