@@ -145,35 +145,23 @@ def __init__(self, api: AnnofabApi):
145
145
# Private Method
146
146
#########################################
147
147
@staticmethod
148
- def _get_content_type (file_path : str , content_type : Optional [ str ] = None ) -> str :
148
+ def _get_mime_type (file_path : str ) -> str :
149
149
"""
150
- ファイルパスからContent-Typeを取得する 。
150
+ ファイルパスからMIME Typeを返す。MIME Typeが推測できない場合は、``application/octet-stream`` を返す 。
151
151
152
152
Args:
153
- file_path: アップロードするファイルのパス
154
- content_type: アップロードするファイルのMIME Type. Noneの場合、ファイルパスから推測する。
153
+ file_path: MIME Typeを取得したいファイルのパス
155
154
156
155
Returns:
157
- APIに渡すContent-Type
158
-
159
- Raises:
160
- AnnofabApiException: Content-Typeを取得できなかった
156
+ ファイルパスから取得したMIME Type
161
157
162
158
"""
159
+ content_type , _ = mimetypes .guess_type (file_path )
160
+ if content_type is not None :
161
+ return content_type
163
162
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"
177
165
178
166
@staticmethod
179
167
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
867
855
"""
868
856
869
857
# 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
871
859
with open (file_path , "rb" ) as f :
872
860
try :
873
861
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(
1141
1129
"""
1142
1130
1143
1131
# 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
1145
1133
1146
1134
# S3にファイルアップロード
1147
1135
s3_path = self .upload_file_to_s3 (project_id , file_path , new_content_type )
@@ -1754,7 +1742,8 @@ def upload_instruction_image(
1754
1742
Returns:
1755
1743
一時データ保存先であるS3パス
1756
1744
"""
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
+
1758
1747
with open (file_path , "rb" ) as f :
1759
1748
return self .upload_data_as_instruction_image (project_id , image_id , data = f , content_type = new_content_type )
1760
1749
0 commit comments