Skip to content

Commit d814e84

Browse files
committed
fix: Refactor
1 parent 3a5858b commit d814e84

File tree

1 file changed

+29
-37
lines changed

1 file changed

+29
-37
lines changed

src/c2pa/c2pa.py

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,33 @@ def load_settings(settings: str, format: str = "json") -> None:
654654
return result
655655

656656

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+
657684
def read_ingredient_file(
658685
path: Union[str, Path], data_dir: Union[str, Path]) -> str:
659686
"""Read a file as C2PA ingredient.
@@ -1236,25 +1263,7 @@ def __init__(self,
12361263
# Create a stream from the file path in format_or_path
12371264
path = str(format_or_path)
12381265

1239-
# Extract file extension:
1240-
# path_obj.suffix returns the extension including
1241-
# the dot (e.g., ".jpg", ".png").
1242-
# If no extension exists, suffix returns empty string,
1243-
# so file_extension will be ""
1244-
path_obj = Path(path)
1245-
file_extension = path_obj.suffix.lower() if path_obj.suffix else ""
1246-
1247-
if file_extension == ".dng":
1248-
# mimetypes guesses the wrong type for dng,
1249-
# so we bypass it and set the correct type
1250-
mime_type = "image/dng"
1251-
else:
1252-
mime_type = mimetypes.guess_type(
1253-
path)[0]
1254-
1255-
if not mime_type:
1256-
raise C2paError.NotSupported(
1257-
f"Could not determine MIME type for file: {path}")
1266+
mime_type = _get_mime_type_from_path(path)
12581267

12591268
if mime_type not in Reader.get_supported_mime_types():
12601269
raise C2paError.NotSupported(
@@ -2299,25 +2308,8 @@ def sign_file(self,
22992308
Raises:
23002309
C2paError: If there was an error during signing
23012310
"""
2302-
# Extract file extension:
2303-
# path_obj.suffix returns the extension
2304-
# including the dot (e.g., ".jpg", ".png")
2305-
# If no extension exists, suffix returns empty string,
2306-
# so file_extension will be ""
2307-
file_extension = ""
2308-
path_obj = Path(source_path)
2309-
file_extension = path_obj.suffix.lower() if path_obj.suffix else ""
2310-
2311-
if file_extension == ".dng":
2312-
# mimetypes guesses the wrong type for dng,
2313-
# so we bypass it and set the correct type
2314-
mime_type = "image/dng"
2315-
else:
2316-
mime_type = mimetypes.guess_type(str(source_path))[0]
23172311

2318-
if not mime_type:
2319-
raise C2paError.NotSupported(
2320-
f"Could not determine MIME type for file: {source_path}")
2312+
mime_type = _get_mime_type_from_path(source_path)
23212313

23222314
try:
23232315
# Open source file and destination file, then use the sign method

0 commit comments

Comments
 (0)