Skip to content

Commit ca21c3f

Browse files
authored
Merge branch 'main' into docs/pkey_caveat
2 parents 3cded9d + ac74feb commit ca21c3f

File tree

4 files changed

+61
-8
lines changed

4 files changed

+61
-8
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "c2pa-python"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
edition = "2021"
55
authors = ["Gavin Peacock <[email protected]"]
66

@@ -11,7 +11,7 @@ crate-type = ["cdylib"]
1111

1212

1313
[dependencies]
14-
c2pa = {version = "0.32.0", features = ["unstable_api", "openssl"]}
14+
c2pa = {version = "0.32.2", features = ["unstable_api", "openssl"]}
1515
pem = "3.0.3"
1616
serde = { version = "1.0.197", features = ["derive"] }
1717
serde_derive = "1.0"

c2pa/c2pa_api/c2pa_api.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import os
1616
import sys
1717
import tempfile
18+
import shutil
1819
PROJECT_PATH = os.getcwd()
1920
SOURCE_PATH = os.path.join(
2021
PROJECT_PATH,"target","python"
@@ -50,7 +51,7 @@ def from_file(cls, path: str, format=None):
5051
def get_manifest(self, label):
5152
manifest_store = json.loads(self.json())
5253
return manifest_store["manifests"].get(label)
53-
return json.loads(self.json())
54+
5455
def get_active_manifest(self):
5556
manifest_store = json.loads(self.json())
5657
active_label = manifest_store.get("active_manifest")
@@ -128,10 +129,27 @@ def sign(self, signer, format, input, output = None):
128129
return super().sign(format, C2paStream(input), C2paStream(output), signer)
129130

130131
def sign_file(self, signer, sourcePath, outputPath):
131-
format = os.path.splitext(outputPath)[1][1:]
132+
extension = os.path.splitext(outputPath)[1][1:]
132133
input = open(sourcePath, "rb")
133-
output = open(outputPath, "wb")
134-
return self.sign(signer, format, input, output)
134+
# Check if outputPath is the same as sourcePath
135+
if outputPath == sourcePath:
136+
# Create a temporary file with the same extension as sourcePath
137+
temp_output = tempfile.NamedTemporaryFile(suffix='.' + extension, delete=False)
138+
temp_output_path = temp_output.name
139+
temp_output.close()
140+
else:
141+
temp_output_path = outputPath
142+
143+
output = open(temp_output_path, "wb")
144+
result = self.sign(signer, extension, input, output)
145+
output.close()
146+
input.close()
147+
148+
# If a temporary file was used, rename or copy it to the outputPath
149+
if outputPath == sourcePath:
150+
shutil.move(temp_output_path, outputPath)
151+
152+
return result
135153

136154

137155
# Implements a C2paStream given a stream handle

tests/test_api.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
import pytest
1515
import tempfile
16+
import shutil
1617

1718
from c2pa import Builder, Error, Reader, SigningAlg, create_signer, sdk_version, sign_ps256, version
1819

@@ -50,7 +51,7 @@ def getitem(d, key):
5051
}
5152

5253
def test_version():
53-
assert version() == "0.5.0"
54+
assert version() == "0.5.1"
5455

5556
def test_sdk_version():
5657
assert "c2pa-rs/" in sdk_version()
@@ -121,6 +122,40 @@ def sign(data: bytes) -> bytes:
121122
assert manifest,["format"] == "image/jpeg"
122123
# There should be no validation status errors
123124
assert manifest.get("validation_status") == None
125+
except Exception as e:
126+
print("Failed to sign manifest store: " + str(e))
127+
exit(1)
128+
129+
# Test signing the same source and destination file
130+
def test_v2_sign_file_same():
131+
data_dir = "tests/fixtures/"
132+
try:
133+
def sign(data: bytes) -> bytes:
134+
return sign_ps256(data, data_dir+"ps256.pem")
135+
136+
certs = open(data_dir + "ps256.pub", "rb").read()
137+
# Create a local signer from a certificate pem file
138+
signer = create_signer(sign, SigningAlg.PS256, certs, "http://timestamp.digicert.com")
139+
140+
builder = Builder(manifest_def)
141+
142+
builder.add_resource_file("A.jpg", data_dir + "A.jpg")
143+
144+
with tempfile.TemporaryDirectory() as output_dir:
145+
path = output_dir + "/A.jpg"
146+
# Copy the file from data_dir to output_dir
147+
shutil.copy(data_dir + "A.jpg", path)
148+
c2pa_data = builder.sign_file(signer, path, path)
149+
assert len(c2pa_data) > 0
150+
151+
reader = Reader.from_file(path)
152+
manifest = reader.get_active_manifest()
153+
154+
# check custom title and format
155+
assert manifest["title"]== "My Title"
156+
assert manifest["format"] == "image/jpeg"
157+
# There should be no validation status errors
158+
assert manifest.get("validation_status") == None
124159
except Exception as e:
125160
print("Failed to sign manifest store: " + str(e))
126161
exit(1)

tests/unit_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
class TestC2paSdk(unittest.TestCase):
2727

2828
def test_version(self):
29-
self.assertIn("0.5.0", sdk_version())
29+
self.assertIn("0.5.1", sdk_version())
3030

3131

3232
class TestReader(unittest.TestCase):

0 commit comments

Comments
 (0)