Skip to content

Commit

Permalink
fix(fs): writeFile command implementation on Android (tauri-apps#1708)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored and Sir-Thom committed Oct 22, 2024
1 parent 75808b9 commit b9a369f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-fs-write-file-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fs": patch:bug
---

Fixes `writeFile` command implementation on Android.
47 changes: 28 additions & 19 deletions plugins/fs/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use tauri::{
};

use std::{
borrow::Cow,
fs::File,
io::{BufReader, Lines, Read, Write},
path::{Path, PathBuf},
Expand Down Expand Up @@ -105,6 +106,8 @@ pub enum CommandError {
#[error(transparent)]
Tauri(#[from] tauri::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
UrlParseError(#[from] url::ParseError),
Expand Down Expand Up @@ -910,25 +913,31 @@ pub async fn write_file<R: Runtime>(
command_scope: CommandScope<Entry>,
request: tauri::ipc::Request<'_>,
) -> CommandResult<()> {
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| 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)
} else {
Err(anyhow::anyhow!("unexpected invoke body").into())
}
let data = match request.body() {
tauri::ipc::InvokeBody::Raw(data) => Cow::Borrowed(data),
tauri::ipc::InvokeBody::Json(serde_json::Value::Array(data)) => Cow::Owned(
data.iter()
.flat_map(|v| v.as_number().and_then(|v| v.as_u64().map(|v| v as u8)))
.collect(),
),
_ => return Err(anyhow::anyhow!("unexpected invoke body").into()),
};

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| 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]
Expand Down

0 comments on commit b9a369f

Please sign in to comment.