Skip to content

Commit

Permalink
Wrapper.upload_file_to_s3など:Content-Typeが取得できないときは、Errorをスローするのではなく…
Browse files Browse the repository at this point in the history
…、"application/octet-stream"を渡すようにする (#362)

* content-typeが推測できないときの処理を変更

* format

* format

* update README
  • Loading branch information
yuji38kwmt committed Sep 27, 2021
1 parent 8140777 commit 3f5cea4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
# 廃止予定


### 2021-09-01 以降

* [annofabapi.wrapper.AnnofabApiWrapper.copy_annotation_specs](https://annofab-api-python-client.readthedocs.io/en/latest/wrapper.html#annofabapi.Wrapper.copy_annotation_specs) を廃止します。特殊なケースにしか対応しておらず、汎用的なメソッドでないためです。
* `annofabapi.models.JobType` を廃止します。替わりに`annofabapi.models.ProjectJobType`を使用してください。
* `annofabapi.dataclass.job.JobInfo`を廃止します。替わりに`annofabapi.models.ProjectJobInfo`を使用してください。

### 2022-01-01 以降
* Python3.6のサポートを停止し、対応するPythonバージョンを3.7以上にします。

Expand Down
2 changes: 1 addition & 1 deletion annofabapi/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.48.0"
__version__ = "0.48.1"
37 changes: 13 additions & 24 deletions annofabapi/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,23 @@ def __init__(self, api: AnnofabApi):
# Private Method
#########################################
@staticmethod
def _get_content_type(file_path: str, content_type: Optional[str] = None) -> str:
def _get_mime_type(file_path: str) -> str:
"""
ファイルパスからContent-Typeを取得する
ファイルパスからMIME Typeを返す。MIME Typeが推測できない場合は、``application/octet-stream`` を返す
Args:
file_path: アップロードするファイルのパス
content_type: アップロードするファイルのMIME Type. Noneの場合、ファイルパスから推測する。
file_path: MIME Typeを取得したいファイルのパス
Returns:
APIに渡すContent-Type
Raises:
AnnofabApiException: Content-Typeを取得できなかった
ファイルパスから取得したMIME Type
"""
content_type, _ = mimetypes.guess_type(file_path)
if content_type is not None:
return content_type

if content_type is None:
new_content_type = mimetypes.guess_type(file_path)[0]
if new_content_type is None:
logger.info("mimetypes.guess_type function can't guess type. file_path = %s", file_path)
new_content_type = content_type

else:
new_content_type = content_type

if new_content_type is None:
raise AnnofabApiException("content_type is none")

return new_content_type
logger.info("ファイルパス '%s' からMIME Typeを推測できませんでした。MIME Typeは `application/octet-stream' とみなします。", file_path)
return "application/octet-stream"

@staticmethod
def _get_all_objects(func_get_list: Callable, limit: int, **kwargs_for_func_get_list) -> List[Dict[str, Any]]:
Expand Down Expand Up @@ -867,7 +855,7 @@ def upload_file_to_s3(self, project_id: str, file_path: str, content_type: Optio
"""

# content_type を推測
new_content_type = self._get_content_type(file_path, content_type)
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type
with open(file_path, "rb") as f:
try:
return self.upload_data_to_s3(project_id, data=f, content_type=new_content_type)
Expand Down Expand Up @@ -1141,7 +1129,7 @@ def put_supplementary_data_from_file(
"""

# content_type を推測
new_content_type = self._get_content_type(file_path, content_type)
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type

# S3にファイルアップロード
s3_path = self.upload_file_to_s3(project_id, file_path, new_content_type)
Expand Down Expand Up @@ -1754,7 +1742,8 @@ def upload_instruction_image(
Returns:
一時データ保存先であるS3パス
"""
new_content_type = self._get_content_type(file_path, content_type)
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type

with open(file_path, "rb") as f:
return self.upload_data_as_instruction_image(project_id, image_id, data=f, content_type=new_content_type)

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "annofabapi"
version = "0.48.0"
version = "0.48.1"
description = "Python Clinet Library of AnnoFab WebAPI (https://annofab.com/docs/api/)"
authors = ["yuji38kwmt"]
license = "MIT"
Expand Down
17 changes: 17 additions & 0 deletions tests/test_local_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import os
from pathlib import Path

from annofabapi.wrapper import Wrapper

# プロジェクトトップに移動する
os.chdir(os.path.dirname(os.path.abspath(__file__)) + "/../")

data_dir = Path("./tests/data")


class TestWrapperUtils:
def test__get_mime_type(self):
assert Wrapper._get_mime_type(str(data_dir / "lenna.png")) == "image/png"
assert Wrapper._get_mime_type("sample.jpg") == "image/jpeg"
assert Wrapper._get_mime_type("sample.txt") == "text/plain"
assert Wrapper._get_mime_type("sample") == "application/octet-stream"

0 comments on commit 3f5cea4

Please sign in to comment.