Skip to content

Commit b63f79a

Browse files
committed
fix: Update sign_file
1 parent e72e4c4 commit b63f79a

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/c2pa/c2pa.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ def sign_file(
671671
# Create a builder from the manifest
672672
builder = Builder(manifest)
673673

674-
# Open source and destination files
675-
with open(source_path, 'rb') as source_file, open(dest_path, 'wb') as dest_file:
674+
# Open source file and create BytesIO for destination
675+
with open(source_path, 'rb') as source_file:
676676
# Get the MIME type from the file extension
677677
mime_type = mimetypes.guess_type(str(source_path))[0]
678678
if not mime_type:
@@ -682,23 +682,34 @@ def sign_file(
682682
if return_manifest_as_bytes:
683683
# Convert Python streams to Stream objects for internal signing
684684
source_stream = Stream(source_file)
685-
dest_stream = Stream(dest_file)
685+
dest_stream = Stream(io.BytesIO(bytearray()))
686686

687687
# Use the builder's internal signing logic to get manifest
688688
# bytes
689689
manifest_bytes = builder._sign_internal(
690690
signer, mime_type, source_stream, dest_stream)
691691

692+
# Write the signed content from BytesIO to the destination file
693+
dest_stream._file.seek(0) # Reset to beginning of stream
694+
with open(dest_path, 'wb') as dest_file:
695+
dest_file.write(dest_stream._file.getvalue())
696+
692697
return manifest_bytes
693698
else:
694-
# Sign the file using the builder
699+
# Sign the file using the builder with BytesIO destination
700+
dest_stream = io.BytesIO(bytearray())
695701
builder.sign(
696702
signer=signer,
697703
format=mime_type,
698704
source=source_file,
699-
dest=dest_file
705+
dest=dest_stream
700706
)
701707

708+
# Write the signed content from BytesIO to the destination file
709+
dest_stream.seek(0) # Reset to beginning of stream
710+
with open(dest_path, 'wb') as dest_file:
711+
dest_file.write(dest_stream.getvalue())
712+
702713
# Read the signed manifest from the destination file
703714
with Reader(dest_path) as reader:
704715
return reader.json()

tests/test_unit_tests.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,11 +1320,6 @@ def error_callback_signer(data: bytes) -> bytes:
13201320
signer=signer
13211321
)
13221322

1323-
# Verify the output file stays empty,
1324-
# as no data should have been written
1325-
self.assertTrue(os.path.exists(output_path))
1326-
self.assertEqual(os.path.getsize(output_path), 0)
1327-
13281323
finally:
13291324
shutil.rmtree(temp_dir)
13301325

0 commit comments

Comments
 (0)