diff --git a/README.md b/README.md index aa03db8a..f2176182 100644 --- a/README.md +++ b/README.md @@ -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以上にします。 diff --git a/annofabapi/__version__.py b/annofabapi/__version__.py index 3158ac8c..f2897f34 100644 --- a/annofabapi/__version__.py +++ b/annofabapi/__version__.py @@ -1 +1 @@ -__version__ = "0.48.0" +__version__ = "0.48.1" diff --git a/annofabapi/wrapper.py b/annofabapi/wrapper.py index 6072268e..5be25f0f 100644 --- a/annofabapi/wrapper.py +++ b/annofabapi/wrapper.py @@ -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]]: @@ -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) @@ -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) @@ -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) diff --git a/pyproject.toml b/pyproject.toml index 7171b044..f29557ee 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tests/test_local_wrapper.py b/tests/test_local_wrapper.py new file mode 100644 index 00000000..c84262c2 --- /dev/null +++ b/tests/test_local_wrapper.py @@ -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"