mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-11 12:37:34 +02:00
feat(fs): use IPC streaming for read and write file (#1251)
* feat(fs): use IPC streaming for read and write file * fix build * clippy
This commit is contained in:
committed by
GitHub
parent
5661cce795
commit
1d8662e71a
File diff suppressed because one or more lines are too long
@@ -698,12 +698,14 @@ async function readFile(
|
||||
throw new TypeError("Must be a file URL.");
|
||||
}
|
||||
|
||||
const arr = await invoke<number[]>("plugin:fs|read_file", {
|
||||
const arr = await invoke<ArrayBuffer | number[]>("plugin:fs|read_file", {
|
||||
path: path instanceof URL ? path.toString() : path,
|
||||
options,
|
||||
});
|
||||
|
||||
return Uint8Array.from(arr);
|
||||
return arr instanceof ArrayBuffer
|
||||
? new Uint8Array(arr)
|
||||
: Uint8Array.from(arr);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1007,10 +1009,11 @@ async function writeFile(
|
||||
throw new TypeError("Must be a file URL.");
|
||||
}
|
||||
|
||||
await invoke("plugin:fs|write_file", {
|
||||
path: path instanceof URL ? path.toString() : path,
|
||||
data: Array.from(data),
|
||||
options,
|
||||
await invoke("plugin:fs|write_file", data, {
|
||||
headers: {
|
||||
path: path instanceof URL ? path.toString() : path,
|
||||
options: JSON.stringify(options),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -336,7 +336,7 @@ pub fn read_file<R: Runtime>(
|
||||
command_scope: CommandScope<Entry>,
|
||||
path: SafePathBuf,
|
||||
options: Option<BaseOptions>,
|
||||
) -> CommandResult<Vec<u8>> {
|
||||
) -> CommandResult<tauri::ipc::Response> {
|
||||
let resolved_path = resolve_path(
|
||||
&webview,
|
||||
&global_scope,
|
||||
@@ -345,6 +345,7 @@ pub fn read_file<R: Runtime>(
|
||||
options.as_ref().and_then(|o| o.base_dir),
|
||||
)?;
|
||||
std::fs::read(&resolved_path)
|
||||
.map(tauri::ipc::Response::new)
|
||||
.map_err(|e| {
|
||||
format!(
|
||||
"failed to read file at path: {} with error: {e}",
|
||||
@@ -756,11 +757,27 @@ pub fn write_file<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
global_scope: GlobalScope<Entry>,
|
||||
command_scope: CommandScope<Entry>,
|
||||
path: SafePathBuf,
|
||||
data: Vec<u8>,
|
||||
options: Option<WriteFileOptions>,
|
||||
request: tauri::ipc::Request<'_>,
|
||||
) -> CommandResult<()> {
|
||||
write_file_inner(webview, &global_scope, &command_scope, path, &data, options)
|
||||
if let tauri::ipc::InvokeBody::Raw(data) = request.body() {
|
||||
let path = request
|
||||
.headers()
|
||||
.get("path")
|
||||
.ok_or_else(|| anyhow::anyhow!("missing file path").into())
|
||||
.and_then(|p| {
|
||||
p.to_str()
|
||||
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
|
||||
})
|
||||
.and_then(|p| SafePathBuf::new(p.into()).map_err(CommandError::from))?;
|
||||
let options = request
|
||||
.headers()
|
||||
.get("options")
|
||||
.and_then(|p| p.to_str().ok())
|
||||
.and_then(|opts| serde_json::from_str(opts).ok());
|
||||
write_file_inner(webview, &global_scope, &command_scope, path, data, options)
|
||||
} else {
|
||||
Err(anyhow::anyhow!("unexpected invoke body").into())
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
|
||||
@@ -77,7 +77,7 @@ fn socket_cleanup(socket: &PathBuf) {
|
||||
}
|
||||
|
||||
fn notify_singleton(socket: &PathBuf) -> Result<(), Error> {
|
||||
let stream = UnixStream::connect(&socket)?;
|
||||
let stream = UnixStream::connect(socket)?;
|
||||
let mut bf = BufWriter::new(&stream);
|
||||
let args_joined = std::env::args().collect::<Vec<String>>().join("\0");
|
||||
bf.write_all(args_joined.as_bytes())?;
|
||||
@@ -91,7 +91,7 @@ fn listen_for_other_instances<A: Runtime>(
|
||||
app: AppHandle<A>,
|
||||
mut cb: Box<SingleInstanceCallback<A>>,
|
||||
) {
|
||||
match UnixListener::bind(&socket) {
|
||||
match UnixListener::bind(socket) {
|
||||
Ok(listener) => {
|
||||
let cwd = std::env::current_dir()
|
||||
.unwrap_or_default()
|
||||
@@ -108,7 +108,7 @@ fn listen_for_other_instances<A: Runtime>(
|
||||
Ok(_) => {
|
||||
let args: Vec<String> =
|
||||
s.split('\0').map(String::from).collect();
|
||||
cb(&app.clone().app_handle(), args, cwd.clone());
|
||||
cb(app.app_handle(), args, cwd.clone());
|
||||
}
|
||||
Err(e) => log::debug!("single_instance failed to be notified: {e}"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user