Skip to content

Commit dac4b31

Browse files
committed
fix: DNG handling
1 parent ac43887 commit dac4b31

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

src/c2pa/c2pa.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,8 +1231,22 @@ def __init__(self,
12311231
# If we don't get a stream as param:
12321232
# Create a stream from the file path in format_or_path
12331233
path = str(format_or_path)
1234-
mime_type = mimetypes.guess_type(
1235-
path)[0]
1234+
1235+
# Extract file extension:
1236+
# path_obj.suffix returns the extension including
1237+
# the dot (e.g., ".jpg", ".png").
1238+
# If no extension exists, suffix returns empty string,
1239+
# so file_extension will be ""
1240+
path_obj = Path(path)
1241+
file_extension = path_obj.suffix.lower() if path_obj.suffix else ""
1242+
1243+
if file_extension == ".dng":
1244+
# mimetypes guesses the wrong type for dng,
1245+
# so we bypass it and set the correct type
1246+
mime_type = "image/dng"
1247+
else:
1248+
mime_type = mimetypes.guess_type(
1249+
path)[0]
12361250

12371251
if not mime_type:
12381252
raise C2paError.NotSupported(
@@ -2279,8 +2293,22 @@ def sign_file(self,
22792293
Raises:
22802294
C2paError: If there was an error during signing
22812295
"""
2282-
# Get the MIME type from the file extension
2283-
mime_type = mimetypes.guess_type(str(source_path))[0]
2296+
# Extract file extension:
2297+
# path_obj.suffix returns the extension
2298+
# including the dot (e.g., ".jpg", ".png")
2299+
# If no extension exists, suffix returns empty string,
2300+
# so file_extension will be ""
2301+
file_extension = ""
2302+
path_obj = Path(source_path)
2303+
file_extension = path_obj.suffix.lower() if path_obj.suffix else ""
2304+
2305+
if file_extension == ".dng":
2306+
# mimetypes guesses the wrong type for dng,
2307+
# so we bypass it and set the correct type
2308+
mime_type = "image/dng"
2309+
else:
2310+
mime_type = mimetypes.guess_type(str(source_path))[0]
2311+
22842312
if not mime_type:
22852313
raise C2paError.NotSupported(
22862314
f"Could not determine MIME type for file: {source_path}")

tests/fixtures/C.dng

162 KB
Binary file not shown.

tests/test_unit_tests.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ def test_reader_streams_with_nested(self):
126126
def test_reader_close_cleanup(self):
127127
with open(self.testPath, "rb") as file:
128128
reader = Reader("image/jpeg", file)
129-
# Store references to internal objects
130-
reader_ref = reader._reader
131-
stream_ref = reader._own_stream
132129
# Close the reader
133130
reader.close()
134131
# Verify all resources are cleaned up
@@ -144,6 +141,23 @@ def test_resource_to_stream_on_closed_reader(self):
144141
with self.assertRaises(Error):
145142
reader.resource_to_stream("", io.BytesIO(bytearray()))
146143

144+
def test_read_dng_from_stream(self):
145+
test_path = os.path.join(self.data_dir, "C.dng")
146+
with open(test_path, "rb") as file:
147+
file_content = file.read()
148+
149+
with Reader("dng", io.BytesIO(file_content)) as reader:
150+
# Just run and verify there is no crash
151+
json.loads(reader.json())
152+
153+
def test_read_dng_file_from_path(self):
154+
test_path = os.path.join(self.data_dir, "C.dng")
155+
156+
# Create reader with the file content
157+
with Reader(test_path) as reader:
158+
# Just run and verify there is no crash
159+
json.loads(reader.json())
160+
147161
def test_read_all_files(self):
148162
"""Test reading C2PA metadata from all files in the fixtures/files-for-reading-tests directory"""
149163
reading_dir = os.path.join(self.data_dir, "files-for-reading-tests")

0 commit comments

Comments
 (0)