tauri_plugin_android_fs/
models.rs

1use std::time::SystemTime;
2use serde::{Deserialize, Serialize};
3
4
5/// Path to represent a file or directory.
6/// 
7/// # Note
8/// For compatibility, an interconversion to [`tauri_plugin_fs::FilePath`] is implemented, such as follwing.  
9/// This is lossy and also not guaranteed to work properly with other plugins.  
10/// However, if a file uri, reading and writing files by [`tauri_plugin_fs`] etc. should work well.  
11/// ```no_run
12/// use tauri_plugin_android_fs::FileUri;
13/// use tauri_plugin_fs::FilePath;
14/// 
15/// let uri: FileUri = unimplemented!();
16/// 
17/// let path: FilePath = uri.into();
18/// 
19/// let uri: FileUri = path.into();
20/// ```
21/// 
22/// # Typescript type
23/// You should use the following type because it might change in the future, and the inner value should not be used directly.  
24/// ```typescript
25/// type FileUri = any
26/// type FileUri = string
27/// ```
28#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
29#[serde(rename_all = "camelCase")]
30pub struct FileUri {
31    pub(crate) uri: String,
32    pub(crate) document_top_tree_uri: Option<String>,
33}
34
35impl From<tauri_plugin_fs::FilePath> for FileUri {
36
37    fn from(value: tauri_plugin_fs::FilePath) -> Self {
38        let uri = match value {
39            tauri_plugin_fs::FilePath::Url(url) => url.to_string(),
40            tauri_plugin_fs::FilePath::Path(path_buf) => format!("file://{}", path_buf.to_string_lossy()),
41        };
42
43        Self { uri, document_top_tree_uri: None }
44    }
45}
46
47impl From<FileUri> for tauri_plugin_fs::FilePath {
48
49    fn from(value: FileUri) -> Self {
50        let result: std::result::Result<_, std::convert::Infallible> = value.uri.parse();
51
52        // This will not cause panic. Because result err is infallible.
53        result.unwrap()
54    }
55}
56
57#[derive(Debug, Clone, Hash, PartialEq, Eq, Deserialize, Serialize)]
58#[serde(rename_all = "camelCase")]
59pub enum Entry {
60
61    #[non_exhaustive]
62    File {
63        name: String,
64        uri: FileUri,
65        last_modified: SystemTime,
66        byte_size: u64,
67        mime_type: String,
68    },
69
70    #[non_exhaustive]
71    Dir {
72        name: String,
73        uri: FileUri,
74        last_modified: SystemTime,
75    }
76}
77
78/// Access mode
79#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
80pub enum PersistableAccessMode {
81
82    /// Read-only access.
83    ReadOnly,
84
85    /// Write-only access.
86    WriteOnly,
87
88    /// Read-write access.
89    ReadAndWrite,
90}
91
92/// Access mode
93#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
94#[non_exhaustive]
95pub enum FileAccessMode {
96
97    /// Opens the file in read-only mode.
98    /// 
99    /// FileDescriptor mode: "r"
100    Read,
101
102    /// Opens the file in write-only mode.
103    /// **This may or may not truncate.**
104    /// So please use `WriteTruncate` or `WriteAppend` instead.
105    ///
106    /// FileDescriptor mode: "w"
107    Write,
108
109    /// Opens the file in write-only mode.
110    /// The existing content is truncated (deleted), and new data is written from the beginning.
111    /// Creates a new file if it does not exist.
112    ///
113    /// FileDescriptor mode: "wt"
114    WriteTruncate,
115
116    /// Opens the file in write-only mode.
117    /// The existing content is preserved, and new data is appended to the end of the file.
118    /// Creates a new file if it does not exist.
119    /// 
120    /// FileDescriptor mode: "wa"
121    WriteAppend,
122
123    /// Opens the file in read-write mode.  
124    /// 
125    /// FileDescriptor mode: "rw"
126    ReadWrite,
127
128    /// Opens the file in read-write mode.
129    /// The existing content is truncated (deleted), and new data is written from the beginning.
130    /// Creates a new file if it does not exist.
131    ///
132    /// FileDescriptor mode: "rwt"
133    ReadWriteTruncate,
134}
135
136/// Filters for VisualMediaPicker.
137#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
138pub enum VisualMediaTarget {
139
140    /// Allow only images to be selected.  
141    ImageOnly,
142
143    /// Allow only videos to be selected.  
144    VideoOnly,
145
146    /// Allow only images and videos to be selected.  
147    ImageAndVideo
148}
149
150/// The application specific directory.  
151#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
152#[non_exhaustive]
153pub enum PrivateDir {
154
155    /// The application specific persistent-data directory.  
156    /// 
157    /// The system prevents other apps from accessing these locations, and on Android 10 (API level 29) and higher, these locations are encrypted.  
158    ///  
159    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request.  
160    /// 
161    /// ex: `/data/user/0/{app-package-name}/files`
162    Data,
163
164    /// The application specific cache directory.  
165    /// 
166    /// The system prevents other apps from accessing these locations, and on Android 10 (API level 29) and higher, these locations are encrypted.  
167    /// 
168    /// These files will be deleted when the app is uninstalled and may also be deleted at the user’s request. 
169    /// In addition, the system will automatically delete files in this directory as disk space is needed elsewhere on the device.  
170    /// 
171    /// ex: `/data/user/0/{app-package-name}/cache`
172    Cache,
173}
174
175#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
176#[non_exhaustive]
177pub enum PublicDir {
178    
179    #[serde(untagged)]
180    Image(PublicImageDir),
181
182    #[serde(untagged)]
183    Video(PublicVideoDir),
184
185    #[serde(untagged)]
186    Audio(PublicAudioDir),
187
188    #[serde(untagged)]
189    GeneralPurpose(PublicGeneralPurposeDir),
190}
191
192/// Directory in which to place images that are available to other applications and users.  
193#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
194#[non_exhaustive]
195pub enum PublicImageDir {
196
197    /// Standard directory in which to place pictures that are available to the user.  
198    /// 
199    /// ex: `~/Pictures`
200    Pictures,
201
202    /// The traditional location for pictures and videos when mounting the device as a camera.  
203    /// 
204    /// ex: `~/DCIM`
205    DCIM,
206}
207
208/// Directory in which to place videos that are available to other applications and users.  
209#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
210#[non_exhaustive]
211pub enum PublicVideoDir {
212
213	/// Standard directory in which to place movies that are available to the user.  
214	/// 
215	/// ex: `~/Movies`
216	Movies,
217
218	/// The traditional location for pictures and videos when mounting the device as a camera.  
219	/// 
220	/// ex: `~/DCIM`
221	DCIM,
222}
223
224/// Directory in which to place audios that are available to other applications and users.  
225#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
226#[non_exhaustive]
227pub enum PublicAudioDir {
228
229    /// Standard directory in which to place movies that are available to the user.  
230    /// 
231    /// ex: `~/Music`
232    Music,
233
234    /// Standard directory in which to place any audio files that should be in the list of alarms that the user can select (not as regular music).  
235    /// 
236    /// ex: `~/Alarms`
237    Alarms,
238
239    /// Standard directory in which to place any audio files that should be in the list of audiobooks that the user can select (not as regular music).  
240    /// 
241    /// This is not available on Android 9 (API level 28) and lower.  
242    /// 
243    /// ex: `~/Audiobooks`  
244    Audiobooks,
245
246    /// Standard directory in which to place any audio files that should be in the list of notifications that the user can select (not as regular music).  
247    /// 
248    /// ex: `~/Notifications`
249    Notifications,
250
251    /// Standard directory in which to place any audio files that should be in the list of podcasts that the user can select (not as regular music).  
252    /// 
253    /// ex: `~/Podcasts`
254    Podcasts,
255
256    /// Standard directory in which to place any audio files that should be in the list of ringtones that the user can select (not as regular music).  
257    /// 
258    /// ex: `~/Ringtones`
259    Ringtones,
260
261    /// Standard directory in which to place any audio files that should be in the list of voice recordings recorded by voice recorder apps that the user can select (not as regular music).   
262    /// 
263    /// This is not available on Android 11 (API level 30) and lower.  
264    /// 
265    /// ex: `~/Recordings`
266    Recordings,
267}
268
269/// Directory in which to place files that are available to other applications and users.  
270#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Deserialize, Serialize)]
271#[non_exhaustive]
272pub enum PublicGeneralPurposeDir {
273
274    /// Standard directory in which to place documents that have been created by the user.  
275    /// 
276    /// ex: `~/Documents`
277    Documents,
278
279    /// Standard directory in which to place files that have been downloaded by the user.  
280    /// 
281    /// ex: `~/Download`  
282    Download,
283}
284
285
286macro_rules! impl_into_pubdir {
287    ($target: ident, $wrapper: ident) => {
288        impl From<$target> for PublicDir {
289            fn from(value: $target) -> Self {
290                Self::$wrapper(value)
291            }
292        }
293    };
294}
295impl_into_pubdir!(PublicImageDir, Image);
296impl_into_pubdir!(PublicVideoDir, Video);
297impl_into_pubdir!(PublicAudioDir, Audio);
298impl_into_pubdir!(PublicGeneralPurposeDir, GeneralPurpose);