@@ -654,6 +654,33 @@ def load_settings(settings: str, format: str = "json") -> None:
654
654
return result
655
655
656
656
657
+ def _get_mime_type_from_path (path : Union [str , Path ]) -> str :
658
+ """Attempt to guess the MIME type from a file path (with extension).
659
+
660
+ Args:
661
+ path: File path as string or Path object
662
+
663
+ Returns:
664
+ MIME type string
665
+
666
+ Raises:
667
+ C2paError.NotSupported: If MIME type cannot be determined
668
+ """
669
+ path_obj = Path (path )
670
+ file_extension = path_obj .suffix .lower () if path_obj .suffix else ""
671
+
672
+ if file_extension == ".dng" :
673
+ # mimetypes guesses the wrong type for dng,
674
+ # so we bypass it and set the correct type
675
+ return "image/dng"
676
+ else :
677
+ mime_type = mimetypes .guess_type (str (path ))[0 ]
678
+ if not mime_type :
679
+ raise C2paError .NotSupported (
680
+ f"Could not determine MIME type for file: { path } " )
681
+ return mime_type
682
+
683
+
657
684
def read_ingredient_file (
658
685
path : Union [str , Path ], data_dir : Union [str , Path ]) -> str :
659
686
"""Read a file as C2PA ingredient.
@@ -1214,7 +1241,11 @@ def __init__(self,
1214
1241
"""Create a new Reader.
1215
1242
1216
1243
Args:
1217
- format_or_path: The format or path to read from
1244
+ format_or_path: The format or path to read from.
1245
+ The stream API (params format and an open stream) is
1246
+ the recommended way to use the Reader. For paths, we
1247
+ will attempt to guess the mimetype of the source
1248
+ file based on the extension.
1218
1249
stream: Optional stream to read from (Python stream-like object)
1219
1250
manifest_data: Optional manifest data in bytes
1220
1251
@@ -1231,12 +1262,8 @@ def __init__(self,
1231
1262
# If we don't get a stream as param:
1232
1263
# Create a stream from the file path in format_or_path
1233
1264
path = str (format_or_path )
1234
- mime_type = mimetypes .guess_type (
1235
- path )[0 ]
1236
1265
1237
- if not mime_type :
1238
- raise C2paError .NotSupported (
1239
- f"Could not determine MIME type for file: { path } " )
1266
+ mime_type = _get_mime_type_from_path (path )
1240
1267
1241
1268
if mime_type not in Reader .get_supported_mime_types ():
1242
1269
raise C2paError .NotSupported (
@@ -2269,7 +2296,9 @@ def sign_file(self,
2269
2296
"""Sign a file and write the signed data to an output file.
2270
2297
2271
2298
Args:
2272
- source_path: Path to the source file
2299
+ source_path: Path to the source file. We will attempt
2300
+ to guess the mimetype of the source file based on
2301
+ the extension.
2273
2302
dest_path: Path to write the signed file to
2274
2303
signer: The signer to use
2275
2304
@@ -2279,11 +2308,8 @@ def sign_file(self,
2279
2308
Raises:
2280
2309
C2paError: If there was an error during signing
2281
2310
"""
2282
- # Get the MIME type from the file extension
2283
- mime_type = mimetypes .guess_type (str (source_path ))[0 ]
2284
- if not mime_type :
2285
- raise C2paError .NotSupported (
2286
- f"Could not determine MIME type for file: { source_path } " )
2311
+
2312
+ mime_type = _get_mime_type_from_path (source_path )
2287
2313
2288
2314
try :
2289
2315
# Open source file and destination file, then use the sign method
0 commit comments