-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Adds Reader.from_manifest_data_and_stream along with set_remote_url and set_no_embed. #43
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,19 @@ impl Reader { | |
Ok(json) | ||
} | ||
|
||
pub fn from_manifest_data_and_stream(&self, manifest_data: &[u8], format: &str, stream: &dyn Stream) -> Result<String> { | ||
// uniffi doesn't allow mutable parameters, so we we use an adapter | ||
let mut stream = StreamAdapter::from(stream); | ||
let reader = c2pa::Reader::from_manifest_data_and_stream(manifest_data, format, &mut stream)?; | ||
let json = reader.to_string(); | ||
if let Ok(mut st) = self.reader.try_write() { | ||
*st = reader; | ||
} else { | ||
return Err(Error::RwLock); | ||
}; | ||
Ok(json) | ||
} | ||
|
||
pub fn json(&self) -> Result<String> { | ||
if let Ok(st) = self.reader.try_read() { | ||
Ok(st.json()) | ||
|
@@ -121,6 +134,25 @@ impl Builder { | |
Ok(()) | ||
} | ||
|
||
/// Set to true to disable embedding a manifest | ||
pub fn set_no_embed(&self) -> Result<()> { | ||
if let Ok(mut builder) = self.builder.try_write() { | ||
builder.set_no_embed(true); | ||
} else { | ||
return Err(Error::RwLock); | ||
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. Question: Is this to poison the lock or... ? 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. This is here because the uniffi tool we use to generate python bindings does not support mutable parameters. So we add an internal check to ensure there is only one mutable access at a time. 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. We could use atomics here in use std::sync::atomic::{AtomicBool, Ordering};
struct Builder {
no_embed: AtomicBool
}
impl Builder {
pub fn set_no_embed(&self, value: bool) {
self.no_embed.store(value, Ordering::Relaxed)
}
}
fn main() {
let b = Builder {
no_embed: AtomicBool::new(true)
};
b.set_no_embed(true);
// note how b, defined above, is not mutable.
std::thread::scope(|s| {
let _ = s.spawn(|| b.set_no_embed(false));
let _ = s.spawn(|| b.set_no_embed(true));
let _ = s.spawn(|| b.set_no_embed(false));
});
} |
||
}; | ||
Ok(()) | ||
} | ||
|
||
pub fn set_remote_url(&self, remote_url: &str) -> Result<()> { | ||
if let Ok(mut builder) = self.builder.try_write() { | ||
builder.set_remote_url(remote_url); | ||
} else { | ||
return Err(Error::RwLock); | ||
}; | ||
Ok(()) | ||
} | ||
|
||
/// Add a resource to the builder | ||
pub fn add_resource(&self, uri: &str, stream: &dyn Stream) -> Result<()> { | ||
if let Ok(mut builder) = self.builder.try_write() { | ||
|
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.
Is there any chance that the manifest_data could be blank (e.g.
or any variation of whatever could be considered blank). Do we need special care around that?
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.
This option is allow verifying a sidecar or remote manifest. If we have retrieved a .c2pa manifest store from our cloud or a sidecar, we can pass it in along with an associated asset to see if it verifies. If the manifest_data is not a correctly structured binary .c2pa we will return errors reporting it.
I should add a ticket somewhere to do more type checking at a higher level, If you pass the wrong type of parameter, it will get caught, but the error messages are obscure ones from the binding layer.