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