// Copyright 2019-2023 Tauri Programme within The Commons Conservancy // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT use serde::de::DeserializeOwned; use tauri::{plugin::PluginApi, AppHandle, Runtime}; use crate::{models::*, FilePath, OpenOptions}; const PLUGIN_IDENTIFIER: &str = "com.plugin.fs"; pub struct Fs(tauri::plugin::PluginHandle); pub fn init( _app: &AppHandle, api: PluginApi, ) -> crate::Result> { let handle = api .register_android_plugin(PLUGIN_IDENTIFIER, "FsPlugin") .unwrap(); Ok(Fs(handle)) } impl Fs { /// Open a file. /// /// # Platform-specific /// /// - **iOS**: This method will automatically start accessing a security-scoped resource if the path is a file URL. /// You must call `stop_accessing_security_scoped_resource` when you're done accessing the file. pub fn open>( &self, path: P, opts: OpenOptions, ) -> std::io::Result { match path.into() { FilePath::Url(u) => self .resolve_content_uri(u.to_string(), opts.android_mode()) .map_err(|e| { std::io::Error::new( std::io::ErrorKind::Other, format!("failed to open file: {e}"), ) }), FilePath::Path(p) => { // tauri::utils::platform::resources_dir() returns a PathBuf with the Android asset URI prefix // we must resolve that file with the Android API if p.strip_prefix(tauri::utils::platform::ANDROID_ASSET_PROTOCOL_URI_PREFIX) .is_ok() { self.resolve_content_uri(p.to_string_lossy(), opts.android_mode()) .map_err(|e| { std::io::Error::new( std::io::ErrorKind::Other, format!("failed to open file: {e}"), ) }) } else { std::fs::OpenOptions::from(opts).open(p) } } } } fn resolve_content_uri( &self, uri: impl Into, mode: impl Into, ) -> crate::Result { let result = self.0.run_mobile_plugin::( "getFileDescriptor", GetFileDescriptorPayload { uri: uri.into(), mode: mode.into(), }, )?; if let Some(fd) = result.fd { Ok(unsafe { use std::os::fd::FromRawFd; std::fs::File::from_raw_fd(fd) }) } else { unimplemented!() } } }