use sync_async::sync_async;
use crate::*;
use super::*;
#[sync_async]
pub struct PublicStorage<'a, R: tauri::Runtime> {
#[cfg(target_os = "android")]
pub(crate) handle: &'a tauri::plugin::PluginHandle<R>,
#[cfg(not(target_os = "android"))]
#[allow(unused)]
pub(crate) handle: &'a std::marker::PhantomData<fn() -> R>,
}
#[cfg(target_os = "android")]
#[sync_async(
use(if_sync) impls::SyncImpls as Impls;
use(if_async) impls::AsyncImpls as Impls;
)]
impl<'a, R: tauri::Runtime> PublicStorage<'a, R> {
#[always_sync]
fn impls(&self) -> Impls<'_, R> {
Impls { handle: &self.handle }
}
}
#[sync_async(
use(if_async) api_async::{AndroidFs, Opener, Picker, AppStorage, PrivateStorage};
use(if_sync) api_sync::{AndroidFs, Opener, Picker, AppStorage, PrivateStorage};
)]
impl<'a, R: tauri::Runtime> PublicStorage<'a, R> {
#[maybe_async]
pub fn request_permission(&self) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().request_storage_permission_for_public_storage().await
}
}
#[maybe_async]
pub fn check_permission(&self) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().check_storage_permission_for_public_storage().await
}
}
#[maybe_async]
pub fn get_volumes(&self) -> Result<Vec<StorageVolume>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().get_available_storage_volumes_for_public_storage().await
}
}
#[maybe_async]
pub fn get_primary_volume(&self) -> Result<Option<StorageVolume>> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().get_primary_storage_volume_if_available_for_public_storage().await
}
}
#[maybe_async]
pub fn create_new_file(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
mime_type: Option<&str>
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().create_new_file_in_public_storage(
volume_id,
base_dir,
relative_path,
mime_type,
false
).await
}
}
#[maybe_async]
pub fn create_new_file_with_pending(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
mime_type: Option<&str>
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().create_new_file_in_public_storage(
volume_id,
base_dir,
relative_path,
mime_type,
true
).await
}
}
#[maybe_async]
pub fn create_dir_all(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().create_dir_all_in_public_storage(volume_id, base_dir, relative_path).await
}
}
#[maybe_async]
pub fn write_new(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
mime_type: Option<&str>,
contents: impl AsRef<[u8]>
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().write_new_file_in_public_storage(volume_id, base_dir, relative_path, mime_type, contents).await
}
}
#[maybe_async]
pub fn scan(
&self,
uri: &FsUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().scan_file_in_public_storage(uri, false).await
}
}
#[maybe_async]
pub fn scan_by_path(
&self,
path: impl AsRef<std::path::Path>,
mime_type: Option<&str>
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().scan_file_by_path_in_public_storage(path, mime_type).await
}
}
#[maybe_async]
pub fn set_pending(&self, uri: &FsUri, is_pending: bool) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().set_file_pending_in_public_storage(uri, is_pending).await
}
}
#[deprecated = "File operations via paths may result in unstable behaviour and inconsistent outcomes."]
#[maybe_async]
pub fn get_path(
&self,
uri: &FsUri,
) -> Result<std::path::PathBuf> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().get_file_path_in_public_storage(uri).await
}
}
#[deprecated = "File operations via paths may result in unstable behaviour and inconsistent outcomes."]
#[maybe_async]
pub fn resolve_path(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
) -> Result<std::path::PathBuf> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().resolve_dir_path_in_public_storage(volume_id, base_dir).await
}
}
#[always_sync]
pub fn is_audiobooks_dir_available(&self) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
Ok(self.impls().consts()?.env_dir_audiobooks.is_some())
}
}
#[always_sync]
pub fn is_recordings_dir_available(&self) -> Result<bool> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
Ok(self.impls().consts()?.env_dir_recordings.is_some())
}
}
#[maybe_async]
pub fn _scan(
&self,
uri: &FsUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().scan_file_in_public_storage(uri, true).await
}
}
#[maybe_async]
pub fn _scan_for_result(
&self,
uri: &FsUri,
) -> Result<()> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().scan_file_in_public_storage_for_result(uri, true).await
}
}
#[deprecated = "Use `Picker::resolve_public_storage_initial_location` instead."]
#[maybe_async]
pub fn resolve_initial_location(
&self,
volume_id: Option<&StorageVolumeId>,
base_dir: impl Into<PublicDir>,
relative_path: impl AsRef<std::path::Path>,
create_dir_all: bool
) -> Result<FsUri> {
#[cfg(not(target_os = "android"))] {
Err(Error::NOT_ANDROID)
}
#[cfg(target_os = "android")] {
self.impls().resolve_initial_location_in_public_storage(volume_id, base_dir, relative_path, create_dir_all).await
}
}
}