# file_management.rs — Persistent File Storage

<span style="white-space: pre-wrap;">Handles all file I/O for the application. Files are stored inside Tauri's </span>`<span class="editor-theme-code">app_data_dir</span>`<span style="white-space: pre-wrap;">, which is platform-specific (e.g. </span>`<span class="editor-theme-code">%APPDATA%\base_station</span>`<span style="white-space: pre-wrap;"> on Windows). Three subdirectories are used:</span>

<table id="bkmrk-directorypurposetask"><colgroup><col style="width: 240px;"></col><col style="width: 240px;"></col></colgroup><tbody><tr style="height: 11px;"><td>**Directory**

</td><td>**Purpose**

</td></tr><tr><td>`<span class="editor-theme-code">tasks/</span>`

</td><td>Saved task plan files

</td></tr><tr><td>`<span class="editor-theme-code">images/</span>`

</td><td>Snapshots captured from video feeds

</td></tr><tr><td>`<span class="editor-theme-code">maps/</span>`

</td><td>Imported map files

</td></tr></tbody></table>

<span style="white-space: pre-wrap;">These directories are created automatically on startup by </span>`<span class="editor-theme-code">ensure_storage_dirs_internal()</span>`.

### Commands

**`<strong class="editor-theme-bold editor-theme-code">save_task_file(file_name, data, directory)</strong>`**<span style="white-space: pre-wrap;"> Writes raw bytes to a file in the given subdirectory. Used to persist task plans.</span>

**`<strong class="editor-theme-bold editor-theme-code">list_task_files(directory) → Vec<String></strong>`**<span style="white-space: pre-wrap;"> Returns a list of filenames in the given subdirectory. Returns an empty list if the directory doesn't exist yet.</span>

**`<strong class="editor-theme-bold editor-theme-code">read_task_file(file_name) → Vec<u8></strong>`**<span style="white-space: pre-wrap;"> Reads a file from the </span>`<span class="editor-theme-code">tasks/</span>`<span style="white-space: pre-wrap;"> directory and returns its raw bytes.</span>

**`<strong class="editor-theme-bold editor-theme-code">delete_one_file(directory, file_name)</strong>`**<span style="white-space: pre-wrap;"> Deletes a single named file from the given subdirectory. Does nothing if the file doesn't exist.</span>

**`<strong class="editor-theme-bold editor-theme-code">delete_all_task_files(directory)</strong>`**<span style="white-space: pre-wrap;"> Removes all files in a subdirectory by deleting and recreating it.</span>

**`<strong class="editor-theme-bold editor-theme-code">import_map_file(directory)</strong>`**<span style="white-space: pre-wrap;"> Copies a file from an arbitrary path on the filesystem into the </span>`<span class="editor-theme-code">maps/</span>`<span style="white-space: pre-wrap;"> subdirectory. Used when the operator imports a new map via the file picker. The original filename is preserved.</span>

**`<strong class="editor-theme-bold editor-theme-code">save_snapshot(port, file_name)</strong>`**<span style="white-space: pre-wrap;"> Captures a single JPEG frame from an MJPEG stream (given by its localhost URL/port) and saves it to the </span>`<span class="editor-theme-code">images/</span>`<span style="white-space: pre-wrap;"> directory as </span>`<span class="editor-theme-code">{file_name}.jpg</span>`. Used in the Science task to photograph samples. It scans the raw HTTP stream for the JPEG start marker (`<span class="editor-theme-code">0xFF 0xD8</span>`) and end marker (`<span class="editor-theme-code">0xFF 0xD9</span>`), extracting the first complete frame. Has a 5 MB safety limit per frame.