Skip to content

Commit

Permalink
feat(fs): support ReadableStream<Unit8Array> for writeFile API
Browse files Browse the repository at this point in the history
closes #1598
  • Loading branch information
amrbashir committed Oct 21, 2024
1 parent 525abc4 commit bcada42
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 41 deletions.
7 changes: 7 additions & 0 deletions .changes/fs-readable-stream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"fs": "patch"
"fs-js": "patch"
---

Add support for using `ReadableStream<Unit8Array>` with `writeFile` API.

2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions plugins/fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ ios = { level = "partial", notes = "Access is restricted to Application folder b
tauri-plugin = { workspace = true, features = ["build"] }
schemars = { workspace = true }
serde = { workspace = true }
toml = "0.8"
tauri-utils = { workspace = true, features = ["build"] }

[dependencies]
serde = { workspace = true }
Expand Down
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.

87 changes: 61 additions & 26 deletions plugins/fs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use std::{
path::{Path, PathBuf},
};

use tauri_utils::acl::manifest::PermissionFile;

#[path = "src/scope.rs"]
#[allow(dead_code)]
mod scope;
Expand Down Expand Up @@ -62,31 +64,31 @@ const BASE_DIR_VARS: &[&str] = &[
"APPCACHE",
"APPLOG",
];
const COMMANDS: &[&str] = &[
"mkdir",
"create",
"copy_file",
"remove",
"rename",
"truncate",
"ftruncate",
"write",
"write_file",
"write_text_file",
"read_dir",
"read_file",
"read",
"open",
"read_text_file",
"read_text_file_lines",
"read_text_file_lines_next",
"seek",
"stat",
"lstat",
"fstat",
"exists",
"watch",
"unwatch",
const COMMANDS: &[(&str, &[&str])] = &[
("mkdir", &[]),
("create", &[]),
("copy_file", &[]),
("remove", &[]),
("rename", &[]),
("truncate", &[]),
("ftruncate", &[]),
("write", &[]),
("write_file", &["open", "write"]),
("write_text_file", &[]),
("read_dir", &[]),
("read_file", &[]),
("read", &[]),
("open", &[]),
("read_text_file", &[]),
("read_text_file_lines", &["read_text_file_lines_next"]),
("read_text_file_lines_next", &[]),
("seek", &[]),
("stat", &[]),
("lstat", &[]),
("fstat", &[]),
("exists", &[]),
("watch", &[]),
("unwatch", &[]),
];

fn main() {
Expand Down Expand Up @@ -192,9 +194,42 @@ permissions = [
}
}

tauri_plugin::Builder::new(COMMANDS)
tauri_plugin::Builder::new(&COMMANDS.iter().map(|c| c.0).collect::<Vec<_>>())
.global_api_script_path("./api-iife.js")
.global_scope_schema(schemars::schema_for!(FsScopeEntry))
.android_path("android")
.build();

// workaround to include nested permissions as `tauri_plugin` doesn't support it
let permissions_dir = autogenerated.join("commands");
for (command, nested_commands) in COMMANDS {
if nested_commands.is_empty() {
continue;
}

let permission_path = permissions_dir.join(format!("{command}.toml"));

let content = std::fs::read_to_string(&permission_path)
.expect(&format!("failed to read {command}.toml"));

let mut permission_file = toml::from_str::<PermissionFile>(&content)
.expect(&format!("failed to deserialize {command}.toml"));

for p in permission_file.permission.iter_mut() {
p.commands
.allow
.extend(nested_commands.iter().map(|s| s.to_string()));
}

let out = toml::to_string_pretty(&permission_file)
.expect(&format!("failed to serialize {command}.toml"));
let out = format!(
r#"# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
{out}"#
);
std::fs::write(permission_path, out).expect(&format!("failed to write {command}.toml"));
}
}
Loading

0 comments on commit bcada42

Please sign in to comment.