mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-13 12:44:45 +02:00
fix(fs): writeFile command implementation on Android (#1708)
This commit is contained in:
committed by
GitHub
parent
f7280c8830
commit
51819c601f
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"fs": patch:bug
|
||||
---
|
||||
|
||||
Fixes `writeFile` command implementation on Android.
|
||||
+28
-19
@@ -13,6 +13,7 @@ use tauri::{
|
||||
};
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
fs::File,
|
||||
io::{BufReader, Lines, Read, Write},
|
||||
path::{Path, PathBuf},
|
||||
@@ -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),
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user