|
@@ -2,12 +2,13 @@ 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};
|
|
|
+use super::cmd::{DirOperationOptions, FileOperationOptions};
|
|
|
|
|
|
pub fn read_dir<T: 'static>(
|
|
|
webview: &mut WebView<'_, T>,
|
|
@@ -19,17 +20,17 @@ pub fn read_dir<T: 'static>(
|
|
|
crate::execute_promise(
|
|
|
webview,
|
|
|
move || {
|
|
|
- let recursive = if let Some(options_value) = options {
|
|
|
- options_value.recursive
|
|
|
+ let (recursive, dir) = if let Some(options_value) = options {
|
|
|
+ (options_value.recursive, options_value.dir)
|
|
|
} else {
|
|
|
- false
|
|
|
+ (false, None)
|
|
|
};
|
|
|
if recursive {
|
|
|
- dir::walk_dir(path)
|
|
|
+ dir::walk_dir(resolve_path(path, dir)?)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.and_then(|f| serde_json::to_string(&f).map_err(|err| err.into()))
|
|
|
} else {
|
|
|
- dir::list_dir_contents(path)
|
|
|
+ dir::list_dir_contents(resolve_path(path, dir)?)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.and_then(|f| serde_json::to_string(&f).map_err(|err| err.into()))
|
|
|
}
|
|
@@ -43,13 +44,20 @@ 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 || {
|
|
|
- fs::copy(source, destination)
|
|
|
+ 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| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.map(|_| "".to_string())
|
|
|
},
|
|
@@ -68,15 +76,16 @@ pub fn create_dir<T: 'static>(
|
|
|
crate::execute_promise(
|
|
|
webview,
|
|
|
move || {
|
|
|
- let recursive = if let Some(options_value) = options {
|
|
|
- options_value.recursive
|
|
|
+ let (recursive, dir) = if let Some(options_value) = options {
|
|
|
+ (options_value.recursive, options_value.dir)
|
|
|
} else {
|
|
|
- false
|
|
|
+ (false, None)
|
|
|
};
|
|
|
+ let resolved_path = resolve_path(path, dir)?;
|
|
|
let response = if recursive {
|
|
|
- fs::create_dir_all(path)
|
|
|
+ fs::create_dir_all(resolved_path)
|
|
|
} else {
|
|
|
- fs::create_dir(path)
|
|
|
+ fs::create_dir(resolved_path)
|
|
|
};
|
|
|
|
|
|
response
|
|
@@ -98,15 +107,16 @@ pub fn remove_dir<T: 'static>(
|
|
|
crate::execute_promise(
|
|
|
webview,
|
|
|
move || {
|
|
|
- let recursive = if let Some(options_value) = options {
|
|
|
- options_value.recursive
|
|
|
+ let (recursive, dir) = if let Some(options_value) = options {
|
|
|
+ (options_value.recursive, options_value.dir)
|
|
|
} else {
|
|
|
- false
|
|
|
+ (false, None)
|
|
|
};
|
|
|
+ let resolved_path = resolve_path(path, dir)?;
|
|
|
let response = if recursive {
|
|
|
- fs::remove_dir_all(path)
|
|
|
+ fs::remove_dir_all(resolved_path)
|
|
|
} else {
|
|
|
- fs::remove_dir(path)
|
|
|
+ fs::remove_dir(resolved_path)
|
|
|
};
|
|
|
|
|
|
response
|
|
@@ -121,13 +131,15 @@ pub fn remove_dir<T: 'static>(
|
|
|
pub fn remove_file<T: 'static>(
|
|
|
webview: &mut WebView<'_, T>,
|
|
|
path: String,
|
|
|
+ options: Option<FileOperationOptions>,
|
|
|
callback: String,
|
|
|
error: String,
|
|
|
) {
|
|
|
crate::execute_promise(
|
|
|
webview,
|
|
|
move || {
|
|
|
- fs::remove_file(path)
|
|
|
+ let resolved_path = resolve_path(path, options.and_then(|o| o.dir))?;
|
|
|
+ fs::remove_file(resolved_path)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.map(|_| "".to_string())
|
|
|
},
|
|
@@ -140,13 +152,20 @@ 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 || {
|
|
|
- fs::rename(old_path, new_path)
|
|
|
+ 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| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.map(|_| "".to_string())
|
|
|
},
|
|
@@ -159,13 +178,14 @@ 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(file)
|
|
|
+ File::create(resolve_path(file, options.and_then(|o| o.dir))?)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.and_then(|mut f| {
|
|
|
f.write_all(contents.as_bytes())
|
|
@@ -181,13 +201,14 @@ pub fn write_file<T: 'static>(
|
|
|
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(path)
|
|
|
+ file::read_string(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.and_then(|f| {
|
|
|
serde_json::to_string(&f)
|
|
@@ -202,13 +223,14 @@ pub fn read_text_file<T: 'static>(
|
|
|
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(path)
|
|
|
+ file::read_binary(resolve_path(path, options.and_then(|o| o.dir))?)
|
|
|
.map_err(|e| crate::ErrorKind::FileSystem(e.to_string()).into())
|
|
|
.and_then(|f| {
|
|
|
serde_json::to_string(&f)
|