1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
use crate::album::Album; use crate::album_version::AlbumVersion; use crate::master::Master; use rusqlite::Result; use rusqlite::NO_PARAMS; use rusqlite::{Connection, Row}; const DEFAULT_LIBRARY: &str = "~/Pictures/Photos Library.photoslibrary/database/photos.db"; pub struct ApplePhotos { database_location: String, connection: Connection, } pub trait Fromable<T> { fn from(row: &Row) -> Result<T>; } impl ApplePhotos { pub fn new_with_path(database_location: &str) -> Result<ApplePhotos> { let conn = Connection::open(database_location)?; Ok(ApplePhotos { database_location: database_location.to_string(), connection: conn, }) } pub fn new() -> Result<ApplePhotos> { return ApplePhotos::new_with_path(DEFAULT_LIBRARY); } pub fn query<T: Fromable<T>>(&self, query: &str) -> Result<Vec<T>> { let mut stmt = self.connection.prepare(query)?; let rows = stmt.query_map(NO_PARAMS, |row| { let item: T = T::from(row)?; Ok(item) })?; let mut items: Vec<T> = vec![]; for row in rows { items.push(row?); } Ok(items) } pub fn get_albums(&self) -> Result<Vec<Album>> { return self.query("SELECT * FROM RKAlbum"); } pub fn get_album_versions(&self) -> Result<Vec<AlbumVersion>> { return self.query("SELECT * FROM RKAlbumVersion"); } pub fn get_masters(&self) -> Result<Vec<Master>> { return self.query("SELECT * FROM RKMaster"); } }