Skip to content

Commit 51819c6

Browse files
authored
fix(fs): writeFile command implementation on Android (#1708)
1 parent f7280c8 commit 51819c6

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fs": patch:bug
3+
---
4+
5+
Fixes `writeFile` command implementation on Android.

plugins/fs/src/commands.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use tauri::{
1313
};
1414

1515
use std::{
16+
borrow::Cow,
1617
fs::File,
1718
io::{BufReader, Lines, Read, Write},
1819
path::{Path, PathBuf},
@@ -105,6 +106,8 @@ pub enum CommandError {
105106
#[error(transparent)]
106107
Tauri(#[from] tauri::Error),
107108
#[error(transparent)]
109+
Json(#[from] serde_json::Error),
110+
#[error(transparent)]
108111
Io(#[from] std::io::Error),
109112
#[error(transparent)]
110113
UrlParseError(#[from] url::ParseError),
@@ -910,25 +913,31 @@ pub async fn write_file<R: Runtime>(
910913
command_scope: CommandScope<Entry>,
911914
request: tauri::ipc::Request<'_>,
912915
) -> CommandResult<()> {
913-
if let tauri::ipc::InvokeBody::Raw(data) = request.body() {
914-
let path = request
915-
.headers()
916-
.get("path")
917-
.ok_or_else(|| anyhow::anyhow!("missing file path").into())
918-
.and_then(|p| {
919-
p.to_str()
920-
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
921-
})
922-
.and_then(|p| SafeFilePath::from_str(p).map_err(CommandError::from))?;
923-
let options = request
924-
.headers()
925-
.get("options")
926-
.and_then(|p| p.to_str().ok())
927-
.and_then(|opts| serde_json::from_str(opts).ok());
928-
write_file_inner(webview, &global_scope, &command_scope, path, data, options)
929-
} else {
930-
Err(anyhow::anyhow!("unexpected invoke body").into())
931-
}
916+
let data = match request.body() {
917+
tauri::ipc::InvokeBody::Raw(data) => Cow::Borrowed(data),
918+
tauri::ipc::InvokeBody::Json(serde_json::Value::Array(data)) => Cow::Owned(
919+
data.iter()
920+
.flat_map(|v| v.as_number().and_then(|v| v.as_u64().map(|v| v as u8)))
921+
.collect(),
922+
),
923+
_ => return Err(anyhow::anyhow!("unexpected invoke body").into()),
924+
};
925+
926+
let path = request
927+
.headers()
928+
.get("path")
929+
.ok_or_else(|| anyhow::anyhow!("missing file path").into())
930+
.and_then(|p| {
931+
p.to_str()
932+
.map_err(|e| anyhow::anyhow!("invalid path: {e}").into())
933+
})
934+
.and_then(|p| SafeFilePath::from_str(p).map_err(CommandError::from))?;
935+
let options = request
936+
.headers()
937+
.get("options")
938+
.and_then(|p| p.to_str().ok())
939+
.and_then(|opts| serde_json::from_str(opts).ok());
940+
write_file_inner(webview, &global_scope, &command_scope, path, &data, options)
932941
}
933942

934943
#[tauri::command]

0 commit comments

Comments
 (0)