diff --git a/slack_sdk/web/internal_utils.py b/slack_sdk/web/internal_utils.py index 3a6db7203..a35de0191 100644 --- a/slack_sdk/web/internal_utils.py +++ b/slack_sdk/web/internal_utils.py @@ -332,12 +332,21 @@ def _remove_none_values(d: dict) -> dict: return {k: v for k, v in d.items() if v is not None} +MAX_V2_FILE_SIZE = 1 * 1024 * 1024 * 1024 # 1 GB in bytes (Slack files.upload limit) + + def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[Any]]: file = upload_file.get("file") content = upload_file.get("content") data: Optional[bytes] = None if file is not None: if isinstance(file, (str, os.PathLike)): # filepath + file_size = os.path.getsize(os.fsencode(file)) + if file_size > MAX_V2_FILE_SIZE: + filename = upload_file.get("filename") or os.path.basename(os.fspath(file)) + raise SlackRequestError( + f"Maximum upload file size is 1GB (file size: {file_size} bytes for filename: {filename})" + ) with open(os.fsencode(file), "rb") as readable: data = readable.read() elif isinstance(file, bytes): @@ -368,6 +377,9 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A if data is None: raise SlackRequestError(f"File content not found for filename: {filename}, title: {title}") + if len(data) > MAX_V2_FILE_SIZE: + raise SlackRequestError(f"Maximum upload file size is 1GB (file size: {len(data)} bytes for filename: {filename})") + if title is None: title = filename # to be consistent with files.upload API diff --git a/tests/slack_sdk/web/test_internal_utils.py b/tests/slack_sdk/web/test_internal_utils.py index e8456b471..020a5d20c 100644 --- a/tests/slack_sdk/web/test_internal_utils.py +++ b/tests/slack_sdk/web/test_internal_utils.py @@ -3,6 +3,9 @@ from io import BytesIO from pathlib import Path from typing import Dict +from unittest.mock import patch + +from slack_sdk.errors import SlackRequestError from slack_sdk.models.attachments import Attachment from slack_sdk.models.blocks import Block, DividerBlock @@ -147,6 +150,20 @@ def test_to_v2_file_upload_item_can_accept_file_as_path(self): assert upload_item_path == upload_item_str assert upload_item_str.get("filename") == "test_internal_utils.py" + def test_to_v2_file_upload_item_raises_when_file_path_exceeds_1gb(self): + with patch("os.path.getsize", return_value=1_073_741_825): + with self.assertRaises(SlackRequestError) as ctx: + _to_v2_file_upload_item({"file": "tests/slack_sdk/web/test_internal_utils.py"}) + self.assertIn("Maximum upload file size is 1GB", str(ctx.exception)) + self.assertIn("1073741825 bytes", str(ctx.exception)) + + def test_to_v2_file_upload_item_raises_when_content_exceeds_1gb(self): + with patch("slack_sdk.web.internal_utils.MAX_V2_FILE_SIZE", 10): + with self.assertRaises(SlackRequestError) as ctx: + _to_v2_file_upload_item({"content": "this content is longer than 10 bytes", "filename": "test.txt"}) + self.assertIn("Maximum upload file size is 1GB", str(ctx.exception)) + self.assertIn("test.txt", str(ctx.exception)) + def test_next_cursor_is_present(self): assert _next_cursor_is_present({"next_cursor": "next-page"}) is True assert _next_cursor_is_present({"next_cursor": ""}) is False