Merge PR #5436: IO: open .psd through the PSD reader, not codecs (#5258)

This commit is contained in:
aiongg committed 2026-10-21 21:56:21 +00:00
commit 7b81efddcb
2 files changed
+77 -16

No files matched your search

+7
View File
@@ -831,6 +831,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")));
}
@@ -860,6 +863,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]
+70 -16
View File
@@ -272,11 +272,21 @@ 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 when no other raster
/// signature is present. A PNG/JPEG/TIFF downloaded as `.psd` still opens from its bytes
/// (#5258 review).
fn is_psd_input(name: &str, bytes: &[u8]) -> bool {
is_psd(bytes) || (PSD_EXTS.iter().any(|e| has_extension(name, e)) && photocraft_codecs::detect(bytes).is_none())
}
/// 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> {
@@ -309,28 +319,34 @@ pub fn import_with_svg_group_depth_and_interrupt(
Ok(r)
}
fn import_psd(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt) -> Result<ImportResult, IoError> {
// A merged image the import reads is decoded with the parse: a ZIP one is inflated once.
let (file, merged) = PsdFile::from_bytes_and_merged_recovering(
bytes,
|f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f),
psd_import::can_recover_layers,
)?;
// Nesting past the document model's cap could never be saved; reject the file with the
// actionable limit.
psd_import::check_group_depth(&file)?;
ctl.check().map_err(|_| IoError::Cancelled)?;
ctl.progress(0.05);
let (mut document, warnings) = psd_import::psd_to_document_decoded(&file, ctl, merged).ok_or(IoError::Cancelled)?;
document.name = name.to_string();
text_import::prepare(&mut document);
Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false })
}
fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, max_group_depth: usize) -> Result<ImportResult, IoError> {
// A declared native extension must reach its loader so malformed bundles retain format errors.
if has_extension(name, photocraft_format::EXTENSION) || photocraft_format::is_pcraft(bytes) {
let document = photocraft_format::load_from_bytes(bytes)?;
return Ok(ImportResult { warnings: open_warnings(&document), document, source_read_only: false, preview_only: false });
}
// Magic first: an Affinity file renamed .psd is still Affinity (#5258 corpus
// preview_never_saves_back_to_its_native_or_renamed_source).
if is_psd(bytes) {
// A merged image the import reads is decoded with the parse: a ZIP one is inflated once.
let (file, merged) = PsdFile::from_bytes_and_merged_recovering(
bytes,
|f| psd_import::check_group_depth(f).is_ok() && psd_import::merged_needed(f),
psd_import::can_recover_layers,
)?;
// Nesting past the document model's cap could never be saved; reject the file with the
// actionable limit.
psd_import::check_group_depth(&file)?;
ctl.check().map_err(|_| IoError::Cancelled)?;
ctl.progress(0.05);
let (mut document, warnings) = psd_import::psd_to_document_decoded(&file, ctl, merged).ok_or(IoError::Cancelled)?;
document.name = name.to_string();
text_import::prepare(&mut document);
return Ok(ImportResult { document, warnings, source_read_only: false, preview_only: false });
return import_psd(name, bytes, ctl);
}
if affinity::is_affinity(bytes) || affinity::has_extension(name) {
return affinity::import(name, bytes);
@@ -352,6 +368,11 @@ fn import_stages(name: &str, bytes: &[u8], ctl: &photocraft_raster::Interrupt, m
if is_svg_input(name, bytes) {
return svg::import_svg_with_group_depth(name, bytes, max_group_depth);
}
// A `.psd`/`.psb`/`.psdt` with no raster signature keeps the PSD error instead of
// `codec: unrecognized image format` (#5258). Magic already returned above.
if is_psd_input(name, bytes) {
return import_psd(name, bytes, ctl);
}
flat::import_flat(name, bytes)
}
@@ -576,7 +597,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() {
@@ -586,4 +607,37 @@ 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}");
// A renamed Affinity file keeps the Affinity loader (magic before the .psd suffix).
assert!(!super::is_psd(b"\0\xffKA"));
}
/// A common mislabeled download: PNG/JPEG bytes named `.psd` open from the signature,
/// as on main and in Photoshop.
#[test]
fn raster_bytes_named_psd_still_open() {
use photocraft_codecs::{ChannelLayout, EncodeOptions, Format, Image};
let n = 8 * 8 * 3;
let img = Image::from_u8(8, 8, ChannelLayout::Rgb, vec![180; n]).unwrap();
let png = photocraft_codecs::encode(&img, Format::Png, &EncodeOptions::default()).unwrap();
let jpeg = photocraft_codecs::encode(&img, Format::Jpeg, &EncodeOptions::default()).unwrap();
for (name, bytes) in [("download.psd", png.as_slice()), ("scan.PSB", jpeg.as_slice())] {
assert!(!is_psd_input(name, bytes), "{name}");
let d = import(name, bytes).unwrap_or_else(|e| panic!("{name}: {e}")).document;
assert_eq!(d.bounds().width(), 8, "{name}");
assert_eq!(d.bounds().height(), 8, "{name}");
}
}
}