Skip to content

Commit 7a4a421

Browse files
authored
feat: Add C binding for setting the base path (#1237)
* feat: Add set_resource_dir and binding, add_resource only writes to memory * test: Add test for reading from ingredient folder using resource_dir * refactor: Rename back to base path and remove breaking changes * docs: Update documentation and warning * chore: Fix test name * fix: Make test consistent with others
1 parent d26972c commit 7a4a421

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

c2pa_c_ffi/src/c_api.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,31 @@ pub unsafe extern "C" fn c2pa_builder_set_remote_url(
797797
0 as c_int
798798
}
799799

800+
/// ⚠️ **Deprecated Soon**
801+
/// This method is planned to be deprecated in a future release.
802+
/// Usage should be limited and temporary.
803+
///
804+
/// Sets the resource directory on the Builder.
805+
/// When set, resources that are not found in memory will be searched for in the given directory.
806+
/// # Parameters
807+
/// * builder_ptr: pointer to a Builder.
808+
/// * base_path: pointer to a C string with the resource directory.
809+
/// # Errors
810+
/// Returns -1 if there were errors, otherwise returns 0.
811+
/// The error string can be retrieved by calling c2pa_error.
812+
/// # Safety
813+
/// Reads from NULL-terminated C strings.
814+
#[no_mangle]
815+
pub unsafe extern "C" fn c2pa_builder_set_base_path(
816+
builder_ptr: *mut C2paBuilder,
817+
base_path: *const c_char,
818+
) -> c_int {
819+
let mut builder = guard_boxed_int!(builder_ptr);
820+
let base_path = from_cstr_or_return_int!(base_path);
821+
builder.set_base_path(&base_path);
822+
0 as c_int
823+
}
824+
800825
/// Adds a resource to the C2paBuilder.
801826
///
802827
/// The resource uri should match an identifier in the manifest definition.

sdk/src/builder.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,22 @@ impl Builder {
293293
self
294294
}
295295

296+
/// ⚠️ **Deprecated Soon**
297+
/// This method is planned to be deprecated in a future release.
298+
/// Usage should be limited and temporary.
299+
///
300+
/// Sets the resource directory for this [`Builder`]
301+
///
302+
/// # Arguments
303+
/// * `base_path` - The directory to search in to find the resources.
304+
/// # Returns
305+
/// * A mutable reference to the [`Builder`].
306+
#[cfg(feature = "file_io")]
307+
pub fn set_base_path<P: Into<PathBuf>>(&mut self, base_path: P) -> &mut Self {
308+
self.base_path = Some(base_path.into());
309+
self
310+
}
311+
296312
/// Sets the remote_url for this [`Builder`].
297313
/// The URL must return the manifest data and is injected into the destination asset when signing.
298314
/// For remote-only manifests, set the `no_embed` flag to `true`.
@@ -1214,6 +1230,8 @@ mod tests {
12141230
use wasm_bindgen_test::*;
12151231

12161232
use super::*;
1233+
#[cfg(feature = "file_io")]
1234+
use crate::utils::test::fixture_path;
12171235
use crate::{
12181236
assertions::{c2pa_action, BoxHash},
12191237
asset_handlers::jpeg_io::JpegIO,
@@ -2230,4 +2248,32 @@ mod tests {
22302248
assert!(reader_json.contains("Test Ingredient"));
22312249
assert!(reader_json.contains("thumbnail.ingredient"));
22322250
}
2251+
2252+
#[cfg(feature = "file_io")]
2253+
#[test]
2254+
fn test_base_path() {
2255+
let mut builder = Builder::new();
2256+
let ingredient_folder = fixture_path("ingredient");
2257+
builder.set_base_path(&ingredient_folder);
2258+
assert_eq!(builder.base_path.as_ref(), Some(&ingredient_folder));
2259+
let ingredient_json =
2260+
std::fs::read_to_string(ingredient_folder.join("ingredient.json")).unwrap();
2261+
2262+
let ingredient = Ingredient::from_json(&ingredient_json).unwrap();
2263+
builder.add_ingredient(ingredient);
2264+
2265+
let signer = test_signer(SigningAlg::Ps256);
2266+
2267+
let mut source = Cursor::new(TEST_IMAGE_CLEAN);
2268+
let mut dest = Cursor::new(Vec::new());
2269+
2270+
builder
2271+
.sign(&signer, "image/jpeg", &mut source, &mut dest)
2272+
.unwrap();
2273+
2274+
let reader = Reader::from_stream("jpeg", &mut dest).unwrap();
2275+
let active_manifest = reader.active_manifest().unwrap();
2276+
let ingredient = active_manifest.ingredients().first().unwrap();
2277+
assert_eq!(ingredient.title(), Some("C.jpg"));
2278+
}
22332279
}
27.8 KB
Loading
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"title": "C.jpg",
3+
"format": "image/jpeg",
4+
"instance_id": "xmp:iid:547f138e-d98d-45b5-a13c-8fd092b4ebc6",
5+
"thumbnail": {
6+
"format": "image/jpeg",
7+
"identifier": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/c2pa.thumbnail.claim.jpeg",
8+
"hash": "Nrhmnt6qXlHbrpaDmEMb98m3TyN3PJwniriapw7QM4k="
9+
},
10+
"relationship": "componentOf",
11+
"active_manifest": "contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3",
12+
"validation_results": {
13+
"activeManifest": {
14+
"success": [
15+
{
16+
"code": "claimSignature.insideValidity",
17+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.signature",
18+
"explanation": "claim signature valid"
19+
},
20+
{
21+
"code": "claimSignature.validated",
22+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.signature",
23+
"explanation": "claim signature valid"
24+
},
25+
{
26+
"code": "assertion.hashedURI.match",
27+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/c2pa.thumbnail.claim.jpeg",
28+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.thumbnail.claim.jpeg"
29+
},
30+
{
31+
"code": "assertion.hashedURI.match",
32+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/stds.schema-org.CreativeWork",
33+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/stds.schema-org.CreativeWork"
34+
},
35+
{
36+
"code": "assertion.hashedURI.match",
37+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/c2pa.actions",
38+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.actions"
39+
},
40+
{
41+
"code": "assertion.hashedURI.match",
42+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/c2pa.hash.data",
43+
"explanation": "hashed uri matched: self#jumbf=c2pa.assertions/c2pa.hash.data"
44+
},
45+
{
46+
"code": "assertion.dataHash.match",
47+
"url": "self#jumbf=/c2pa/contentauth:urn:uuid:b2b1f7fa-b119-4de1-9c0d-c97fbea3f2c3/c2pa.assertions/c2pa.hash.data",
48+
"explanation": "data hash valid"
49+
}
50+
],
51+
"informational": [],
52+
"failure": []
53+
}
54+
},
55+
"manifest_data": {
56+
"format": "application/c2pa",
57+
"identifier": "manifest_data.c2pa"
58+
}
59+
}
44.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)