Files
photocraft/crates/format/src/lib.rs
T
.CooCoo 289dd0a874 Format: take the .pcraft envelope from craft-pcraft-format (#10724)
* Format: take the .pcraft envelope from craft-pcraft-format

The manifest envelope, TileRef, Hash, FORMAT_VERSION, EXTENSION, the
entry names, tile_path and blob_path, is_valid_hash, the manifest
nesting and item checks and the migration runner now come from
craft-libs' craft-pcraft-format. photocraft-format re-exports them, so
callers do not change. Manifest is craft_pcraft_format::Manifest<DocM>.

Saved bundles keep the same bytes. A save still checks only the nesting
and the item count of its manifest, not its size, and the manifest
nesting limit is still computed from photocraft_doc::MAX_GROUP_DEPTH.

Tests: a save, load and save again gives the same bytes in every color
mode and depth, with and without previews; MAX_GROUP_DEPTH equals
craft_pcraft_format::MAX_GROUP_DEPTH.

* Format: pin craft-pcraft-format from storytold/craft-libs

* Format: read manifests with craft-pcraft-format's read_manifest_with

Pins craft-pcraft-format to dca870a0, the head of craft-libs #407.
Loading reads the manifest with read_manifest_with, which runs the
limit check, the version check, the migration steps and the envelope
read in one path; the layers are still decoded one at a time after the
rest of the document. A save builds the manifest with Manifest::new and
tile references with TileRef::new, and the bundle's directory names and
the .zst suffix come from the crate's constants. Saved bytes and error
messages are unchanged.

* Format: pin craft-pcraft-format to craft-libs #407's head 04dd1bd

04dd1bd changes only the crate's docs and a test, so photocraft-format
builds the same code.

* Format: pin craft-pcraft-format to craft-libs main at b8f7d93

craft-libs #407 merged as b8f7d93. Its read_manifest also refuses a
thumbnail other than thumb.png and a composite other than
composite/preview.png, the only names photocraft-format writes.
2026-11-02 11:43:46 -05:00

215 lines
8.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! # photocraft-format
//!
//! The native, lossless `.pcraft` document bundle (architecture §9).
//!
//! ```text
//! manifest.json versioned document tree (see [`manifest`])
//! tiles/<blake3>.zst content-addressed 256×256 tiles (zstd, little-endian samples)
//! blobs/<blake3>.zst content-addressed binary data (ICC, EXIF, PSD blocks, smart objects)
//! thumb.png optional thumbnail (caller-rendered)
//! composite/preview.png optional flattened preview (caller-rendered)
//! save.lock directory bundles only: held while a save runs (empty)
//! ```
//!
//! A bundle is either a ZIP archive (STORE entries) or a directory with the
//! same layout. Saving is incremental: [`PcraftWriter`] remembers what it
//! already compressed/wrote, so only new tiles are encoded. Directory saves
//! verify existing content-addressed objects once per writer and folder,
//! rechecking files whose size or modification time changes; they replace
//! damaged or missing objects and garbage-collect unreferenced ones. A directory
//! save holds an OS file lock on `save.lock` throughout, so two writers (or two
//! processes) saving one bundle take turns instead of removing each other's objects.
//!
//! This crate sits at L3 next to the compositor, so it does not render.
//! Callers pass previews in [`SaveOptions`]; `photocraft-io` does that.
#![forbid(unsafe_code)]
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
pub mod atomic;
pub mod autosave;
mod convert;
mod finite;
pub mod manifest;
mod migrate;
pub mod read;
mod store;
pub mod zip;
use std::path::Path;
use photocraft_doc::Document;
use photocraft_raster::Rgba8Image;
pub use atomic::{atomic_write, atomic_write_progress};
#[cfg(not(target_arch = "wasm32"))]
pub use autosave::RecoveryStore;
pub use autosave::{Autosaver, RecoveryEntry, discard_recovery, list_recovery, recover};
pub use convert::MAX_GROUP_DEPTH;
pub use manifest::{FORMAT_VERSION, Manifest};
pub use read::read_file;
pub use store::{ByteSource, DEFAULT_REVERIFY_BUDGET, PcraftWriter, SaveStats, read_entry};
/// File extension of the native format.
pub use craft_pcraft_format::EXTENSION;
#[derive(Debug, thiserror::Error)]
pub enum FormatError {
#[error("corrupt .pcraft bundle: {0}")]
Corrupt(String),
#[error("unsupported .pcraft feature: {0}")]
Unsupported(String),
#[error("bundle written by a newer version (format {found}, this build reads up to {supported})")]
TooNew { found: u32, supported: u32 },
#[error("limit exceeded: {0}")]
LimitExceeded(String),
#[error("`{path}` is NaN or infinite, which a .pcraft manifest cannot hold; reset that value and save again")]
NonFinite { path: String },
#[error("I/O: {0}")]
Io(#[from] std::io::Error),
#[error("manifest JSON: {0}")]
Json(#[from] serde_json::Error),
}
impl FormatError {
pub(crate) fn corrupt(msg: impl Into<String>) -> Self {
FormatError::Corrupt(msg.into())
}
}
impl From<craft_pcraft_format::Error> for FormatError {
fn from(e: craft_pcraft_format::Error) -> Self {
use craft_pcraft_format::Error;
match e {
Error::Corrupt(m) => FormatError::Corrupt(m),
Error::Unsupported(m) => FormatError::Unsupported(m),
Error::TooNew { found, supported, .. } => FormatError::TooNew { found, supported },
Error::LimitExceeded(m) => FormatError::LimitExceeded(m),
Error::Json(e) => FormatError::Json(e),
other => FormatError::Corrupt(other.to_string()),
}
}
}
pub type Result<T> = std::result::Result<T, FormatError>;
/// Previews to embed. The format crate cannot render (it sits beside the
/// compositor), so callers provide them.
#[derive(Debug, Clone, Default)]
pub struct SaveOptions {
pub thumbnail: Option<Rgba8Image>,
pub composite: Option<Rgba8Image>,
}
/// Limits applied while loading untrusted bundles.
#[derive(Debug, Clone, Copy)]
pub struct LoadOptions {
/// Maximum size of `manifest.json`.
pub max_manifest_bytes: usize,
/// Maximum size of one decompressed blob.
pub max_blob_bytes: usize,
/// Maximum total decompressed tile + blob bytes.
pub max_total_bytes: u64,
/// Keep document and layer ids from the file (the id counter is advanced
/// past them). When `false`, fresh ids are assigned.
pub preserve_ids: bool,
/// Longest canvas side the manifest may declare (Photoshop's PSB limit, as for PSD and
/// `file.new`).
pub max_canvas_side: u32,
/// Most canvas pixels the manifest may declare. A canvas is a few bytes of JSON, but the
/// compositor, thumbnails and display textures work in proportion to its area, so a caller
/// handling untrusted files (a previewer, a batch job) may lower it. The default admits every
/// canvas the app lets a user create.
pub max_canvas_pixels: u64,
}
/// Default [`LoadOptions::max_canvas_side`].
pub const MAX_CANVAS_SIDE: u32 = 300_000;
/// Default [`LoadOptions::max_canvas_pixels`]: the full 300000 × 300000 PSB canvas. `file.new`,
/// Canvas Size, Image Size (Resample off) and PSB import all reach it, and a document that can be
/// created and saved must reopen. Loading itself stays proportional to the file: layers, masks
/// and channels are sparse tiles read from the bundle under the byte budgets, so a huge declared
/// canvas with no content allocates nothing for its area.
pub const MAX_CANVAS_PIXELS: u64 = MAX_CANVAS_SIDE as u64 * MAX_CANVAS_SIDE as u64;
impl Default for LoadOptions {
fn default() -> Self {
let limits = craft_pcraft_format::Limits::default();
LoadOptions {
max_manifest_bytes: limits.max_manifest_bytes,
max_blob_bytes: limits.max_blob_bytes,
max_total_bytes: limits.max_total_bytes,
preserve_ids: true,
max_canvas_side: MAX_CANVAS_SIDE,
max_canvas_pixels: MAX_CANVAS_PIXELS,
}
}
}
/// `true` when ZIP bytes contain a `manifest.json` entry.
///
/// Bundles written here place the manifest first; re-zipped bundles may reorder entries.
pub fn is_pcraft(bytes: &[u8]) -> bool {
if !bytes.starts_with(b"PK\x03\x04") {
return false;
}
if bytes.len() >= 30 {
let name_len = u16::from_le_bytes([bytes[26], bytes[27]]) as usize;
if bytes.get(30..30 + name_len) == Some(b"manifest.json") {
return true;
}
}
zip::ZipReader::new(bytes).is_ok_and(|archive| archive.find("manifest.json").is_some())
}
/// Save to an in-memory ZIP bundle (one-shot; use [`PcraftWriter`] for
/// incremental saves).
pub fn save_to_bytes(doc: &Document, opts: &SaveOptions) -> Result<Vec<u8>> {
PcraftWriter::new().save_zip(doc, opts).map(|(b, _)| b)
}
/// Load a ZIP bundle from memory.
pub fn load_from_bytes(bytes: &[u8]) -> Result<Document> {
load_from_bytes_with(bytes, &LoadOptions::default())
}
pub fn load_from_bytes_with(bytes: &[u8], opts: &LoadOptions) -> Result<Document> {
let src = store::ZipSource::new(bytes)?;
store::load(&src, opts)
}
/// Load a bundle from a path: a directory bundle or a ZIP file.
pub fn load_path(path: &Path) -> Result<Document> {
load_path_with(path, &LoadOptions::default())
}
/// Load from any byte source — a directory behind a capability handle, an archive, a test
/// fixture — the same migration and limits as the path loads.
pub fn load_bytes_source(src: &dyn ByteSource, opts: &LoadOptions) -> Result<Document> {
store::load(src, opts)
}
pub fn load_path_with(path: &Path, opts: &LoadOptions) -> Result<Document> {
if path.is_dir() {
store::load(&store::DirSource { root: path.to_path_buf() }, opts)
} else {
let bytes = read::read_file(path)?;
load_from_bytes_with(&bytes, opts)
}
}
/// Read and migrate only the manifest (fast: no tiles are decoded).
pub fn read_manifest(bytes: &[u8]) -> Result<Manifest> {
let src = store::ZipSource::new(bytes)?;
store::read_manifest(&src, &LoadOptions::default())
}
/// The embedded thumbnail, if present (PNG bytes).
pub fn read_thumbnail(bytes: &[u8]) -> Result<Option<Vec<u8>>> {
let src = store::ZipSource::new(bytes)?;
let m = store::read_manifest(&src, &LoadOptions::default())?;
match m.thumbnail {
Some(p) => Ok(Some(src.get(&p, 64 << 20)?)),
None => Ok(None),
}
}