Skip to content

Commit 1a0c75c

Browse files
authored
fix(web): raise an error when a files_upload_v2 file exceeds 1 GB (#2049)
Signed-off-by: Aymane El Homaïdi <60507@etu.he2b.be>
1 parent 6a772c6 commit 1a0c75c

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎slack_sdk/web/internal_utils.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,21 @@ def _remove_none_values(d: dict) -> dict:
332332
return {k: v for k, v in d.items() if v is not None}
333333

334334

335+
MAX_V2_FILE_SIZE = 1 * 1024 * 1024 * 1024 # 1 GB in bytes (Slack files.upload limit)
336+
337+
335338
def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[Any]]:
336339
file = upload_file.get("file")
337340
content = upload_file.get("content")
338341
data: Optional[bytes] = None
339342
if file is not None:
340343
if isinstance(file, (str, os.PathLike)): # filepath
344+
file_size = os.path.getsize(os.fsencode(file))
345+
if file_size > MAX_V2_FILE_SIZE:
346+
filename = upload_file.get("filename") or os.path.basename(os.fspath(file))
347+
raise SlackRequestError(
348+
f"Maximum upload file size is 1GB (file size: {file_size} bytes for filename: {filename})"
349+
)
341350
with open(os.fsencode(file), "rb") as readable:
342351
data = readable.read()
343352
elif isinstance(file, bytes):
@@ -368,6 +377,9 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
368377
if data is None:
369378
raise SlackRequestError(f"File content not found for filename: {filename}, title: {title}")
370379

380+
if len(data) > MAX_V2_FILE_SIZE:
381+
raise SlackRequestError(f"Maximum upload file size is 1GB (file size: {len(data)} bytes for filename: {filename})")
382+
371383
if title is None:
372384
title = filename # to be consistent with files.upload API
373385

‎tests/slack_sdk/web/test_internal_utils.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
from io import BytesIO
44
from pathlib import Path
55
from typing import Dict
6+
from unittest.mock import patch
7+
8+
from slack_sdk.errors import SlackRequestError
69

710
from slack_sdk.models.attachments import Attachment
811
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):
147150
assert upload_item_path == upload_item_str
148151
assert upload_item_str.get("filename") == "test_internal_utils.py"
149152

153+
def test_to_v2_file_upload_item_raises_when_file_path_exceeds_1gb(self):
154+
with patch("os.path.getsize", return_value=1_073_741_825):
155+
with self.assertRaises(SlackRequestError) as ctx:
156+
_to_v2_file_upload_item({"file": "tests/slack_sdk/web/test_internal_utils.py"})
157+
self.assertIn("Maximum upload file size is 1GB", str(ctx.exception))
158+
self.assertIn("1073741825 bytes", str(ctx.exception))
159+
160+
def test_to_v2_file_upload_item_raises_when_content_exceeds_1gb(self):
161+
with patch("slack_sdk.web.internal_utils.MAX_V2_FILE_SIZE", 10):
162+
with self.assertRaises(SlackRequestError) as ctx:
163+
_to_v2_file_upload_item({"content": "this content is longer than 10 bytes", "filename": "test.txt"})
164+
self.assertIn("Maximum upload file size is 1GB", str(ctx.exception))
165+
self.assertIn("test.txt", str(ctx.exception))
166+
150167
def test_next_cursor_is_present(self):
151168
assert _next_cursor_is_present({"next_cursor": "next-page"}) is True
152169
assert _next_cursor_is_present({"next_cursor": ""}) is False

0 commit comments

Comments
 (0)