Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions integration_tests/web/test_files_upload_v2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from pathlib import Path
import unittest
from io import BytesIO

Expand Down Expand Up @@ -59,6 +60,18 @@ def test_uploading_bytes_io(self):
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

def test_uploading_text_files_path(self):
client = self.sync_client
file = __file__
upload = client.files_upload_v2(
channel=self.channel_id,
file=Path(file),
title="Test code",
)
self.assertIsNotNone(upload)
self.assertIsNotNone(upload.get("files")[0].get("id"))
self.assertIsNotNone(upload.get("files")[0].get("title"))

def test_uploading_multiple_files(self):
client = self.sync_client
file = __file__
Expand Down
2 changes: 1 addition & 1 deletion slack_sdk/web/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3749,7 +3749,7 @@ async def files_upload_v2(
*,
# for sending a single file
filename: Optional[str] = None, # you can skip this only when sending along with content parameter
file: Optional[Union[str, bytes, IOBase]] = None,
file: Optional[Union[str, bytes, IOBase, os.PathLike]] = None,
content: Optional[Union[str, bytes]] = None,
title: Optional[str] = None,
alt_txt: Optional[str] = None,
Expand Down
2 changes: 1 addition & 1 deletion slack_sdk/web/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3739,7 +3739,7 @@ def files_upload_v2(
*,
# for sending a single file
filename: Optional[str] = None, # you can skip this only when sending along with content parameter
file: Optional[Union[str, bytes, IOBase]] = None,
file: Optional[Union[str, bytes, IOBase, os.PathLike]] = None,
content: Optional[Union[str, bytes]] = None,
title: Optional[str] = None,
alt_txt: Optional[str] = None,
Expand Down
8 changes: 4 additions & 4 deletions slack_sdk/web/internal_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
content = upload_file.get("content")
data: Optional[bytes] = None
if file is not None:
if isinstance(file, str): # filepath
with open(file.encode("utf-8", "ignore"), "rb") as readable:
if isinstance(file, (str, os.PathLike)): # filepath
with open(os.fsencode(file), "rb") as readable:
data = readable.read()
elif isinstance(file, bytes):
data = file
Expand All @@ -339,8 +339,8 @@ def _to_v2_file_upload_item(upload_file: Dict[str, Any]) -> Dict[str, Optional[A
filename = upload_file.get("filename")
if filename is None:
# use the local filename if filename is missing
if isinstance(file, str):
filename = file.split(os.path.sep)[-1]
if isinstance(file, (str, os.PathLike)):
filename = os.fspath(file).split(os.path.sep)[-1]
else:
filename = "Uploaded file"

Expand Down
2 changes: 1 addition & 1 deletion slack_sdk/web/legacy_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3751,7 +3751,7 @@ def files_upload_v2(
*,
# for sending a single file
filename: Optional[str] = None, # you can skip this only when sending along with content parameter
file: Optional[Union[str, bytes, IOBase]] = None,
file: Optional[Union[str, bytes, IOBase, os.PathLike]] = None,
content: Optional[Union[str, bytes]] = None,
title: Optional[str] = None,
alt_txt: Optional[str] = None,
Expand Down
8 changes: 8 additions & 0 deletions tests/slack_sdk/web/test_internal_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import unittest
from io import BytesIO
from pathlib import Path
from typing import Dict, Sequence, Union

import pytest
Expand Down Expand Up @@ -101,6 +102,13 @@ def test_files_upload_v2_issue_1356(self):
file_io_item = _to_v2_file_upload_item({"file": file_io, "filename": "foo.txt"})
assert file_io_item.get("filename") == "foo.txt"

def test_to_v2_file_upload_item_can_accept_file_as_path(self):
filepath = "tests/slack_sdk/web/test_internal_utils.py"
upload_item_str = _to_v2_file_upload_item({"file": filepath})
upload_item_path = _to_v2_file_upload_item({"file": Path(filepath)})
assert upload_item_path == upload_item_str
assert upload_item_str.get("filename") == "test_internal_utils.py"

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
Expand Down
Loading