Skip to content

Commit

Permalink
guess mimetype prior to upload
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Feb 2, 2024
1 parent c518477 commit 6f9de46
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/pyDataverse/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"""Dataverse API wrapper for all it's API's."""

from io import IOBase
import json
import mimetypes
import os
import subprocess as sp
from typing import IO, Dict, Tuple, Union
from urllib.parse import urljoin

from requests import ConnectionError, Response, delete, get, post, put
Expand Down Expand Up @@ -172,6 +177,9 @@ def post_request(self, url, data=None, auth=False, params=None, files=None):
if self.api_token:
params["key"] = self.api_token

if files is not None:
files = self._extract_mimetypes(files)

try:
resp = post(url, data=data, params=params, files=files)
if resp.status_code == 401:
Expand All @@ -187,6 +195,51 @@ def post_request(self, url, data=None, auth=False, params=None, files=None):
"ERROR: POST - Could not establish connection to API: {0}".format(url)
)

def _extract_mimetypes(
self,
files: Dict[str, Union[IO, Tuple[str, IO, str]]],
) -> Dict[str, Union[IO, Tuple[str, IO, str]]]:
"""
Prepare files for upload to the server by adding the filename, file object, and mimetype.
Args:
files (dict): A dictionary containing the files to be prepared
Returns:
dict: A dictionary containing the prepared files
"""

assert isinstance(files, dict), "Files must be a dictionary"

for key, file in files.items():
files[key] = self._extract_mimetype(file)

return files

def _extract_mimetype(self, file: IOBase) -> Union[IO, Tuple[str, IO, str]]:
"""
Prepare files for upload to the server
Args:
file (IOBase): The file to be prepared for upload.
Returns:
tuple: A tuple containing the filename, file object, and mimetype.
Raises:
AssertionError: If the file is not a file-like object.
"""

assert isinstance(file, IOBase), "File must be a file-like object"

fname = os.path.basename(file.name)
mimetype, _ = mimetypes.guess_type(file.name)

if mimetype is None:
return file

return (fname, file, mimetype)

def put_request(self, url, data=None, auth=False, params=None):
"""Make a PUT request.
Expand Down

0 comments on commit 6f9de46

Please sign in to comment.