//! A copy of a Photos original leaves this device once the object holds it //! (KEEP 1e, review H2; slice 2's eviction brought forward). //! //! Albums' Keep copies an original out of the phone's Photos library and //! queues it like any capture. Kept for good, a 10 GB album would leave 10 GB //! in Cotton's own store beside the 10 GB Photos keeps, and on a phone that //! optimises its storage it would pull the iCloud originals down first. So a //! file queued with `source = 'photos'` is RELEASED here once the object has //! confirmed it: the original's local copy goes, its renditions stay (they are //! what a grid draws), and the row stays with no path, so a detail view fetches //! the original from the object as it fetches any picture not on this phone. //! //! The fence is the budget's and stricter ([`storage::evict_one`]): only a //! `synced` row goes, so the object holds the bytes, and Photos holds them too. //! A file another row shares (the content-addressed store keeps one file for //! the same bytes under every key) stays while any of those rows is a capture //! this device made, pinned, or not yet confirmed. Nothing changes for a photo //! taken in Cotton: it has no `source` and is `captured_here`. use cotton_store::{driver::Driver, mint::Held, outbox::IdSource}; use super::evict::MediaEvictDeps; use super::storage::evict_one; use crate::layout::Layout; /// The `source` of a file whose original the phone's Photos library keeps. pub const PHOTOS_SOURCE: &str = "photos"; /// What one release did. #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct Released { /// Originals whose local copy left this device. pub files: usize, pub bytes: u64, } /// The keys whose local original may go now, oldest first. pub fn releasable(db: &Driver) -> cotton_store::error::Result> { Ok(db .all( "SELECT m.key FROM _media m WHERE m.source = 'photos' AND m.captured_here = 0 AND m.pinned = 0 AND m.tier = 'original' AND m.state = 'synced' AND m.local_path IS NOT NULL AND NOT EXISTS (SELECT 1 FROM _media o WHERE o.local_path = m.local_path AND o.key <> m.key AND (o.captured_here = 1 OR o.pinned = 1 OR o.state <> 'synced')) ORDER BY m.updated_at, m.key", &[], )? .iter() .filter_map(|row| row.text("key").map(str::to_string)) .collect()) } /// Release every copy of a Photos original the object has confirmed. Called /// under the file's writer lock, as `evict_one` is; a file that would not go /// is left for the next tick and named in the log, never retried in a loop. #[allow(clippy::too_many_arguments)] pub fn release_photos_copies( db: &Driver, workspace: &str, device: &str, layout: &Layout, deps: &dyn MediaEvictDeps, now_ms: i64, ids: &mut dyn IdSource, held: Held, ) -> Result { let mut released = Released::default(); for key in releasable(db).map_err(|error| error.to_string())? { match evict_one(db, workspace, device, layout, deps, &key, now_ms, ids, held) { Ok(bytes) => { released.files += 1; released.bytes += bytes; } Err(error) => eprintln!("[cotton-media] a Photos copy stays for now: {error}"), } } Ok(released) } #[cfg(test)] #[path = "release_tests.rs"] mod tests;