# Handlers


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Gallery State

State object for tracking gallery state between requests.

------------------------------------------------------------------------

### GalleryState

``` python

def GalleryState(
    view_mode:ViewMode=<ViewMode.GRID: 'grid'>, current_page:int=1, active_types:List=<factory>,
    selected_paths:List=<factory>, preview_path:Optional=None
)->None:

```

*State for the media gallery.*

``` python
# Test GalleryState
state = GalleryState(
    view_mode=ViewMode.LIST,
    current_page=2,
    active_types=[FileType.IMAGE, FileType.VIDEO],
    selected_paths=["/path/to/file.jpg"]
)

# Test serialization
data = state.to_dict()
assert data["view_mode"] == "list"
assert data["current_page"] == 2
assert "image" in data["active_types"]

# Test deserialization
restored = GalleryState.from_dict(data)
assert restored.view_mode == ViewMode.LIST
assert restored.current_page == 2
assert FileType.IMAGE in restored.active_types

print("GalleryState tests passed!")
```

    GalleryState tests passed!

## Handler Functions

Individual handler functions for gallery actions.

## Router Initialization

Create and configure the APIRouter with all gallery routes.

------------------------------------------------------------------------

### init_router

``` python

def init_router(
    config:GalleryConfig, # Gallery configuration
    files_getter:Callable, # Function to get files
    mounter:DirectoryMounter, # File URL mounter
    state_getter:Callable, # Function to get current state
    state_setter:Callable, # Function to save state
    route_prefix:str='/gallery', # Route prefix for all gallery routes
    callbacks:Optional=None, # Optional callbacks
)->APIRouter: # Configured APIRouter with all gallery routes

```

*Initialize and return an APIRouter with all gallery routes.*

``` python
import tempfile
from pathlib import Path

# Simple mock app for testing
class MockApp:
    def __init__(self):
        self.routes = []
app = MockApp()

# Test router initialization
with tempfile.TemporaryDirectory() as tmpdir:
    # Create test files
    (Path(tmpdir) / "image.jpg").write_text("img")
    (Path(tmpdir) / "song.mp3").write_text("audio")
    
    config = GalleryConfig()
    mounter = DirectoryMounter()
    mounter.mount(app, tmpdir)
    
    # Simple state storage
    _state = GalleryState(active_types=config.filter.enabled_types)
    def get_state(): return _state
    def set_state(s): global _state; _state = s
    
    # Simple files getter
    def get_files():
        return [
            FileInfo(name="image.jpg", path=str(Path(tmpdir) / "image.jpg"),
                     is_directory=False, file_type=FileType.IMAGE),
            FileInfo(name="song.mp3", path=str(Path(tmpdir) / "song.mp3"),
                     is_directory=False, file_type=FileType.AUDIO),
        ]
    
    # Create router
    router = init_router(
        config=config,
        files_getter=get_files,
        mounter=mounter,
        state_getter=get_state,
        state_setter=set_state,
        route_prefix="/gallery",
    )
    
    # Verify router was created
    assert router is not None
    assert router.prefix == "/gallery"

print("Router initialization tests passed!")
```

    [DirectoryMounter] Warning: Directory does not exist: t
    [DirectoryMounter] Warning: Directory does not exist: m
    [DirectoryMounter] Warning: Directory does not exist: p
    [DirectoryMounter] Warning: Directory does not exist: t
    [DirectoryMounter] Warning: Directory does not exist: m
    [DirectoryMounter] Warning: Directory does not exist: p
    [DirectoryMounter] Warning: Directory does not exist: u
    [DirectoryMounter] Warning: Directory does not exist: i
    [DirectoryMounter] Warning: Directory does not exist: a
    [DirectoryMounter] Warning: Directory does not exist: w
    [DirectoryMounter] Warning: Directory does not exist: c
    [DirectoryMounter] Warning: Directory does not exist: l
    [DirectoryMounter] Warning: Directory does not exist: e
    [DirectoryMounter] Warning: Directory does not exist: o
    Router initialization tests passed!
