PhotoCraft: An open-source, photo editor written by scratch in pure Rust
IO: open .psd/.psb/.psdt through the PSD reader, not codecs (#5258)
Vikram Bhaskaran478c98697279
diff --git a/apps/photocraft-web/src/web.rs b/apps/photocraft-web/src/web.rsindex a5c9d8a56..14105cc6c 100644--- a/apps/photocraft-web/src/web.rs+++ b/apps/photocraft-web/src/web.rs@@ -362,7 +362,10 @@ fn services(inbox: Inbox) -> Services {let dialog = if let Some(exts) = extensions {rfd::AsyncFileDialog::new().add_filter("Supported Files", &exts)} else {- rfd::AsyncFileDialog::new().add_filter("All Formats", OPEN_EXTS).add_filter("SVG", SVG_EXTS)+ rfd::AsyncFileDialog::new()+ .add_filter("All Formats", OPEN_EXTS)+ .add_filter("Photoshop", &["psd", "psb", "psdt"])+ .add_filter("SVG", SVG_EXTS)};let answer = if multiple {match dialog.pick_files().await {diff --git a/apps/photocraft/src/services.rs b/apps/photocraft/src/services.rsindex d4a3869f3..e2df19102 100644--- a/apps/photocraft/src/services.rs+++ b/apps/photocraft/src/services.rs@@ -42,6 +42,7 @@ fn open_filters() -> Vec<(&'static str, Vec<String>)> {vec![("All Formats", open_filter_extensions(OPEN_EXTS)),("PhotoCraft", open_filter_extensions(&["pcraft"])),+ ("Photoshop", open_filter_extensions(&["psd", "psb", "psdt"])),("OpenRaster", open_filter_extensions(&["ora"])),("Krita", open_filter_extensions(&["kra", "krz"])),("SVG", open_filter_extensions(SVG_EXTS)),@@ -628,6 +629,9 @@ mod tests {assert!(!globs.iter().any(|glob| matches(glob, "txt")));let svg_filter = open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts).unwrap_or_default();assert_eq!(svg_filter, open_filter_extensions(SVG_EXTS));+ let psd_filter = open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts).unwrap_or_default();+ assert_eq!(psd_filter, open_filter_extensions(&["psd", "psb", "psdt"]));+ assert!(psd_filter.iter().any(|glob| matches(glob, "psd")) && psd_filter.iter().any(|glob| matches(glob, "PSD")));assert!(svg_filter.iter().any(|glob| matches(glob, "svg")) && svg_filter.iter().any(|glob| matches(glob, "svgz")));}@@ -637,6 +641,10 @@ mod tests {assert_eq!(open_filter_extensions(OPEN_EXTS), OPEN_EXTS);assert_eq!(open_filter_extensions(&["pcraft"]), ["pcraft"]);assert_eq!(open_filters().into_iter().find(|(name, _)| *name == "SVG").map(|(_, exts)| exts), Some(vec!["svg".to_string(), "svgz".to_string()]));+ assert_eq!(+ open_filters().into_iter().find(|(name, _)| *name == "Photoshop").map(|(_, exts)| exts),+ Some(vec!["psd".to_string(), "psb".to_string(), "psdt".to_string()])+ );}#[test]diff --git a/crates/io/src/lib.rs b/crates/io/src/lib.rsindex 49e4bb60e..c2c91a10b 100644--- a/crates/io/src/lib.rs+++ b/crates/io/src/lib.rs@@ -230,11 +230,20 @@ impl TgaBits {}}+/// Photoshop document extensions File › Open routes to the PSD reader.+const PSD_EXTS: &[&str] = &["psd", "psb", "psdt"];+/// `true` if `bytes` start with the PSD/PSB signature.pub fn is_psd(bytes: &[u8]) -> bool {bytes.starts_with(b"8BPS")}+/// PSD/PSB/PSDT by magic or by a declared Photoshop extension so File › Open does not+/// send `.psd` through the generic image codecs (`codec: unrecognized image format`, #5258).+fn is_psd_input(name: &str, bytes: &[u8]) -> bool {+ is_psd(bytes) || PSD_EXTS.iter().any(|e| has_extension(name, e))+}+/// Imports a file. PSD/PSB, Affinity containers and camera raws are detected by magic; everything/// else is decoded with `photocraft-codecs`.pub fn import(name: &str, bytes: &[u8]) -> Result<ImportResult, IoError> {@@ -272,7 +281,7 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, mif has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) {return Ok(ImportResult { document: photocraft_format::load_from_bytes(bytes)?, warnings: Vec::new(), source_read_only: false, preview_only: false });}- if is_psd(bytes) {+ if is_psd_input(name, bytes) {let file = PsdFile::from_bytes(bytes)?;// Nesting past the document model's cap could never be saved; reject the file with the// actionable limit.@@ -516,7 +525,7 @@ pub fn merged_composite(file: &PsdFile) -> Result<Vec<[f32; 4]>, IoError> {#[cfg(test)]mod tests {- use super::is_svg_input;+ use super::{import, is_psd_input, is_svg_input};#[test]fn svg_extension_does_not_override_another_format_signature() {@@ -526,4 +535,18 @@ mod tests {assert!(is_svg_input("drawing.svgz", &[0x1f, 0x8b, 0, 0]));assert!(!is_svg_input("drawing.svgz", webp_header));}++ #[test]+ fn photoshop_extension_reaches_the_psd_loader() {+ // A declared .psd/.PSB/.psdt must not fall through to codecs (#5258).+ for name in ["photo.psd", "dir/Photo.PSD", r"C:\w\a.PSB", "card.psdt"] {+ assert!(is_psd_input(name, b"not a signature"), "{name}");+ }+ assert!(!is_psd_input("photo.png", b"not a signature"));+ assert!(is_psd_input("renamed.png", b"8BPS"));+ let err = import("photo.psd", b"not a photoshop file").expect_err("named .psd is a PSD parse, not a codec miss");+ let msg = err.to_string();+ assert!(msg.starts_with("PSD:"), "{msg}");+ assert!(!msg.contains("unrecognized image format"), "{msg}");+ }}