Skip to main content

tauri_plugin_medialibrary/
lib.rs

1use tauri::{
2    ipc::ScopeObject,
3    plugin::{Builder, TauriPlugin},
4    utils::acl::Value,
5    AppHandle, Manager, Runtime,
6};
7
8pub use models::*;
9
10#[cfg(desktop)]
11mod desktop;
12#[cfg(mobile)]
13mod mobile;
14
15mod commands;
16mod directory_reader;
17mod error;
18mod models;
19mod scope;
20
21mod thumbnail_provider;
22
23mod uri;
24#[cfg(feature = "xdg")]
25mod xdg_directory_reader;
26
27#[cfg(feature = "amt")]
28mod amt_thumbnail_provider;
29
30#[cfg(feature = "thumb_cache")]
31mod thumbcache_thumbnail_provider;
32mod walkdir_reader;
33
34pub use error::{Error, Result};
35
36#[cfg(desktop)]
37use desktop::Medialibrary;
38#[cfg(mobile)]
39use mobile::Medialibrary;
40
41// implement ScopeObject here instead of in the scope module because it is also used on the build script
42// and we don't want to add tauri as a build dependency
43impl ScopeObject for scope::Entry {
44    type Error = Error;
45    fn deserialize<R: Runtime>(
46        _app: &AppHandle<R>,
47        raw: Value,
48    ) -> std::result::Result<Self, Self::Error> {
49        match serde_json::from_value(raw.into()).map(|raw| match raw {
50            scope::EntryRaw::Object { source } => source,
51            scope::EntryRaw::Value(val) => val,
52        }) {
53            Ok(source) => Ok(Self { source }),
54            Err(err) => Err(err.into()),
55        }
56    }
57}
58
59pub trait MedialibraryExt<R: Runtime> {
60    fn medialibrary(&self) -> &Medialibrary<R>;
61}
62
63impl<R: Runtime, T: Manager<R>> crate::MedialibraryExt<R> for T {
64    fn medialibrary(&self) -> &Medialibrary<R> {
65        self.state::<Medialibrary<R>>().inner()
66    }
67}
68
69/// Initializes the plugin.
70pub fn init<R: Runtime>() -> TauriPlugin<R> {
71    Builder::new("medialibrary")
72        .invoke_handler(tauri::generate_handler![
73            commands::get_images,
74            commands::get_thumbnail,
75            commands::get_available_sources,
76            commands::request_permissions,
77            commands::get_image,
78            commands::delete_image
79        ])
80        .setup(|app, api| {
81            #[cfg(mobile)]
82            let medialibrary = mobile::init(app, api)?;
83            #[cfg(desktop)]
84            let medialibrary = desktop::init(app, api)?;
85            app.manage(medialibrary);
86            Ok(())
87        })
88        .build()
89}