path.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Types and functions related to file system path operations.
  5. use std::{
  6. env,
  7. path::{Component, Path, PathBuf},
  8. };
  9. use crate::{Config, PackageInfo};
  10. use serde_repr::{Deserialize_repr, Serialize_repr};
  11. /// A base directory to be used in [`resolve_path`].
  12. ///
  13. /// The base directory is the optional root of a file system operation.
  14. /// If informed by the API call, all paths will be relative to the path of the given directory.
  15. ///
  16. /// For more information, check the [`dirs_next` documentation](https://docs.rs/dirs_next/).
  17. #[derive(Serialize_repr, Deserialize_repr, Clone, Debug)]
  18. #[repr(u16)]
  19. #[non_exhaustive]
  20. pub enum BaseDirectory {
  21. /// The Audio directory.
  22. Audio = 1,
  23. /// The Cache directory.
  24. Cache,
  25. /// The Config directory.
  26. Config,
  27. /// The Data directory.
  28. Data,
  29. /// The LocalData directory.
  30. LocalData,
  31. /// The Desktop directory.
  32. Desktop,
  33. /// The Document directory.
  34. Document,
  35. /// The Download directory.
  36. Download,
  37. /// The Executable directory.
  38. Executable,
  39. /// The Font directory.
  40. Font,
  41. /// The Home directory.
  42. Home,
  43. /// The Picture directory.
  44. Picture,
  45. /// The Public directory.
  46. Public,
  47. /// The Runtime directory.
  48. Runtime,
  49. /// The Template directory.
  50. Template,
  51. /// The Video directory.
  52. Video,
  53. /// The Resource directory.
  54. Resource,
  55. /// The default App config directory.
  56. /// Resolves to [`BaseDirectory::Config`].
  57. App,
  58. /// The current working directory.
  59. Current,
  60. /// The Log directory.
  61. /// Resolves to [`BaseDirectory::Home/Library/Logs/{bundle_identifier}`] on macOS
  62. /// and [`BaseDirectory::Config/{bundle_identifier}/logs`] on linux and windows.
  63. Log,
  64. }
  65. /// Resolves the path with the optional base directory.
  66. ///
  67. /// # Example
  68. /// ```
  69. /// use tauri::{api::path::{resolve_path, BaseDirectory}, PackageInfo};
  70. /// // we use the default config and a mock PackageInfo, but in an actual app you should get the
  71. /// // Config created from `tauri.conf.json` and the app's PackageInfo instance.
  72. /// let path = resolve_path(
  73. /// &Default::default(),
  74. /// &PackageInfo {
  75. /// name: "app".into(),
  76. /// version: "1.0.0".into(),
  77. /// },
  78. /// "path/to/something",
  79. /// Some(BaseDirectory::Config)
  80. /// ).expect("failed to resolve path");
  81. /// // path is equal to "/home/${whoami}/.config/path/to/something" on Linux
  82. /// ```
  83. pub fn resolve_path<P: AsRef<Path>>(
  84. config: &Config,
  85. package_info: &PackageInfo,
  86. path: P,
  87. dir: Option<BaseDirectory>,
  88. ) -> crate::api::Result<PathBuf> {
  89. if let Some(base_dir) = dir {
  90. let resolve_resource = matches!(base_dir, BaseDirectory::Resource);
  91. let base_dir_path = match base_dir {
  92. BaseDirectory::Audio => audio_dir(),
  93. BaseDirectory::Cache => cache_dir(),
  94. BaseDirectory::Config => config_dir(),
  95. BaseDirectory::Data => data_dir(),
  96. BaseDirectory::LocalData => local_data_dir(),
  97. BaseDirectory::Desktop => desktop_dir(),
  98. BaseDirectory::Document => document_dir(),
  99. BaseDirectory::Download => download_dir(),
  100. BaseDirectory::Executable => executable_dir(),
  101. BaseDirectory::Font => font_dir(),
  102. BaseDirectory::Home => home_dir(),
  103. BaseDirectory::Picture => picture_dir(),
  104. BaseDirectory::Public => public_dir(),
  105. BaseDirectory::Runtime => runtime_dir(),
  106. BaseDirectory::Template => template_dir(),
  107. BaseDirectory::Video => video_dir(),
  108. BaseDirectory::Resource => resource_dir(package_info),
  109. BaseDirectory::App => app_dir(config),
  110. BaseDirectory::Current => Some(env::current_dir()?),
  111. BaseDirectory::Log => log_dir(config),
  112. };
  113. if let Some(mut base_dir_path_value) = base_dir_path {
  114. // use the same path resolution mechanism as the bundler's resource injection algorithm
  115. if resolve_resource {
  116. let mut resource_path = PathBuf::new();
  117. for component in path.as_ref().components() {
  118. match component {
  119. Component::Prefix(_) => {}
  120. Component::RootDir => resource_path.push("_root_"),
  121. Component::CurDir => {}
  122. Component::ParentDir => resource_path.push("_up_"),
  123. Component::Normal(p) => resource_path.push(p),
  124. }
  125. }
  126. base_dir_path_value.push(resource_path);
  127. } else {
  128. base_dir_path_value.push(path);
  129. }
  130. Ok(base_dir_path_value)
  131. } else {
  132. Err(crate::api::Error::Path(
  133. "unable to determine base dir path".to_string(),
  134. ))
  135. }
  136. } else {
  137. let mut dir_path = PathBuf::new();
  138. dir_path.push(path);
  139. Ok(dir_path)
  140. }
  141. }
  142. /// Returns the path to the user's audio directory.
  143. pub fn audio_dir() -> Option<PathBuf> {
  144. dirs_next::audio_dir()
  145. }
  146. /// Returns the path to the user's cache directory.
  147. pub fn cache_dir() -> Option<PathBuf> {
  148. dirs_next::cache_dir()
  149. }
  150. /// Returns the path to the user's config directory.
  151. pub fn config_dir() -> Option<PathBuf> {
  152. dirs_next::config_dir()
  153. }
  154. /// Returns the path to the user's data directory.
  155. pub fn data_dir() -> Option<PathBuf> {
  156. dirs_next::data_dir()
  157. }
  158. /// Returns the path to the user's local data directory.
  159. pub fn local_data_dir() -> Option<PathBuf> {
  160. dirs_next::data_local_dir()
  161. }
  162. /// Returns the path to the user's desktop directory.
  163. pub fn desktop_dir() -> Option<PathBuf> {
  164. dirs_next::desktop_dir()
  165. }
  166. /// Returns the path to the user's document directory.
  167. pub fn document_dir() -> Option<PathBuf> {
  168. dirs_next::document_dir()
  169. }
  170. /// Returns the path to the user's download directory.
  171. pub fn download_dir() -> Option<PathBuf> {
  172. dirs_next::download_dir()
  173. }
  174. /// Returns the path to the user's executable directory.
  175. pub fn executable_dir() -> Option<PathBuf> {
  176. dirs_next::executable_dir()
  177. }
  178. /// Returns the path to the user's font directory.
  179. pub fn font_dir() -> Option<PathBuf> {
  180. dirs_next::font_dir()
  181. }
  182. /// Returns the path to the user's home directory.
  183. pub fn home_dir() -> Option<PathBuf> {
  184. dirs_next::home_dir()
  185. }
  186. /// Returns the path to the user's picture directory.
  187. pub fn picture_dir() -> Option<PathBuf> {
  188. dirs_next::picture_dir()
  189. }
  190. /// Returns the path to the user's public directory.
  191. pub fn public_dir() -> Option<PathBuf> {
  192. dirs_next::public_dir()
  193. }
  194. /// Returns the path to the user's runtime directory.
  195. pub fn runtime_dir() -> Option<PathBuf> {
  196. dirs_next::runtime_dir()
  197. }
  198. /// Returns the path to the user's template directory.
  199. pub fn template_dir() -> Option<PathBuf> {
  200. dirs_next::template_dir()
  201. }
  202. /// Returns the path to the user's video dir
  203. pub fn video_dir() -> Option<PathBuf> {
  204. dirs_next::video_dir()
  205. }
  206. /// Returns the path to the resource directory of this app.
  207. pub fn resource_dir(package_info: &PackageInfo) -> Option<PathBuf> {
  208. crate::utils::platform::resource_dir(package_info).ok()
  209. }
  210. /// Returns the path to the suggested directory for your app config files.
  211. pub fn app_dir(config: &Config) -> Option<PathBuf> {
  212. dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
  213. }
  214. /// Returns the path to the log directory.
  215. pub fn log_dir(config: &Config) -> Option<PathBuf> {
  216. #[cfg(target_os = "macos")]
  217. let path = dirs_next::home_dir().map(|dir| {
  218. dir
  219. .join("Library/Logs")
  220. .join(&config.tauri.bundle.identifier)
  221. });
  222. #[cfg(not(target_os = "macos"))]
  223. let path =
  224. dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier).join("logs"));
  225. path
  226. }