Skip to content

Commit

Permalink
Merge pull request #12 from aoxolotl/fixMimetype
Browse files Browse the repository at this point in the history
Support automatic mimetype inference
  • Loading branch information
aoxolotl authored Aug 23, 2023
2 parents 96ae6cc + 6f305d8 commit ba844f9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
<img alt="Build Status" src="https://img.shields.io/github/actions/workflow/status/datatorch/python/package.yml?branch=master">
</p>

## Installation

*Note*: Newer versions (>=0.4.8.2) of DataTorch client require `libmagic` for file uploads
to datasets. Your system may or may not have `libmagic` pre-installed. Please
make sure you have `libmagic` installed before proceeding.

```bash
pip install datatorch
```
Expand Down
11 changes: 9 additions & 2 deletions datatorch/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import IO, overload, cast
from urllib import request
from urllib.parse import urlencode
import magic

from datatorch.api.entity.dataset import Dataset
from datatorch.api.entity.storage_link import StorageLink
Expand Down Expand Up @@ -141,10 +142,16 @@ def upload_to_default_filesource(
datasetId = "" if dataset is None else dataset.id
importFiles = "false" if dataset is None else "true"
endpoint = f"{self.api_url}/file/v1/upload/{storageId}?path={storageFolderName}&import={importFiles}&datasetId={datasetId}"
# r = requests.post(endpoint, files={"file": file}, user=self.viewer)

# save the current position
tell = file.tell()
# read 1024 bytes and get the mimetype
mimetype = magic.from_buffer(file.read(1024), mime=True)
# go back to the saved position
file.seek(tell)
r = requests.post(
endpoint,
files={"file": file},
files={"file": (os.path.basename(file.name), file, mimetype)},
headers={self.token_header: self._api_token},
stream=True,
)
Expand Down
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

requirements = [
"Click==8.0.0",
"numpy",
"docker",
"gql==3.4.0",
"websockets==10.4",
"websocket-client",
Expand All @@ -26,13 +24,16 @@
"shapely==2.0.1",
"tqdm~=4.65.0",
"urllib3==1.26.15",
"numpy",
"docker",
"python-magic",
]

requirements_agents = []

setup(
name="datatorch",
version="0.4.8.1",
version="0.4.8.2",
description="A CLI and library for interacting with DataTorch.",
author="DataTorch",
author_email="support@datatorch.io",
Expand Down

0 comments on commit ba844f9

Please sign in to comment.