fix(fs): writeFile command implementation on Android (#1708)

pull/1721/head
Lucas Fernandes Nogueira 9 months ago committed by GitHub
parent f7280c8830
commit 51819c601f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
"fs": patch:bug
---
Fixes `writeFile` command implementation on Android.

@ -13,6 +13,7 @@ use tauri::{
}; };
use std::{ use std::{
borrow::Cow,
fs::File, fs::File,
io::{BufReader, Lines, Read, Write}, io::{BufReader, Lines, Read, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -105,6 +106,8 @@ pub enum CommandError {
#[error(transparent)] #[error(transparent)]
Tauri(#[from] tauri::Error), Tauri(#[from] tauri::Error),
#[error(transparent)] #[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error), Io(#[from] std::io::Error),
#[error(transparent)] #[error(transparent)]
UrlParseError(#[from] url::ParseError), UrlParseError(#[from] url::ParseError),
@ -910,25 +913,31 @@ pub async fn write_file<R: Runtime>(
command_scope: CommandScope<Entry>, command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>, request: tauri::ipc::Request<'_>,
) -> CommandResult<()> { ) -> CommandResult<()> {
if let tauri::ipc::InvokeBody::Raw(data) = request.body() { let data = match request.body() {
let path = request tauri::ipc::InvokeBody::Raw(data) => Cow::Borrowed(data),
.headers() tauri::ipc::InvokeBody::Json(serde_json::Value::Array(data)) => Cow::Owned(
.get("path") data.iter()
.ok_or_else(|| anyhow::anyhow!("missing file path").into()) .flat_map(|v| v.as_number().and_then(|v| v.as_u64().map(|v| v as u8)))
.and_then(|p| { .collect(),
p.to_str() ),
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into()) _ => return Err(anyhow::anyhow!("unexpected invoke body").into()),
}) };
.and_then(|p| SafeFilePath::from_str(p).map_err(CommandError::from))?;
let options = request let path = request
.headers() .headers()
.get("options") .get("path")
.and_then(|p| p.to_str().ok()) .ok_or_else(|| anyhow::anyhow!("missing file path").into())
.and_then(|opts| serde_json::from_str(opts).ok()); .and_then(|p| {
write_file_inner(webview, &global_scope, &command_scope, path, data, options) p.to_str()
} else { .map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
Err(anyhow::anyhow!("unexpected invoke body").into()) })
} .and_then(|p| SafeFilePath::from_str(p).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)
} }
#[tauri::command] #[tauri::command]

Loading…
Cancel
Save