Skip to content

Commit

Permalink
use percent encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Sep 11, 2024
1 parent e81d162 commit 48e3ff7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ glob = "0.3"
notify = { version = "6", optional = true, features = ["serde"] }
notify-debouncer-full = { version = "0.3", optional = true }
dunce = { workspace = true }
percent-encoding = "2"

[features]
watch = ["notify", "notify-debouncer-full"]
2 changes: 1 addition & 1 deletion plugins/fs/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 11 additions & 13 deletions plugins/fs/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,11 +1013,9 @@ async function writeFile(
throw new TypeError('Must be a file URL.')
}

const pathStr = path instanceof URL ? path.toString() : path

await invoke('plugin:fs|write_file', data, {
headers: {
path: Array.from(new TextEncoder().encode(pathStr)).join(','),
path: encodeURIComponent(path instanceof URL ? path.toString() : path),
options: JSON.stringify(options)
}
})
Expand Down Expand Up @@ -1146,16 +1144,16 @@ type WatchEventKindModify =
| { kind: 'any' }
| { kind: 'data'; mode: 'any' | 'size' | 'content' | 'other' }
| {
kind: 'metadata'
mode:
| 'any'
| 'access-time'
| 'write-time'
| 'permissions'
| 'ownership'
| 'extended'
| 'other'
}
kind: 'metadata'
mode:
| 'any'
| 'access-time'
| 'write-time'
| 'permissions'
| 'ownership'
| 'extended'
| 'other'
}
| { kind: 'rename'; mode: 'any' | 'to' | 'from' | 'both' | 'other' }
| { kind: 'other' }

Expand Down
55 changes: 27 additions & 28 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 @@ -839,34 +840,32 @@ 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| {
let bytes: Vec<u8> = p
.split(',')
.map(str::trim)
.filter_map(|n| n.parse().ok())
.collect();
String::from_utf8(bytes)
.map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").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| {
percent_encoding::percent_decode(p.as_ref())
.decode_utf8()
.map_err(|_| anyhow::anyhow!("path is not a valid UTF-8").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 48e3ff7

Please sign in to comment.