Skip to content

Commit 3f5cea4

Browse files
authored
Wrapper.upload_file_to_s3など:Content-Typeが取得できないときは、Errorをスローするのではなく、"application/octet-stream"を渡すようにする (#362)
* content-typeが推測できないときの処理を変更 * format * format * update README
1 parent 8140777 commit 3f5cea4

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

README.md

-6
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
# 廃止予定
2828

2929

30-
### 2021-09-01 以降
31-
32-
* [annofabapi.wrapper.AnnofabApiWrapper.copy_annotation_specs](https://annofab-api-python-client.readthedocs.io/en/latest/wrapper.html#annofabapi.Wrapper.copy_annotation_specs) を廃止します。特殊なケースにしか対応しておらず、汎用的なメソッドでないためです。
33-
* `annofabapi.models.JobType` を廃止します。替わりに`annofabapi.models.ProjectJobType`を使用してください。
34-
* `annofabapi.dataclass.job.JobInfo`を廃止します。替わりに`annofabapi.models.ProjectJobInfo`を使用してください。
35-
3630
### 2022-01-01 以降
3731
* Python3.6のサポートを停止し、対応するPythonバージョンを3.7以上にします。
3832

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.48.0"
1+
__version__ = "0.48.1"

annofabapi/wrapper.py

+13-24
Original file line numberDiff line numberDiff line change
@@ -145,35 +145,23 @@ def __init__(self, api: AnnofabApi):
145145
# Private Method
146146
#########################################
147147
@staticmethod
148-
def _get_content_type(file_path: str, content_type: Optional[str] = None) -> str:
148+
def _get_mime_type(file_path: str) -> str:
149149
"""
150-
ファイルパスからContent-Typeを取得する
150+
ファイルパスからMIME Typeを返す。MIME Typeが推測できない場合は、``application/octet-stream`` を返す
151151
152152
Args:
153-
file_path: アップロードするファイルのパス
154-
content_type: アップロードするファイルのMIME Type. Noneの場合、ファイルパスから推測する。
153+
file_path: MIME Typeを取得したいファイルのパス
155154
156155
Returns:
157-
APIに渡すContent-Type
158-
159-
Raises:
160-
AnnofabApiException: Content-Typeを取得できなかった
156+
ファイルパスから取得したMIME Type
161157
162158
"""
159+
content_type, _ = mimetypes.guess_type(file_path)
160+
if content_type is not None:
161+
return content_type
163162

164-
if content_type is None:
165-
new_content_type = mimetypes.guess_type(file_path)[0]
166-
if new_content_type is None:
167-
logger.info("mimetypes.guess_type function can't guess type. file_path = %s", file_path)
168-
new_content_type = content_type
169-
170-
else:
171-
new_content_type = content_type
172-
173-
if new_content_type is None:
174-
raise AnnofabApiException("content_type is none")
175-
176-
return new_content_type
163+
logger.info("ファイルパス '%s' からMIME Typeを推測できませんでした。MIME Typeは `application/octet-stream' とみなします。", file_path)
164+
return "application/octet-stream"
177165

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

869857
# content_type を推測
870-
new_content_type = self._get_content_type(file_path, content_type)
858+
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type
871859
with open(file_path, "rb") as f:
872860
try:
873861
return self.upload_data_to_s3(project_id, data=f, content_type=new_content_type)
@@ -1141,7 +1129,7 @@ def put_supplementary_data_from_file(
11411129
"""
11421130

11431131
# content_type を推測
1144-
new_content_type = self._get_content_type(file_path, content_type)
1132+
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type
11451133

11461134
# S3にファイルアップロード
11471135
s3_path = self.upload_file_to_s3(project_id, file_path, new_content_type)
@@ -1754,7 +1742,8 @@ def upload_instruction_image(
17541742
Returns:
17551743
一時データ保存先であるS3パス
17561744
"""
1757-
new_content_type = self._get_content_type(file_path, content_type)
1745+
new_content_type = self._get_mime_type(file_path) if content_type is None else content_type
1746+
17581747
with open(file_path, "rb") as f:
17591748
return self.upload_data_as_instruction_image(project_id, image_id, data=f, content_type=new_content_type)
17601749

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "annofabapi"
3-
version = "0.48.0"
3+
version = "0.48.1"
44
description = "Python Clinet Library of AnnoFab WebAPI (https://annofab.com/docs/api/)"
55
authors = ["yuji38kwmt"]
66
license = "MIT"

tests/test_local_wrapper.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
from pathlib import Path
3+
4+
from annofabapi.wrapper import Wrapper
5+
6+
# プロジェクトトップに移動する
7+
os.chdir(os.path.dirname(os.path.abspath(__file__)) + "/../")
8+
9+
data_dir = Path("./tests/data")
10+
11+
12+
class TestWrapperUtils:
13+
def test__get_mime_type(self):
14+
assert Wrapper._get_mime_type(str(data_dir / "lenna.png")) == "image/png"
15+
assert Wrapper._get_mime_type("sample.jpg") == "image/jpeg"
16+
assert Wrapper._get_mime_type("sample.txt") == "text/plain"
17+
assert Wrapper._get_mime_type("sample") == "application/octet-stream"

0 commit comments

Comments
 (0)