123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- use web_view::WebView;
- use tauri_api::dir;
- use tauri_api::file;
- use tauri_api::path::resolve_path;
- use std::fs;
- use std::fs::File;
- use std::io::Write;
- use super::cmd::{DirOperationOptions, FileOperationOptions};
- /// Reads a directory.
- #[cfg(read_dir)]
- pub fn read_dir<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<DirOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let (recursive, dir) = if let Some(options_value) = options {
- (options_value.recursive, options_value.dir)
- } else {
- (false, None)
- };
- dir::read_dir(resolve_path(path, dir)?, recursive)
- },
- callback,
- error,
- );
- }
- /// Copies a file.
- #[cfg(copy_file)]
- pub fn copy_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- source: String,
- destination: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let (src, dest) = match options.and_then(|o| o.dir) {
- Some(dir) => (
- resolve_path(source, Some(dir.clone()))?,
- resolve_path(destination, Some(dir))?,
- ),
- None => (source, destination),
- };
- fs::copy(src, dest).map_err(|e| e.into())
- },
- callback,
- error,
- );
- }
- /// Creates a directory.
- #[cfg(create_dir)]
- pub fn create_dir<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<DirOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let (recursive, dir) = if let Some(options_value) = options {
- (options_value.recursive, options_value.dir)
- } else {
- (false, None)
- };
- let resolved_path = resolve_path(path, dir)?;
- let response = if recursive {
- fs::create_dir_all(resolved_path)
- } else {
- fs::create_dir(resolved_path)
- };
- response.map_err(|e| e.into())
- },
- callback,
- error,
- );
- }
- /// Removes a directory.
- #[cfg(remove_dir)]
- pub fn remove_dir<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<DirOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let (recursive, dir) = if let Some(options_value) = options {
- (options_value.recursive, options_value.dir)
- } else {
- (false, None)
- };
- let resolved_path = resolve_path(path, dir)?;
- let response = if recursive {
- fs::remove_dir_all(resolved_path)
- } else {
- fs::remove_dir(resolved_path)
- };
- response.map_err(|e| e.into())
- },
- callback,
- error,
- );
- }
- /// Removes a file
- #[cfg(remove_file)]
- pub fn remove_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let resolved_path = resolve_path(path, options.and_then(|o| o.dir))?;
- fs::remove_file(resolved_path).map_err(|e| e.into())
- },
- callback,
- error,
- );
- }
- /// Renames a file.
- #[cfg(rename_file)]
- pub fn rename_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- old_path: String,
- new_path: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- let (old, new) = match options.and_then(|o| o.dir) {
- Some(dir) => (
- resolve_path(old_path, Some(dir.clone()))?,
- resolve_path(new_path, Some(dir))?,
- ),
- None => (old_path, new_path),
- };
- fs::rename(old, new).map_err(|e| e.into())
- },
- callback,
- error,
- );
- }
- /// Writes a text file.
- #[cfg(write_file)]
- pub fn write_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- file: String,
- contents: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- File::create(resolve_path(file, options.and_then(|o| o.dir))?)
- .map_err(|e| e.into())
- .and_then(|mut f| f.write_all(contents.as_bytes()).map_err(|err| err.into()))
- },
- callback,
- error,
- );
- }
- /// Writes a binary file.
- #[cfg(write_binary_file)]
- pub fn write_binary_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- file: String,
- contents: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || {
- base64::decode(contents)
- .map_err(|e| e.into())
- .and_then(|c| {
- File::create(resolve_path(file, options.and_then(|o| o.dir))?)
- .map_err(|e| e.into())
- .and_then(|mut f| f.write_all(&c).map_err(|err| err.into()))
- })
- },
- callback,
- error,
- );
- }
- /// Reads a text file.
- #[cfg(read_text_file)]
- pub fn read_text_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || file::read_string(resolve_path(path, options.and_then(|o| o.dir))?),
- callback,
- error,
- );
- }
- /// Reads a binary file.
- #[cfg(read_binary_file)]
- pub fn read_binary_file<T: 'static>(
- webview: &mut WebView<'_, T>,
- path: String,
- options: Option<FileOperationOptions>,
- callback: String,
- error: String,
- ) {
- crate::execute_promise(
- webview,
- move || file::read_binary(resolve_path(path, options.and_then(|o| o.dir))?),
- callback,
- error,
- );
- }
- // test webview functionality.
- #[cfg(test)]
- mod test {
- // use super::*;
- // use web_view::*;
- // create a makeshift webview
- // fn create_test_webview() -> crate::Result<WebView<'static, ()>> {
- // // basic html set into webview
- // let content = r#"<html><head></head><body></body></html>"#;
- // Ok(
- // // use webview builder to create simple webview
- // WebViewBuilder::new()
- // .title("test")
- // .size(800, 800)
- // .resizable(true)
- // .debug(true)
- // .user_data(())
- // .invoke_handler(|_wv, _arg| Ok(()))
- // .content(Content::Html(content))
- // .build()?,
- // )
- // }
- /* #[test]
- #[cfg(not(any(target_os = "linux", target_os = "macos")))]
- // test the file_write functionality
- fn test_write_to_file() -> crate::Result<()> {
- // import read_to_string and write to be able to manipulate the file.
- use std::fs::{read_to_string, write};
- // create the webview
- let mut webview = create_test_webview()?;
- // setup the contents and the path.
- let contents = String::from(r#"Write to the Test file"#);
- let path = String::from("test/fixture/test.txt");
- // clear the file by writing nothing to it.
- write(&path, "")?;
- //call write file with the path and contents.
- write_file(
- &mut webview,
- path.clone(),
- contents.clone(),
- String::from(""),
- String::from(""),
- );
- // sleep the main thread to wait for the promise to execute.
- std::thread::sleep(std::time::Duration::from_millis(200));
- // read from the file.
- let data = read_to_string(path)?;
- // check that the file contents is equal to the expected contents.
- assert_eq!(data, contents);
- Ok(())
- } */
- }
|