@@ -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
0 commit comments