ExTauri.Filesystem (ex_tauri v0.2.0)

View Source

Read 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-fs must be installed (see Plugin Setup below)
  • The TauriHook must be mounted in your LiveView (see ExTauri.Hook)
  • Appropriate filesystem permissions must be configured in capabilities

Plugin Setup

mix ex_tauri.add fs

This 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.

Writes content to a file on the filesystem.

Functions

exists?(socket, path, opts \\ [], on_reply \\ nil)

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")

mkdir(socket, path, opts \\ [], on_reply \\ nil)

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")

read(socket, path, opts \\ [], on_reply \\ nil)

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")

readdir(socket, path, opts \\ [], on_reply \\ nil)

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")

remove(socket, path, opts \\ [], on_reply \\ nil)

Removes a file.

Options

  • :base_dir - Base directory for relative paths

Examples

ExTauri.Filesystem.remove(socket, "/path/to/file.txt")

write(socket, path, contents, opts \\ [], on_reply \\ nil)

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)