-
Notifications
You must be signed in to change notification settings - Fork 4
docs: add docs on adding ingredient from archive #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -241,6 +241,125 @@ const ingredient = builder.addIngredientFromReader(sourceReader); | |
| console.log(ingredient.title); // Contains ingredient metadata | ||
| ``` | ||
|
|
||
| #### Adding Ingredients from Archives (.c2pa files) | ||
|
|
||
| You can add ingredients from `.c2pa` archive files. Archives are binary files that contain a manifest store with ingredients and their associated resources (thumbnails, manifest data, etc.). To work with them, read the archive with `Reader` using the `application/c2pa` MIME type, then extract the ingredients and transfer their binary resources to a new `Builder`. | ||
|
|
||
| There are two types of archives sharing the same binary format: | ||
|
|
||
| - **Builder archives** (working store archives): Serialized snapshots of a `Builder`, created by `builder.toArchive()`. They can contain multiple ingredients. | ||
| - **Ingredient archives**: Contain exactly one ingredient from a source asset. They carry the provenance history for reuse in other manifests. | ||
|
|
||
| ##### Reading an archive and adding its ingredients | ||
|
|
||
| ```javascript | ||
| import { Reader, Builder } from '@contentauth/c2pa-node'; | ||
| import * as fs from 'node:fs/promises'; | ||
|
|
||
| // Read the archive using the application/c2pa MIME type | ||
| const archiveBuffer = await fs.readFile('ingredients.c2pa'); | ||
| const reader = await Reader.fromAsset( | ||
| { buffer: archiveBuffer, mimeType: 'application/c2pa' }, | ||
| { verify: { verify_after_reading: false } } | ||
| ); | ||
|
|
||
| // Get the ingredients from the active manifest | ||
| const activeManifest = reader.getActive(); | ||
| const ingredients = activeManifest.ingredients; | ||
|
|
||
| // Create a new builder with the ingredients from the archive | ||
| const builder = Builder.withJson({ | ||
| claim_generator_info: [{ name: 'my-app', version: '1.0.0' }], | ||
| ingredients: ingredients, | ||
| }); | ||
|
|
||
| // Transfer binary resources (thumbnails, manifest_data) for each ingredient | ||
| for (const ingredient of ingredients) { | ||
| if (ingredient.thumbnail) { | ||
| const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null }); | ||
| await builder.addResource(ingredient.thumbnail.identifier, { | ||
| buffer: resource.buffer, | ||
| mimeType: ingredient.thumbnail.format, | ||
| }); | ||
| } | ||
| if (ingredient.manifest_data) { | ||
| const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null }); | ||
| await builder.addResource(ingredient.manifest_data.identifier, { | ||
| buffer: resource.buffer, | ||
| mimeType: 'application/c2pa', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Sign the manifest | ||
| const signer = LocalSigner.newSigner(cert, key, 'es256'); | ||
| builder.sign(signer, inputAsset, outputAsset); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be missing an
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The LocalSigner is synchronous. |
||
| ``` | ||
|
|
||
| ##### Selecting specific ingredients from an archive | ||
|
|
||
| When an archive contains multiple ingredients, you can filter to include only the ones you need: | ||
|
|
||
| ```javascript | ||
| const reader = await Reader.fromAsset( | ||
| { buffer: archiveBuffer, mimeType: 'application/c2pa' }, | ||
| { verify: { verify_after_reading: false } } | ||
| ); | ||
|
|
||
| const activeManifest = reader.getActive(); | ||
| const allIngredients = activeManifest.ingredients; | ||
|
|
||
| // Select only the ingredients you want (e.g., by title or instance_id) | ||
| const selected = allIngredients.filter( | ||
| (ing) => ing.title === 'photo_1.jpg' || ing.instance_id === 'catalog:logo' | ||
| ); | ||
|
|
||
| // Build with only the selected ingredients | ||
| const builder = Builder.withJson({ | ||
| claim_generator_info: [{ name: 'my-app', version: '1.0.0' }], | ||
| ingredients: selected, | ||
| }); | ||
|
|
||
| // Transfer resources only for selected ingredients | ||
| for (const ingredient of selected) { | ||
| if (ingredient.thumbnail) { | ||
| const resource = await reader.resourceToAsset(ingredient.thumbnail.identifier, { buffer: null }); | ||
| await builder.addResource(ingredient.thumbnail.identifier, { | ||
| buffer: resource.buffer, | ||
| mimeType: ingredient.thumbnail.format, | ||
| }); | ||
| } | ||
| if (ingredient.manifest_data) { | ||
| const resource = await reader.resourceToAsset(ingredient.manifest_data.identifier, { buffer: null }); | ||
| await builder.addResource(ingredient.manifest_data.identifier, { | ||
| buffer: resource.buffer, | ||
| mimeType: 'application/c2pa', | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ##### Building an ingredient archive | ||
|
|
||
| To create an ingredient archive, add ingredients to a `Builder` and save it as an archive: | ||
|
|
||
| ```javascript | ||
| const builder = Builder.new(); | ||
|
|
||
| // Add ingredients with stable instance_id for later catalog lookups | ||
| await builder.addIngredient( | ||
| JSON.stringify({ | ||
| title: 'photo-A.jpg', | ||
| relationship: 'componentOf', | ||
| instance_id: 'catalog:photo-A', | ||
| }), | ||
| { path: 'photo-A.jpg' } | ||
| ); | ||
|
|
||
| // Save as a .c2pa archive | ||
| await builder.toArchive({ path: 'ingredient-catalog.c2pa' }); | ||
| ``` | ||
|
|
||
| #### Creating and Reusing Builder Archives | ||
|
|
||
| Builder archives allow you to save a builder's state (including ingredients) and reuse it later: | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So it seems like there's to analogous way to add an ingredient from Blob like in the JS SDK with
addIngredientFromBlob. 🤔 Is that right?If so, that feels like something we should definitely revisit when we look into refactoring and cleaning up these two SDKs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. The problem with Blob is that it isn't supported in older versions of Node. We can probably add it now.