ExTauri.Filesystem (ex_tauri v0.2.0)
View SourceRead and write files on the desktop filesystem from your LiveView.
Bridges to tauri-plugin-fs via the LiveView hook. This allows your
LiveView to read/write files outside the web sandbox.
Requirements
tauri-plugin-fsmust be installed (see Plugin Setup below)- The
TauriHookmust be mounted in your LiveView (seeExTauri.Hook) - Appropriate filesystem permissions must be configured in capabilities
Plugin Setup
mix ex_tauri.add fsThis adds the Cargo dependency, registers the plugin in main.rs, and adds
the fs:default capability. See Mix.Tasks.ExTauri.Add for details.
For broader access, add scoped permissions to
src-tauri/capabilities/default.json:
{
"identifier": "fs:allow-read-text-file",
"allow": [{"path": "$APPDATA/**"}]
}Examples
# Read a file
def handle_event("read_file", %{"path" => path}, socket) do
socket = ExTauri.Filesystem.read(socket, path)
{:noreply, socket}
end
# Handle the response
def handle_event("tauri_response", %{"command" => "fs_read", "contents" => contents}, socket) do
{:noreply, assign(socket, :file_contents, contents)}
end
Summary
Functions
Checks if a file or directory exists.
Creates a directory (and parent directories if needed).
Reads a file from the filesystem.
Lists entries in a directory.
Removes a file.
Writes content to a file on the filesystem.
Functions
Checks if a file or directory exists.
The result will be sent back as a "tauri_response" event with
%{"command" => "fs_exists", "exists" => true | false}.
Options
:base_dir- Base directory for relative paths
Examples
ExTauri.Filesystem.exists?(socket, "/path/to/file.txt")
Creates a directory (and parent directories if needed).
Options
:base_dir- Base directory for relative paths:recursive- Create parent directories (default: true)
Examples
ExTauri.Filesystem.mkdir(socket, "/path/to/new/directory")
Reads a file from the filesystem.
The result will be sent back as a "tauri_response" event with
%{"command" => "fs_read", "contents" => "..."}.
Options
:base_dir- Base directory for relative paths (e.g., "AppData", "Home", "Desktop")
Examples
ExTauri.Filesystem.read(socket, "/path/to/file.txt")
ExTauri.Filesystem.read(socket, "config.json", base_dir: "AppData")
Lists entries in a directory.
The result will be sent back as a "tauri_response" event with
%{"command" => "fs_readdir", "entries" => [...]}.
Options
:base_dir- Base directory for relative paths
Examples
ExTauri.Filesystem.readdir(socket, "/path/to/directory")
Removes a file.
Options
:base_dir- Base directory for relative paths
Examples
ExTauri.Filesystem.remove(socket, "/path/to/file.txt")
Writes content to a file on the filesystem.
Options
:base_dir- Base directory for relative paths:append- Append to file instead of overwriting (default: false)
Examples
ExTauri.Filesystem.write(socket, "/path/to/file.txt", "Hello, world!")
ExTauri.Filesystem.write(socket, "log.txt", "entry\n", base_dir: "AppData", append: true)