//! Native shell around apps/app. The web app calls these commands through `@tauri-apps/api` when it runs //! inside the desktop app (see apps/app/src/platform.ts). The refresh token never touches disk in clear: //! it lives in the OS keychain (macOS Keychain, Windows Credential Manager, Secret Service on Linux). //! `ftp` runs the local FTP server cameras send photos to; `local` finds originals on disk for the editor; `catalog` //! indexes the photographer's own folders for it; `deliver` saves deliveries to folders here and imports upload //! destinations from Photo Mechanic and FileZilla; `updater` keeps the shell itself current. mod accel; mod card; mod catalog; mod faces; mod deliver; mod ftp; mod local; mod preview; mod ui; mod updater; use keyring::Entry; use tauri::{WebviewUrl, WebviewWindowBuilder}; /// Where new versions of the interface are published (`ui-manifest.json`, see `ui`). Set at build time /// (`PHOTOREPORT_APP_URL`). pub(crate) const APP_URL: &str = match option_env!("PHOTOREPORT_APP_URL") { Some(url) => url, None => "https://app.next.photoreport.pt", }; const API_URL: &str = match option_env!("PHOTOREPORT_API_URL") { Some(url) => url, None => "https://api.next.photoreport.pt", }; /// Tells the interface (whichever version is on disk) where the API is and where its updates come from. fn init_script() -> String { format!( "window.__PHOTOREPORT__ = Object.freeze({{ api: {}, app: {} }});", serde_json::to_string(API_URL).unwrap(), serde_json::to_string(APP_URL).unwrap() ) } const KEYCHAIN_SERVICE: &str = "pt.photoreport.desktop"; const KEYCHAIN_ACCOUNT: &str = "refresh-token"; fn entry() -> Result { Entry::new(KEYCHAIN_SERVICE, KEYCHAIN_ACCOUNT).map_err(|e| e.to_string()) } #[tauri::command] fn refresh_token_get() -> Result, String> { match entry()?.get_password() { Ok(token) => Ok(Some(token)), Err(keyring::Error::NoEntry) => Ok(None), Err(e) => Err(e.to_string()), } } #[tauri::command] fn refresh_token_set(token: String) -> Result<(), String> { entry()?.set_password(&token).map_err(|e| e.to_string()) } #[tauri::command] fn refresh_token_clear() -> Result<(), String> { match entry()?.delete_credential() { Ok(()) | Err(keyring::Error::NoEntry) => Ok(()), Err(e) => Err(e.to_string()), } } #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { // The interface is served from disk: the downloaded version in use, else the installer's copy (see `ui`). let shared = ui::Ui::default(); let mut context = tauri::generate_context!(); let embedded = context.set_assets(Box::new(ui::NoAssets)); context.set_assets(Box::new(ui::UiAssets { embedded, shared: shared.clone(), })); let result = tauri::Builder::default() .plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_dialog::init()) .plugin(tauri_plugin_updater::Builder::new().build()) .manage(ftp::FtpState::default()) .manage(local::LocalIndex::default()) .manage(catalog::Catalog::default()) .manage(card::CardState::default()) .manage(faces::FaceScan::default()) .manage(deliver::Deliveries::default()) .manage(shared) .setup(|app| { use tauri::Manager; ftp::auto_start(&app.state::()); // `tauri dev` serves the dev server: nothing to update. if !cfg!(debug_assertions) { ui::watch(app.handle().clone()); } // Always the interface on this computer (`tauri dev`: the dev server). No download to start, and it // works without a network; new versions arrive in the background (see `ui`). let url = WebviewUrl::App("index.html".into()); WebviewWindowBuilder::new(app, "main", url) .initialization_script(init_script()) .title("PhotoReport") .inner_size(1440.0, 900.0) .min_inner_size(1024.0, 680.0) .background_color(tauri::window::Color(0x12, 0x13, 0x14, 0xff)) .build()?; Ok(()) }) .invoke_handler(tauri::generate_handler![ refresh_token_get, refresh_token_set, refresh_token_clear, ftp::ftp_start, ftp::ftp_stop, ftp::ftp_status, ftp::ftp_check, ftp::ftp_take, ftp::ftp_retry, ftp::ftp_read, ftp::ftp_sent, ftp::ftp_open_folder, ftp::ftp_save_settings, local::local_original, local::local_original_changed, ftp::ftp_preview, ftp::ftp_info, catalog::catalog_folders, catalog::catalog_add, catalog::catalog_remove, catalog::catalog_scan, catalog::catalog_status, catalog::catalog_cancel, card::card_scan, card::card_ingest, card::card_settings, card::card_save_settings, faces::faces_local, faces::faces_scan, faces::faces_cancel, faces::faces_remember, faces::faces_read, updater::update_check, updater::update_install, ui::ui_status, ui::ui_check, deliver::delivery_machine, deliver::delivery_folders, deliver::delivery_folder_pick, deliver::delivery_write, deliver::import_upload_profiles ]) .run(context); if let Err(err) = result { eprintln!("PhotoReport desktop failed to start: {err}"); std::process::exit(1); } }