Skip to content

refactor: Move uri_to_path into utils #1186

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions sdk/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::{
settings::get_settings_value,
status_tracker::StatusTracker,
store::Store,
utils::io_utils::uri_to_path,
validation_results::{ValidationResults, ValidationState},
validation_status::ValidationStatus,
Manifest, ManifestAssertion,
Expand Down Expand Up @@ -586,22 +587,6 @@ impl Reader {
.map(|size| size as usize)
}

/// Convert a URI to a file path. (todo: move this to utils)
fn uri_to_path(uri: &str, manifest_label: &str) -> String {
let mut path = uri.to_string();
if path.starts_with("self#jumbf=") {
// convert to a file path always including the manifest label
path = path.replace("self#jumbf=", "");
if path.starts_with("/c2pa/") {
path = path.replacen("/c2pa/", "", 1);
} else {
path = format!("{}/{path}", manifest_label);
}
path = path.replace([':'], "_");
}
path
}

/// Write all resources to a folder.
///
///
Expand All @@ -626,7 +611,7 @@ impl Reader {
for manifest in self.manifests.values() {
let resources = manifest.resources();
for (uri, data) in resources.resources() {
let id_path = Self::uri_to_path(uri, manifest.label().unwrap_or("unknown"));
let id_path = uri_to_path(uri, manifest.label().unwrap_or("unknown"));
let path = path.as_ref().join(id_path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
Expand Down
16 changes: 16 additions & 0 deletions sdk/src/utils/io_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,22 @@ pub fn wasm_remove_dir_all<P: AsRef<std::path::Path>>(path: P) -> Result<()> {
))?
}

/// Convert a URI to a file path.
pub fn uri_to_path(uri: &str, manifest_label: &str) -> String {
let mut path = uri.to_string();
if path.starts_with("self#jumbf=") {
// convert to a file path always including the manifest label
path = path.replace("self#jumbf=", "");
if path.starts_with("/c2pa/") {
path = path.replacen("/c2pa/", "", 1);
} else {
path = format!("{}/{path}", manifest_label);
}
path = path.replace([':'], "_");
}
path
}

#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
Expand Down