Use XDG directory structure, introduce split DB format

This commit is contained in:
Reinout Meliesie 2025-03-17 13:16:03 +01:00
parent 70e7729443
commit 08ede4ed82
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
2 changed files with 40 additions and 27 deletions

View file

@ -5,27 +5,50 @@ use async_sqlite :: rusqlite :: Row ;
use async_sqlite :: rusqlite :: Error :: * ;
use async_sqlite :: rusqlite :: ffi :: ErrorCode :: * ;
use fallible_iterator :: * ;
use std :: env :: * ;
use std :: path :: * ;
use crate :: error :: * ;
use crate :: error :: ZoodexError :: * ;
use crate :: utility :: * ;
pub struct DataManager { sqlite_client : Client }
pub struct DataManager {
sqlite_client_local : Client ,
sqlite_client_shared : Client ,
}
impl DataManager {
pub async fn new () -> Result <Self> {
let sqlite_client = ClientBuilder :: new ()
. path ("zoodex.sqlite")
let home_dir = var_os ("HOME") . unwrap () ;
let xdg_data_home = var_os ("XDG_DATA_HOME") ;
let data_dir = match xdg_data_home {
Some (xdg_data_home) => concat_os_str ! ( xdg_data_home , "/zoodex" ) ,
None => concat_os_str ! ( home_dir , "/.local/share/zoodex" ) ,
} ;
let sqlite_client_shared = ClientBuilder :: new ()
. path ( concat_os_str ! ( & data_dir , "/shared.sqlite" ) )
. flags ( OpenFlags :: SQLITE_OPEN_READ_ONLY | OpenFlags :: SQLITE_OPEN_NO_MUTEX )
. open ()
. await ? ;
let sqlite_client_local = ClientBuilder :: new ()
. path ( concat_os_str ! ( & data_dir , "/shared.sqlite" ) )
. flags ( OpenFlags :: SQLITE_OPEN_READ_WRITE | OpenFlags :: SQLITE_OPEN_NO_MUTEX )
. open ()
. await ? ;
Ok ( Self { sqlite_client } )
Ok ( Self {
sqlite_client_local ,
sqlite_client_shared ,
} )
}
pub async fn get_collection_overview ( & self ) -> Result <CollectionOverview> {
let collection_overview = self . sqlite_client . conn ( |sqlite_connection| {
let collection_overview = self . sqlite_client_shared . conn ( |sqlite_connection| {
let films = sqlite_connection
. prepare ( "
select uuid , name , original_name , release_date , runtime_minutes , poster_file_path
@ -53,7 +76,7 @@ impl DataManager {
}
pub async fn get_film_details ( & self , uuid : String ) -> Result <FilmDetails> {
let film_details = self . sqlite_client . conn ( |sqlite_connection| {
let film_details = self . sqlite_client_shared . conn ( |sqlite_connection| {
let film_details = sqlite_connection
. prepare ( "
select
@ -63,14 +86,13 @@ impl DataManager {
films . release_date ,
films . runtime_minutes ,
films . poster_file_path ,
films . source ,
sources . media_uuid ,
sources . bittorrent_hash ,
sources . file_path ,
sources . subtitle_file_path ,
sources . video_track ,
sources . audio_track ,
sources . subtitle_track
from films left join sources
on films . source = sources . uuid
on films . uuid = sources . media_uuid
where films . uuid = (?1)
" ) ?
. query ( [ uuid ] ) ?
@ -179,9 +201,8 @@ fn row_to_series_overview ( row : & Row ) -> rusqlite :: Result <SeriesOverview>
pub source : Option <SourceDetails> ,
}
# [ derive (Clone) ] pub struct SourceDetails {
pub bittorrent_hash : String ,
pub file_path : PathBuf ,
pub subtitle_file_path : Option <PathBuf> ,
pub video_track : Option <u32> ,
pub audio_track : Option <u32> ,
pub subtitle_track : Option <u32> ,
}
@ -194,19 +215,17 @@ fn row_to_film_details ( row : & Row ) -> rusqlite :: Result <FilmDetails> {
let runtime_minutes = row . get (4) ? ;
let poster_file_path = row . get :: < _ , Option <String> > (5) ? . map ( PathBuf :: from ) ;
let source_uuid = row . get :: < _ , Option <String> > (6) ? ;
let source = match source_uuid {
let source_media_uuid = row . get :: < _ , Option <String> > (6) ? ;
let source = match source_media_uuid {
Some (_) => {
let file_path = PathBuf :: from ( row . get :: < _ , String > (7) ? ) ;
let subtitle_file_path = row . get :: < _ , Option <String> > (8) ? . map ( PathBuf :: from ) ;
let video_track = row . get (9) ? ;
let audio_track = row . get (10) ? ;
let subtitle_track = row . get (11) ? ;
let bittorrent_hash = row . get (7) ? ;
let file_path = PathBuf :: from ( row . get :: < _ , String > (8) ? ) ;
let audio_track = row . get (9) ? ;
let subtitle_track = row . get (10) ? ;
Some ( SourceDetails {
bittorrent_hash ,
file_path ,
subtitle_file_path ,
video_track ,
audio_track ,
subtitle_track ,
} )

View file

@ -62,12 +62,6 @@ impl UI {
let arguments = [
Some ( source . file_path . as_os_str () . to_owned () ) ,
source . subtitle_file_path . map (
|subtitle_file_path| concat_os_str ! ( "--mpv-sub-file=" , subtitle_file_path ) ,
) ,
source . video_track . map (
|video_track| concat_os_str ! ( "--mpv-vid=" , to_os_string (video_track) ) ,
) ,
source . audio_track . map (
|audio_track| concat_os_str ! ( "--mpv-aid=" , to_os_string (audio_track) ) ,
) ,