Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
[HN-266] fix: dynamic content type on file upload (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToJen authored Jun 8, 2024
1 parent 414f98a commit 1c9de8b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions hive_agent_client/files/files.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import httpx
import logging
import mimetypes
import os
import sys

Expand All @@ -18,6 +19,9 @@ def get_log_level():
logger.setLevel(get_log_level())


ALLOWED_FILE_TYPES = ['application/json', 'text/csv', 'text/plain', 'application/pdf']


async def upload_files(http_client: httpx.AsyncClient, base_url: str, file_paths: List[str]) -> dict:
"""
Uploads files to the server.
Expand All @@ -30,7 +34,16 @@ async def upload_files(http_client: httpx.AsyncClient, base_url: str, file_paths
"""
endpoint = "/uploadfiles/"
url = f"{base_url}{endpoint}"
files = [("files", (os.path.basename(file_path), open(file_path, "rb"), "multipart/form-data")) for file_path in file_paths]

files = []
for file_path in file_paths:
file_name = os.path.basename(file_path)
content_type, _ = mimetypes.guess_type(file_path)

if content_type is None or content_type not in ALLOWED_FILE_TYPES:
raise ValueError(f"File type {content_type} is not allowed or missing for file {file_name}")

files.append(("files", (file_name, open(file_path, "rb"), content_type)))

try:
logger.debug(f"Uploading files to {url}")
Expand All @@ -39,7 +52,7 @@ async def upload_files(http_client: httpx.AsyncClient, base_url: str, file_paths
logger.debug(f"Response for uploading files to {url}: {response.json()}")
return response.json()
except httpx.HTTPStatusError as e:
logging.error(f"Failed to upload files: {e}")
logger.error(f"Failed to upload files: {e}")
raise Exception(f"Failed to upload files: {e.response.text}") from e
finally:
for _, (name, file, _) in files:
Expand Down

0 comments on commit 1c9de8b

Please sign in to comment.