Reading manifest data
- JavaScript
- Python
- Node.js
- C++
- Rust
Once you've used createC2pa to create an instance of c2pa-web (for example in c2pa in this example), use c2pa.reader.fromBlob() to create a Reader for an asset.
Then use Reader's manifestStore() method to read manifest data (if any) from the asset.
For example:
import { createC2pa } from '@contentauth/c2pa-web';
import wasmSrc from '@contentauth/c2pa-web/resources/c2pa.wasm?url';
const c2pa = createC2pa({ wasmSrc });
const response = await fetch(
'https://contentauth.github.io/example-assets/images/Firefly_tabby_cat.jpg'
);
const blob = await response.blob();
const reader = await c2pa.reader.fromBlob(blob.type, blob);
const manifestStore = await reader.manifestStore();
console.log(manifestStore);
// Free SDK objects when they are no longer needed to avoid memory leaks.
await reader.free();
Use the Reader object to read manifest data from a file or stream and perform validation on the manifest store.
Use the json() method to return a JSON manifest report; If there are validation errors, the report includes a validation_status field.
An asset file may contain many manifests in a manifest store. The most recent manifest is identified by the value of the active_manifest field in the manifests map.
# Import the C2PA Python package.
from c2pa import *
# Import standard general-purpose packages.
import os
import io
import logging
import json
try:
# Create a reader from a file path.
reader = c2pa.Reader.from_file("path/to/media_file.jpg")
# Print the JSON for a manifest.
print("manifest store:", reader.json())
except Exception as err:
print(err)
The Node.js library is being revised. The documentation will be updated as soon as possible with the latest changes.
Use the read_file function to read C2PA data from the specified file. This function examines the specified asset file for C2PA data and returns a JSON report if it finds any; it throws exceptions on errors. If there are validation errors, the report includes a validation_status field.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdexcept>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include "c2pa.hpp"
#include "test_signer.hpp"
using namespace std;
namespace fs = std::filesystem;
using namespace c2pa;
auto json_store = C2pa::read_file("work/media_file.jpg", "output/data_dir")
Where:
work/media_file.jpgis the asset file to read.output/data_diris the optional path to data output directory; If provided, the function extracts any binary resources, such as thumbnails, icons, and C2PA data into that directory. These files are referenced by the identifier fields in the manifest store report.
Use the Reader struct to read manifest data from a file or stream.
Reading from a file
Use from_file to read manifest data from a file:
use std::{
io::{Cursor, Write},
process::{Command, Stdio},
};
use anyhow::Result;
use c2pa::{settings::load_settings_from_str, Builder, CallbackSigner, Reader};
use c2pa_crypto::raw_signature::SigningAlg;
use serde_json::json;
const TEST_IMAGE: &[u8] = include_bytes!("../tests/fixtures/CA.jpg");
const CERTS: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pub");
const PRIVATE_KEY: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pem");
use c2pa::Reader;
let reader = Reader::from_file("path/to/file.jpg").unwrap();
There is also an asynchronous version of this method, from_stream_async.
Reading from a stream
Use from_stream to read manifest data from a stream:
use std::{
io::{Cursor, Write},
process::{Command, Stdio},
};
use anyhow::Result;
use c2pa::{settings::load_settings_from_str, Builder, CallbackSigner, Reader};
use c2pa_crypto::raw_signature::SigningAlg;
use serde_json::json;
const TEST_IMAGE: &[u8] = include_bytes!("../tests/fixtures/CA.jpg");
const CERTS: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pub");
const PRIVATE_KEY: &[u8] = include_bytes!("../tests/fixtures/certs/ed25519.pem");
let mut stream = Cursor::new(include_bytes!("../tests/fixtures/CA.jpg"));
let reader = Reader::from_stream("image/jpeg", stream).unwrap();
println!("{}", reader.json());
There is also an asynchronous version of this method, from_stream_async.