file_system.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. use web_view::WebView;
  2. use tauri_api::dir;
  3. use tauri_api::file;
  4. use tauri_api::path::resolve_path;
  5. use std::fs;
  6. use std::fs::File;
  7. use std::io::Write;
  8. use super::cmd::{DirOperationOptions, FileOperationOptions};
  9. /// Reads a directory.
  10. #[cfg(read_dir)]
  11. pub fn read_dir<T: 'static>(
  12. webview: &mut WebView<'_, T>,
  13. path: String,
  14. options: Option<DirOperationOptions>,
  15. callback: String,
  16. error: String,
  17. ) {
  18. crate::execute_promise(
  19. webview,
  20. move || {
  21. let (recursive, dir) = if let Some(options_value) = options {
  22. (options_value.recursive, options_value.dir)
  23. } else {
  24. (false, None)
  25. };
  26. dir::read_dir(resolve_path(path, dir)?, recursive)
  27. },
  28. callback,
  29. error,
  30. );
  31. }
  32. /// Copies a file.
  33. #[cfg(copy_file)]
  34. pub fn copy_file<T: 'static>(
  35. webview: &mut WebView<'_, T>,
  36. source: String,
  37. destination: String,
  38. options: Option<FileOperationOptions>,
  39. callback: String,
  40. error: String,
  41. ) {
  42. crate::execute_promise(
  43. webview,
  44. move || {
  45. let (src, dest) = match options.and_then(|o| o.dir) {
  46. Some(dir) => (
  47. resolve_path(source, Some(dir.clone()))?,
  48. resolve_path(destination, Some(dir))?,
  49. ),
  50. None => (source, destination),
  51. };
  52. fs::copy(src, dest).map_err(|e| e.into())
  53. },
  54. callback,
  55. error,
  56. );
  57. }
  58. /// Creates a directory.
  59. #[cfg(create_dir)]
  60. pub fn create_dir<T: 'static>(
  61. webview: &mut WebView<'_, T>,
  62. path: String,
  63. options: Option<DirOperationOptions>,
  64. callback: String,
  65. error: String,
  66. ) {
  67. crate::execute_promise(
  68. webview,
  69. move || {
  70. let (recursive, dir) = if let Some(options_value) = options {
  71. (options_value.recursive, options_value.dir)
  72. } else {
  73. (false, None)
  74. };
  75. let resolved_path = resolve_path(path, dir)?;
  76. let response = if recursive {
  77. fs::create_dir_all(resolved_path)
  78. } else {
  79. fs::create_dir(resolved_path)
  80. };
  81. response.map_err(|e| e.into())
  82. },
  83. callback,
  84. error,
  85. );
  86. }
  87. /// Removes a directory.
  88. #[cfg(remove_dir)]
  89. pub fn remove_dir<T: 'static>(
  90. webview: &mut WebView<'_, T>,
  91. path: String,
  92. options: Option<DirOperationOptions>,
  93. callback: String,
  94. error: String,
  95. ) {
  96. crate::execute_promise(
  97. webview,
  98. move || {
  99. let (recursive, dir) = if let Some(options_value) = options {
  100. (options_value.recursive, options_value.dir)
  101. } else {
  102. (false, None)
  103. };
  104. let resolved_path = resolve_path(path, dir)?;
  105. let response = if recursive {
  106. fs::remove_dir_all(resolved_path)
  107. } else {
  108. fs::remove_dir(resolved_path)
  109. };
  110. response.map_err(|e| e.into())
  111. },
  112. callback,
  113. error,
  114. );
  115. }
  116. /// Removes a file
  117. #[cfg(remove_file)]
  118. pub fn remove_file<T: 'static>(
  119. webview: &mut WebView<'_, T>,
  120. path: String,
  121. options: Option<FileOperationOptions>,
  122. callback: String,
  123. error: String,
  124. ) {
  125. crate::execute_promise(
  126. webview,
  127. move || {
  128. let resolved_path = resolve_path(path, options.and_then(|o| o.dir))?;
  129. fs::remove_file(resolved_path).map_err(|e| e.into())
  130. },
  131. callback,
  132. error,
  133. );
  134. }
  135. /// Renames a file.
  136. #[cfg(rename_file)]
  137. pub fn rename_file<T: 'static>(
  138. webview: &mut WebView<'_, T>,
  139. old_path: String,
  140. new_path: String,
  141. options: Option<FileOperationOptions>,
  142. callback: String,
  143. error: String,
  144. ) {
  145. crate::execute_promise(
  146. webview,
  147. move || {
  148. let (old, new) = match options.and_then(|o| o.dir) {
  149. Some(dir) => (
  150. resolve_path(old_path, Some(dir.clone()))?,
  151. resolve_path(new_path, Some(dir))?,
  152. ),
  153. None => (old_path, new_path),
  154. };
  155. fs::rename(old, new).map_err(|e| e.into())
  156. },
  157. callback,
  158. error,
  159. );
  160. }
  161. /// Writes a text file.
  162. #[cfg(write_file)]
  163. pub fn write_file<T: 'static>(
  164. webview: &mut WebView<'_, T>,
  165. file: String,
  166. contents: String,
  167. options: Option<FileOperationOptions>,
  168. callback: String,
  169. error: String,
  170. ) {
  171. crate::execute_promise(
  172. webview,
  173. move || {
  174. File::create(resolve_path(file, options.and_then(|o| o.dir))?)
  175. .map_err(|e| e.into())
  176. .and_then(|mut f| f.write_all(contents.as_bytes()).map_err(|err| err.into()))
  177. },
  178. callback,
  179. error,
  180. );
  181. }
  182. /// Writes a binary file.
  183. #[cfg(write_binary_file)]
  184. pub fn write_binary_file<T: 'static>(
  185. webview: &mut WebView<'_, T>,
  186. file: String,
  187. contents: String,
  188. options: Option<FileOperationOptions>,
  189. callback: String,
  190. error: String,
  191. ) {
  192. crate::execute_promise(
  193. webview,
  194. move || {
  195. base64::decode(contents)
  196. .map_err(|e| e.into())
  197. .and_then(|c| {
  198. File::create(resolve_path(file, options.and_then(|o| o.dir))?)
  199. .map_err(|e| e.into())
  200. .and_then(|mut f| f.write_all(&c).map_err(|err| err.into()))
  201. })
  202. },
  203. callback,
  204. error,
  205. );
  206. }
  207. /// Reads a text file.
  208. #[cfg(read_text_file)]
  209. pub fn read_text_file<T: 'static>(
  210. webview: &mut WebView<'_, T>,
  211. path: String,
  212. options: Option<FileOperationOptions>,
  213. callback: String,
  214. error: String,
  215. ) {
  216. crate::execute_promise(
  217. webview,
  218. move || file::read_string(resolve_path(path, options.and_then(|o| o.dir))?),
  219. callback,
  220. error,
  221. );
  222. }
  223. /// Reads a binary file.
  224. #[cfg(read_binary_file)]
  225. pub fn read_binary_file<T: 'static>(
  226. webview: &mut WebView<'_, T>,
  227. path: String,
  228. options: Option<FileOperationOptions>,
  229. callback: String,
  230. error: String,
  231. ) {
  232. crate::execute_promise(
  233. webview,
  234. move || file::read_binary(resolve_path(path, options.and_then(|o| o.dir))?),
  235. callback,
  236. error,
  237. );
  238. }
  239. // test webview functionality.
  240. #[cfg(test)]
  241. mod test {
  242. // use super::*;
  243. // use web_view::*;
  244. // create a makeshift webview
  245. // fn create_test_webview() -> crate::Result<WebView<'static, ()>> {
  246. // // basic html set into webview
  247. // let content = r#"<html><head></head><body></body></html>"#;
  248. // Ok(
  249. // // use webview builder to create simple webview
  250. // WebViewBuilder::new()
  251. // .title("test")
  252. // .size(800, 800)
  253. // .resizable(true)
  254. // .debug(true)
  255. // .user_data(())
  256. // .invoke_handler(|_wv, _arg| Ok(()))
  257. // .content(Content::Html(content))
  258. // .build()?,
  259. // )
  260. // }
  261. /* #[test]
  262. #[cfg(not(any(target_os = "linux", target_os = "macos")))]
  263. // test the file_write functionality
  264. fn test_write_to_file() -> crate::Result<()> {
  265. // import read_to_string and write to be able to manipulate the file.
  266. use std::fs::{read_to_string, write};
  267. // create the webview
  268. let mut webview = create_test_webview()?;
  269. // setup the contents and the path.
  270. let contents = String::from(r#"Write to the Test file"#);
  271. let path = String::from("test/fixture/test.txt");
  272. // clear the file by writing nothing to it.
  273. write(&path, "")?;
  274. //call write file with the path and contents.
  275. write_file(
  276. &mut webview,
  277. path.clone(),
  278. contents.clone(),
  279. String::from(""),
  280. String::from(""),
  281. );
  282. // sleep the main thread to wait for the promise to execute.
  283. std::thread::sleep(std::time::Duration::from_millis(200));
  284. // read from the file.
  285. let data = read_to_string(path)?;
  286. // check that the file contents is equal to the expected contents.
  287. assert_eq!(data, contents);
  288. Ok(())
  289. } */
  290. }