mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
refactor(fs)!: remove read_text_file and write_text_file commands (#3605)
`readTextFile` and `writeTextFile` now go through `read_file` and `write_file`, so the `allow-read-text-file`, `deny-read-text-file`, `allow-write-text-file` and `deny-write-text-file` permissions are gone; grant `fs:allow-read-file` and `fs:allow-write-file` instead.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -86,12 +86,10 @@ const COMMANDS: &[&str] = &[
|
||||
"truncate",
|
||||
"ftruncate",
|
||||
"write",
|
||||
"write_text_file",
|
||||
"read_dir",
|
||||
"read_file",
|
||||
"read",
|
||||
"open",
|
||||
"read_text_file",
|
||||
"read_text_file_lines_next",
|
||||
"seek",
|
||||
"stat",
|
||||
|
||||
@@ -781,16 +781,7 @@ async function readTextFile(
|
||||
path: string | URL,
|
||||
options?: ReadFileOptions
|
||||
): Promise<string> {
|
||||
if (path instanceof URL && path.protocol !== 'file:') {
|
||||
throw new TypeError('Must be a file URL.')
|
||||
}
|
||||
|
||||
const arr = await invoke<ArrayBuffer | number[]>('plugin:fs|read_text_file', {
|
||||
path: path instanceof URL ? path.toString() : path,
|
||||
options
|
||||
})
|
||||
|
||||
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr)
|
||||
const bytes = await readFile(path, options)
|
||||
|
||||
return new TextDecoder(options?.encoding ?? 'utf-8').decode(bytes)
|
||||
}
|
||||
@@ -1138,18 +1129,7 @@ async function writeTextFile(
|
||||
data: string,
|
||||
options?: WriteFileOptions
|
||||
): Promise<void> {
|
||||
if (path instanceof URL && path.protocol !== 'file:') {
|
||||
throw new TypeError('Must be a file URL.')
|
||||
}
|
||||
|
||||
const encoder = new TextEncoder()
|
||||
|
||||
await invoke('plugin:fs|write_text_file', encoder.encode(data), {
|
||||
headers: {
|
||||
path: encodeURIComponent(path instanceof URL ? path.toString() : path),
|
||||
options: JSON.stringify(options)
|
||||
}
|
||||
})
|
||||
await writeFile(path, new TextEncoder().encode(data), options)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -330,32 +330,6 @@ Denies the read_file command without any pre-configured scope.
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-read-text-file`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Enables the read_text_file command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:deny-read-text-file`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Denies the read_text_file command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-read-text-file-lines-next`
|
||||
|
||||
</td>
|
||||
@@ -668,32 +642,6 @@ Denies the write command without any pre-configured scope.
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-write-text-file`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Enables the write_text_file command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:deny-write-text-file`
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
Denies the write_text_file command without any pre-configured scope.
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
`fs:allow-app-read-recursive`
|
||||
|
||||
</td>
|
||||
|
||||
@@ -8,7 +8,6 @@ commands.allow = [
|
||||
"read_file",
|
||||
"read",
|
||||
"open",
|
||||
"read_text_file",
|
||||
"read_text_file_lines",
|
||||
"read_text_file_lines_next",
|
||||
"seek",
|
||||
|
||||
@@ -9,7 +9,6 @@ specific base directories.
|
||||
permissions = [
|
||||
"allow-read-dir",
|
||||
"allow-read-file",
|
||||
"allow-read-text-file",
|
||||
"allow-read-text-file-lines",
|
||||
"allow-read-text-file-lines-next",
|
||||
"allow-exists",
|
||||
|
||||
@@ -7,7 +7,6 @@ commands.allow = [
|
||||
"read_file",
|
||||
"read",
|
||||
"open",
|
||||
"read_text_file",
|
||||
"read_text_file_lines",
|
||||
"read_text_file_lines_next",
|
||||
"seek",
|
||||
|
||||
@@ -13,5 +13,4 @@ commands.allow = [
|
||||
"ftruncate",
|
||||
"write",
|
||||
"write_file",
|
||||
"write_text_file",
|
||||
]
|
||||
|
||||
@@ -12,5 +12,4 @@ commands.allow = [
|
||||
"ftruncate",
|
||||
"write",
|
||||
"write_file",
|
||||
"write_text_file",
|
||||
]
|
||||
|
||||
@@ -605,26 +605,6 @@ pub struct ReadTextFileOptions {
|
||||
encoding: Option<String>,
|
||||
}
|
||||
|
||||
// TODO, remove in v3, rely on `read_file` command instead
|
||||
#[tauri::command]
|
||||
pub async fn read_text_file<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
global_scope: GlobalScope<Entry>,
|
||||
command_scope: CommandScope<Entry>,
|
||||
path: SafeFilePath,
|
||||
options: Option<BaseOptions>,
|
||||
) -> CommandResult<tauri::ipc::Response> {
|
||||
read_file_inner(
|
||||
"read-text-file",
|
||||
webview,
|
||||
global_scope,
|
||||
command_scope,
|
||||
path,
|
||||
options,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn read_text_file_lines<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
@@ -1167,24 +1147,6 @@ pub async fn write_file<R: Runtime>(
|
||||
write_file_inner("write-file", webview, global_scope, command_scope, request).await
|
||||
}
|
||||
|
||||
// TODO, remove in v3, rely on `write_file` command instead
|
||||
#[tauri::command]
|
||||
pub async fn write_text_file<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
global_scope: GlobalScope<Entry>,
|
||||
command_scope: CommandScope<Entry>,
|
||||
request: tauri::ipc::Request<'_>,
|
||||
) -> CommandResult<()> {
|
||||
write_file_inner(
|
||||
"write-text-file",
|
||||
webview,
|
||||
global_scope,
|
||||
command_scope,
|
||||
request,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn exists<R: Runtime>(
|
||||
webview: Webview<R>,
|
||||
|
||||
@@ -464,7 +464,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
|
||||
commands::read_dir,
|
||||
commands::read,
|
||||
commands::read_file,
|
||||
commands::read_text_file,
|
||||
commands::read_text_file_lines,
|
||||
commands::read_text_file_lines_next,
|
||||
commands::remove,
|
||||
@@ -477,7 +476,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
|
||||
commands::ftruncate,
|
||||
commands::write,
|
||||
commands::write_file,
|
||||
commands::write_text_file,
|
||||
commands::exists,
|
||||
commands::size,
|
||||
commands::start_accessing_security_scoped_resource,
|
||||
|
||||
Reference in New Issue
Block a user