Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(py): truncate long filenames from public URL uploads
jkomyno6e4cc38f0682
diff --git a/python/composio/core/models/_files.py b/python/composio/core/models/_files.pyindex 5c8d42db5..b4bf0f328 100644--- a/python/composio/core/models/_files.py+++ b/python/composio/core/models/_files.py@@ -28,6 +28,12 @@ if t.TYPE_CHECKING:_DEFAULT_CHUNK_SIZE = 1024 * 1024_FILE_UPLOAD = "/api/v3/files/upload/request"+_MAX_FILENAME_LENGTH = 100+"""+Maximum filename length to prevent issues with long URLs from public buckets.+Long filenames (containing hashes, UUIDs, or encoded metadata) are replaced+with timestamped filenames to match TypeScript SDK behavior.+"""LOCAL_CACHE_DIRECTORY_NAME = ".composio""""@@ -133,6 +139,35 @@ def _generate_timestamped_filename(extension: str) -> str:return f"file_{timestamp}_{unique_id}{extension}"+def _truncate_filename(filename: str, max_length: int = _MAX_FILENAME_LENGTH) -> str:+ """Truncate filename if it exceeds max length by generating a timestamped name.++ Long filenames are common with public bucket URLs containing hashes or UUIDs.+ These can cause issues, so we replace them with timestamped filenames while+ preserving the extension.++ Args:+ filename: The original filename+ max_length: Maximum allowed length for the filename++ Returns:+ The original filename if within limits, or a timestamped filename+ with the extension preserved if the original is too long+ """+ if len(filename) <= max_length:+ return filename++ # Extract extension+ if "." in filename:+ _, ext = filename.rsplit(".", 1)+ ext = f".{ext}"+ else:+ ext = ""++ # Generate a timestamped filename (matches TypeScript SDK behavior)+ return _generate_timestamped_filename(ext)++def _fetch_file_from_url(url: str) -> t.Tuple[str, bytes, str]:"""Fetch file content from a URL.@@ -165,6 +200,9 @@ def _fetch_file_from_url(url: str) -> t.Tuple[str, bytes, str]:extension = _get_extension_from_mimetype(mimetype)filename = _generate_timestamped_filename(extension)+ # Truncate long filenames (common with public bucket URLs containing hashes)+ filename = _truncate_filename(filename)+return filename, content, mimetypediff --git a/python/tests/test_files.py b/python/tests/test_files.pyindex aaea80d43..48e4c76df 100644--- a/python/tests/test_files.py+++ b/python/tests/test_files.py@@ -15,8 +15,10 @@ from composio.core.models._files import (_is_url,_get_extension_from_mimetype,_generate_timestamped_filename,+ _truncate_filename,_fetch_file_from_url,_upload_bytes_to_s3,+ _MAX_FILENAME_LENGTH,)from composio.core.models.base import allow_trackingfrom composio.exceptions import ErrorUploadingFile@@ -1402,3 +1404,196 @@ class TestFileHelperWithUrls:assert len(result["attachments"]) == 2assert result["attachments"][0]["s3key"] == "key1"assert result["attachments"][1]["s3key"] == "key2"+++class TestTruncateFilename:+ """Test cases for _truncate_filename function.++ Long filenames are common with public bucket URLs containing hashes or UUIDs.+ These can cause issues, so they are replaced with timestamped filenames.+ """++ def test_truncate_filename_short_unchanged(self):+ """Short filenames should not be modified."""+ assert _truncate_filename("document.pdf") == "document.pdf"+ assert _truncate_filename("image.jpg") == "image.jpg"+ assert _truncate_filename("file.txt") == "file.txt"++ def test_truncate_filename_at_limit_unchanged(self):+ """Filenames exactly at the limit should not be modified."""+ # Create a filename exactly at the limit (100 chars by default)+ name = "a" * 95 + ".pdf" # 95 + 4 = 99 chars+ assert _truncate_filename(name) == name++ name_at_limit = "a" * 96 + ".pdf" # 96 + 4 = 100 chars+ assert _truncate_filename(name_at_limit) == name_at_limit++ def test_truncate_filename_long_generates_timestamped(self):+ """Long filenames should be replaced with timestamped filename."""+ long_name = "a" * 150 + ".pdf"+ result = _truncate_filename(long_name)++ assert len(result) < _MAX_FILENAME_LENGTH+ assert result.startswith("file_")+ assert result.endswith(".pdf")++ def test_truncate_filename_preserves_extension(self):+ """Extension should be preserved when generating timestamped filename."""+ test_cases = [+ ("very_long_" * 20 + ".docx", ".docx"),+ ("hash_" * 30 + ".jpg", ".jpg"),+ ("uuid_" * 25 + ".png", ".png"),+ ("data_" * 40 + ".json", ".json"),+ ]++ for long_name, expected_ext in test_cases:+ result = _truncate_filename(long_name)+ assert result.endswith(expected_ext), (+ f"Expected {result} to end with {expected_ext}"+ )++ def test_truncate_filename_long_hash_url(self):+ """Hash-based filenames from URLs should be truncated."""+ # Typical long hash filename from public bucket URLs (needs to be >100 chars)+ hash_filename = "8f14e45fceea167a5a36dedd4bea2543_5d41402abc4b2a76b9719d911017c592_extra_hash_data_to_exceed_limit_download.jpg"+ assert len(hash_filename) > _MAX_FILENAME_LENGTH # Verify test setup++ result = _truncate_filename(hash_filename)++ assert len(result) <= _MAX_FILENAME_LENGTH+ assert result.startswith("file_")+ assert result.endswith(".jpg")++ def test_truncate_filename_multiple_hashes(self):+ """Multiple concatenated hashes should be truncated."""+ # 32 char hash * 5 = 160 chars + extension+ multi_hash = "8f14e45fceea167a5a36dedd4bea2543" * 5 + ".pdf"+ result = _truncate_filename(multi_hash)++ assert len(result) <= _MAX_FILENAME_LENGTH+ assert result.endswith(".pdf")++ def test_truncate_filename_no_extension(self):+ """Long filenames without extension should still be truncated."""+ long_name = "a" * 150+ result = _truncate_filename(long_name)++ assert len(result) <= _MAX_FILENAME_LENGTH+ assert result.startswith("file_")+ # Should not end with a dot+ assert not result.endswith(".")++ def test_truncate_filename_custom_max_length(self):+ """Custom max_length parameter should be respected."""+ name = "a" * 60 + ".txt" # 64 chars total++ # With default limit (100), should be unchanged+ assert _truncate_filename(name) == name++ # With custom limit (50), should be truncated+ result = _truncate_filename(name, max_length=50)+ assert len(result) <= 50+ assert result.startswith("file_")+ assert result.endswith(".txt")++ def test_truncate_filename_edge_case_one_over_limit(self):+ """Filename one character over limit should be truncated."""+ # Create filename exactly one char over the limit+ name = "a" * 97 + ".pdf" # 97 + 4 = 101 chars (one over default 100)+ result = _truncate_filename(name)++ assert len(result) < _MAX_FILENAME_LENGTH+ assert result.startswith("file_")+ assert result.endswith(".pdf")++ def test_truncate_filename_long_extension(self):+ """Long extensions should be preserved when truncating."""+ long_name = "file_" * 30 + ".dockerfile"+ result = _truncate_filename(long_name)++ assert result.endswith(".dockerfile")+ assert result.startswith("file_")++ def test_truncate_filename_multiple_dots_preserves_last_extension(self):+ """Filename with multiple dots should preserve only the last extension."""+ long_name = "archive" * 20 + ".backup.tar.gz"+ result = _truncate_filename(long_name)++ # rsplit(".", 1) takes the part after the last dot+ assert result.endswith(".gz")+++class TestFetchFileFromUrlWithTruncation:+ """Test cases for _fetch_file_from_url with filename truncation."""++ @patch("composio.core.models._files.requests.get")+ def test_fetch_truncates_long_filename(self, mock_get):+ """Long filenames from URLs should be truncated."""+ mock_response = MagicMock()+ mock_response.ok = True+ mock_response.content = b"test content"+ mock_response.headers = {"content-type": "application/pdf"}+ mock_get.return_value = mock_response++ # Create a very long filename (hash-based, common in public buckets)+ long_filename = "8f14e45fceea167a5a36dedd4bea2543" * 5 + ".pdf"++ filename, content, mimetype = _fetch_file_from_url(+ f"https://bucket.example.com/files/{long_filename}"+ )++ assert len(filename) <= _MAX_FILENAME_LENGTH+ assert filename.startswith("file_")+ assert filename.endswith(".pdf")+ assert content == b"test content"++ @patch("composio.core.models._files.requests.get")+ def test_fetch_preserves_short_filename(self, mock_get):+ """Short filenames should be preserved unchanged."""+ mock_response = MagicMock()+ mock_response.ok = True+ mock_response.content = b"image data"+ mock_response.headers = {"content-type": "image/jpeg"}+ mock_get.return_value = mock_response++ filename, content, mimetype = _fetch_file_from_url(+ "https://example.com/photo.jpg"+ )++ assert filename == "photo.jpg"++ @patch("composio.core.models._files.requests.get")+ def test_fetch_truncates_after_adding_extension(self, mock_get):+ """Truncation should happen after extension is appended."""+ mock_response = MagicMock()+ mock_response.ok = True+ mock_response.content = b"pdf content"+ mock_response.headers = {"content-type": "application/pdf"}+ mock_get.return_value = mock_response++ # URL without extension - extension will be added from mimetype+ long_name_no_ext = "8f14e45fceea167a5a36dedd4bea2543" * 4+ filename, content, mimetype = _fetch_file_from_url(+ f"https://bucket.example.com/{long_name_no_ext}"+ )++ # Should be truncated and have .pdf extension+ assert len(filename) <= _MAX_FILENAME_LENGTH+ assert filename.endswith(".pdf")++ @patch("composio.core.models._files.requests.get")+ def test_fetch_generated_filename_not_truncated(self, mock_get):+ """Generated timestamped filenames (when URL has no filename) should be short enough."""+ mock_response = MagicMock()+ mock_response.ok = True+ mock_response.content = b"data"+ mock_response.headers = {"content-type": "image/png"}+ mock_get.return_value = mock_response++ # URL with no filename - will generate a timestamped one+ filename, content, mimetype = _fetch_file_from_url("https://example.com/")++ # Generated filename should be naturally short+ assert filename.startswith("file_")+ assert filename.endswith(".png")+ assert len(filename) < 50 # Timestamped names are short