Skip to content

fix: use base name when uploading files #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2024
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
2 changes: 1 addition & 1 deletion taskingai/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__title__ = "taskingai"
__version__ = "0.2.2"
__version__ = "0.2.3"
23 changes: 17 additions & 6 deletions taskingai/file/file.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Union, Dict, BinaryIO
from io import BufferedReader
import os

from taskingai.client.models import FileIdData, UploadFilePurpose, UploadFileResponse
from taskingai.client.utils import get_api_client
Expand All @@ -16,14 +16,25 @@ def __prepare_files(file: BinaryIO, purpose: Union[UploadFilePurpose, str]) -> D
:param purpose: The purpose of the upload, either as a string or UploadFilePurpose enum.
:return: A dictionary formatted for the API call.
"""
if not isinstance(file, BufferedReader):
raise ValueError("Unsupported file type: Expected a BufferedReader")
if not hasattr(file, "read"):
raise ValueError("Unsupported file type: Expected a file-like object with a read method")

file_bytes = file.read()
file_name = file.name
try:
file_bytes = file.read()
except Exception as e:
raise ValueError(f"Error reading file: {e}")

file_name = os.path.basename(file.name)

if isinstance(purpose, str):
purpose = UploadFilePurpose(purpose)
try:
purpose = UploadFilePurpose(purpose)
except ValueError:
raise ValueError(f"Invalid purpose value: {purpose}")

if not isinstance(purpose, UploadFilePurpose):
raise ValueError("Purpose must be an instance of UploadFilePurpose or a valid string")

return {
"file": (file_name, file_bytes, "application/octet-stream"),
"purpose": (None, str(purpose.value)),
Expand Down
Loading