v2show.rs (2455B)
1 // Copyright 2024 Adobe. All rights reserved. 2 // This file is licensed to you under the Apache License, 3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) 4 // or the MIT license (http://opensource.org/licenses/MIT), 5 // at your option. 6 7 // Unless required by applicable law or agreed to in writing, 8 // this software is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or 10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the 11 // specific language governing permissions and limitations under 12 // each license. 13 14 //! Example App that generates a manifest store listing for a given file 15 16 use anyhow::Result; 17 #[cfg(target_arch = "wasm32")] 18 fn main() -> Result<()> { 19 Ok(()) 20 } 21 22 #[cfg(not(target_arch = "wasm32"))] 23 fn main() -> Result<()> { 24 use std::io::Read; 25 26 use c2pa::{format_from_path, Error, Reader}; 27 28 let args: Vec<String> = std::env::args().collect(); 29 if args.len() > 1 { 30 let path = std::path::PathBuf::from(&args[1]); 31 let format = format_from_path(&path).ok_or(Error::UnsupportedType)?; 32 let mut file = std::fs::File::open(&path)?; 33 34 let reader = match Reader::from_stream(&format, &mut file) { 35 Ok(reader) => Ok(reader), 36 Err(Error::RemoteManifestUrl(url)) => { 37 println!("Fetching remote manifest from {}", url); 38 let mut c2pa_data = Vec::new(); 39 let resp = ureq::get(&url).call()?; 40 resp.into_reader().read_to_end(&mut c2pa_data)?; 41 Reader::from_manifest_data_and_stream(&c2pa_data, &format, &mut file) 42 } 43 Err(Error::JumbfNotFound) => { 44 // if not embedded or cloud, check for sidecar first and load if it exists 45 let potential_sidecar_path = path.with_extension("c2pa"); 46 if potential_sidecar_path.exists() { 47 let manifest_data = std::fs::read(potential_sidecar_path)?; 48 Ok(Reader::from_manifest_data_and_stream( 49 &manifest_data, 50 &format, 51 &mut file, 52 )?) 53 } else { 54 Err(Error::JumbfNotFound) 55 } 56 } 57 Err(e) => Err(e), 58 }?; 59 println!("{reader}"); 60 } else { 61 println!("Prints a manifest report (requires a file path argument)") 62 } 63 Ok(()) 64 }