Skip to content

Commit a297d22

Browse files
author
rllin
authored
[BACKEND-766] upload with content type guess (#28)
* wip * clean up * change log and bump version
1 parent 2fe7d66 commit a297d22

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 2.4.1 (2020-07-22)
4+
### Fixed
5+
* `Dataset.create_data_row` and `Dataset.create_data_rows` will now upload with content type to ensure the Labelbox editor can show videos.
6+
37
## Version 2.4 (2020-01-30)
48

59
### Added

labelbox/client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from datetime import datetime, timezone
22
import json
33
import logging
4+
import mimetypes
45
import os
56

67
import requests
@@ -171,6 +172,24 @@ def check_errors(keywords, *path):
171172

172173
return response["data"]
173174

175+
def upload_file(self, path):
176+
"""Uploads given path to local file.
177+
178+
Also includes best guess at the content type of the file.
179+
180+
Args:
181+
path (str): path to local file to be uploaded.
182+
Returns:
183+
str, the URL of uploaded data.
184+
Raises:
185+
labelbox.exceptions.LabelboxError: If upload failed.
186+
187+
"""
188+
content_type, _ = mimetypes.guess_type(path)
189+
basename = os.path.basename(path)
190+
with open(path, "rb") as f:
191+
return self.upload_data(data=(basename, f.read(), content_type))
192+
174193
def upload_data(self, data):
175194
""" Uploads the given data (bytes) to Labelbox.
176195
@@ -183,8 +202,8 @@ def upload_data(self, data):
183202
"""
184203
request_data = {
185204
"operations": json.dumps({
186-
"variables": {"file": None, "contentLength": len(data), "sign": False},
187-
"query": """mutation UploadFile($file: Upload!, $contentLength: Int!,
205+
"variables": {"file": None, "contentLength": len(data), "sign": False},
206+
"query": """mutation UploadFile($file: Upload!, $contentLength: Int!,
188207
$sign: Boolean) {
189208
uploadFile(file: $file, contentLength: $contentLength,
190209
sign: $sign) {url filename} } """,}),

labelbox/schema/dataset.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ def create_data_row(self, **kwargs):
4848
# If row data is a local file path, upload it to server.
4949
row_data = kwargs[DataRow.row_data.name]
5050
if os.path.exists(row_data):
51-
with open(row_data, "rb") as f:
52-
kwargs[DataRow.row_data.name] = self.client.upload_data(f.read())
51+
kwargs[DataRow.row_data.name] = self.client.upload_file(row_data)
5352

5453
kwargs[DataRow.dataset.name] = self
5554

5655
return self.client._create(DataRow, kwargs)
5756

5857
def create_data_rows(self, items):
5958
""" Creates multiple DataRow objects based on the given `items`.
60-
59+
6160
Each element in `items` can be either a `str` or a `dict`. If
6261
it is a `str`, then it is interpreted as a local file path. The file
6362
is uploaded to Labelbox and a DataRow referencing it is created.
@@ -91,9 +90,7 @@ def create_data_rows(self, items):
9190

9291
def upload_if_necessary(item):
9392
if isinstance(item, str):
94-
with open(item, "rb") as f:
95-
item_data = f.read()
96-
item_url = self.client.upload_data(item_data)
93+
item_url = self.client.upload_file(item)
9794
# Convert item from str into a dict so it gets processed
9895
# like all other dicts.
9996
item = {DataRow.row_data: item_url,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setuptools.setup(
99
name="labelbox",
10-
version="2.4",
10+
version="2.4.1",
1111
author="Labelbox",
1212
author_email="[email protected]",
1313
description="Labelbox Python API",

0 commit comments

Comments
 (0)