Note: I’m using a translation tool, so there may be some inappropriate expressions.
Overview
This plugin provides functions that create ReadableStream and WritableStream from a file path.
Setup
First, install this plugin to your Tauri project:
src-tauri/Cargo.toml
[]
= "=0.3.0"
Next, register this plugin in your Tauri project:
src-tauri/src/lib.rs
Then, set the APIs and file paths that can be used from the Javascript:
src-tauri/capabilities/*.json
Finally, install the JavaScript Guest bindings using whichever JavaScript package manager you prefer:
# or
# or
NOTE: Please make sure that the Rust-side tauri-plugin-fs-stream and the JavaScript-side tauri-plugin-fs-stream-api versions match exactly.
Example
import { openReadFileStream, openWriteFileStream } from "tauri-plugin-fs-stream-api";
async function convertFile(
inputPath: string,
outputPath: string,
convertor: TransformStream<Uint8Array<ArrayBuffer>, Uint8Array>
) {
let input: ReadableStream<Uint8Array<ArrayBuffer>> | null = null
let output: WritableStream<Uint8Array> | null = null
try {
input = await openReadFileStream(inputPath)
output = await openWriteFileStream(outputPath)
await input.pipeThrough(convertor).pipeTo(output)
}
catch (e) {
await input?.cancel().catch(() => {})
await output?.abort().catch(() => {})
throw e
}
}
File Access
Access control for file paths follows the same model as the fs plugin.
This plugin prevents path traversal and can access only the paths explicitly declared in the capability file. In that case, you can also configure plugins.fs-stream.requireLiteralLeadingDot in src-tauri/tauri.conf.json like with the fs plugin.
An exception applies to files that the user explicitly selects through drag and drop (only when the fs plugin is enabled) or via the dialog plugin. Such files are accessible even if they are not declared in the capability configuration. And these permissions can be persisted using the persisted scope plugin, allowing access to remain available across application restarts.
License
This project is licensed under either of
- MIT license
- Apache License (Version 2.0)
at your option.