Skip to content

Commit 910560f

Browse files
committed
fix: One char stream fix
1 parent e61a061 commit 910560f

File tree

2 files changed

+45
-58
lines changed

2 files changed

+45
-58
lines changed

src/c2pa/c2pa.py

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -828,38 +828,18 @@ def sign_file(
828828
# Create a builder from the manifest
829829
builder = Builder(manifest)
830830

831-
# Open source file and create BytesIO for destination
832-
with open(source_path, 'rb') as source_file:
833-
# Get the MIME type from the file extension
834-
mime_type = mimetypes.guess_type(str(source_path))[0]
835-
if not mime_type:
836-
raise C2paError.NotSupported(
837-
f"Could not determine MIME type for file: {source_path}")
838-
839-
# Convert Python streams to Stream objects for internal signing
840-
source_stream = Stream(source_file)
841-
# In-memory buffer stream that will be flushed to file
842-
dest_stream = Stream(io.BytesIO(bytearray()))
843-
844-
# Use the builder's internal signing logic to get manifest
845-
# bytes
846-
manifest_bytes = builder._sign_internal(
847-
signer, mime_type, source_stream, dest_stream)
848-
849-
source_stream.close()
850-
851-
# Write the in-memory signed content
852-
# from BytesIO to the destination file
853-
with open(dest_path, 'wb') as dest_file:
854-
dest_stream.write_to_target(dest_file)
855-
dest_stream.close()
831+
manifest_bytes = builder.sign_file(
832+
source_path,
833+
dest_path,
834+
signer
835+
)
856836

857-
if return_manifest_as_bytes:
858-
return manifest_bytes
859-
else:
860-
# Read the signed manifest from the destination file
861-
with Reader(dest_path) as reader:
862-
return reader.json()
837+
if return_manifest_as_bytes:
838+
return manifest_bytes
839+
else:
840+
# Read the signed manifest from the destination file
841+
with Reader(dest_path) as reader:
842+
return reader.json()
863843

864844
except Exception as e:
865845
# Clean up destination file if it exists and there was an error
@@ -2276,7 +2256,7 @@ def sign_file(self,
22762256
signer: The signer to use
22772257
22782258
Returns:
2279-
A tuple of (size of C2PA data, manifest bytes)
2259+
Manifest bytes
22802260
22812261
Raises:
22822262
C2paError: If there was an error during signing
@@ -2288,28 +2268,9 @@ def sign_file(self,
22882268
f"Could not determine MIME type for file: {source_path}")
22892269

22902270
try:
2291-
# Open source file and create BytesIO for destination
2292-
with open(source_path, 'rb') as source_file:
2293-
source_stream = Stream(source_file)
2294-
2295-
# In-memory buffer stream that will be flushed to target,
2296-
# to ensure proper in-memory processing and validatin
2297-
dest_stream = Stream(io.BytesIO(bytearray()))
2298-
2299-
# Use the internal stream-base signing logic to
2300-
# sign the content to the BytesIO stream
2301-
result = self._sign_internal(
2302-
signer, mime_type, source_stream, dest_stream)
2303-
2304-
source_stream.close()
2305-
2306-
# Write the signed content from in-memory stream
2307-
# to the destination file
2308-
with open(dest_path, 'wb') as dest_file:
2309-
dest_stream.write_to_target(dest_file)
2310-
dest_stream.close()
2311-
2312-
return result
2271+
# Open source file and destination file, then use the sign method
2272+
with open(source_path, 'rb') as source_file, open(dest_path, 'w+b') as dest_file:
2273+
return self.sign(signer, mime_type, source_file, dest_file)
23132274
except Exception as e:
23142275
raise C2paError(f"Error signing file: {str(e)}")
23152276

tests/test_unit_tests.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,35 @@ def test_streams_sign_with_es256_alg_v1_manifest(self):
506506

507507
output.close()
508508

509+
def test_streams_sign_with_es256_alg_v1_manifest_to_file(self):
510+
test_file_name = os.path.join(self.data_dir, "temp_data", "temp_signing.jpg")
511+
# Ensure tmp directory exists
512+
os.makedirs(os.path.dirname(test_file_name), exist_ok=True)
513+
514+
# Ensure the target file exists before opening it in rb+ mode
515+
with open(test_file_name, "wb") as f:
516+
pass # Create empty file
517+
518+
try:
519+
with open(self.testPath, "rb") as source, open(test_file_name, "w+b") as target:
520+
builder = Builder(self.manifestDefinition)
521+
builder.sign(self.signer, "image/jpeg", source, target)
522+
reader = Reader("image/jpeg", target)
523+
json_data = reader.json()
524+
self.assertIn("Python Test", json_data)
525+
self.assertNotIn("validation_status", json_data)
526+
527+
finally:
528+
# Clean up...
529+
530+
if os.path.exists(test_file_name):
531+
os.remove(test_file_name)
532+
533+
# Also clean up the temp directory if it's empty
534+
temp_dir = os.path.dirname(test_file_name)
535+
if os.path.exists(temp_dir) and not os.listdir(temp_dir):
536+
os.rmdir(temp_dir)
537+
509538
def test_streams_sign_with_es256_alg(self):
510539
with open(self.testPath, "rb") as file:
511540
builder = Builder(self.manifestDefinitionV2)
@@ -1168,8 +1197,6 @@ def test_sign_file_tmn_wip(self):
11681197
try:
11691198
# Create a temporary output file path
11701199
output_path = os.path.join(temp_dir, "signed_output.jpg")
1171-
print(f"## output_path: {output_path}")
1172-
print(f"## self.testPath: {self.testPath}")
11731200

11741201
# Use the sign_file method
11751202
builder = Builder(self.manifestDefinition)
@@ -1846,8 +1873,7 @@ def test_sign_file(self):
18461873
manifest_json = json.dumps(manifest)
18471874

18481875
try:
1849-
# Sign the file
1850-
result_json = sign_file(
1876+
sign_file(
18511877
self.testPath,
18521878
output_path,
18531879
manifest_json,

0 commit comments

Comments
 (0)