Rindle.LiveView (Rindle v0.1.5)

Copy Markdown View Source

LiveView integration helpers for direct-to-storage uploads via Rindle.

These helpers wrap Phoenix LiveView's upload primitives to configure external (direct-to-storage) uploads using Rindle's presigned URL generation and the Rindle.verify_completion/2 post-upload verification pipeline.

Usage

In your LiveView:

def mount(_params, _session, socket) do
  socket =
    socket
    |> Rindle.LiveView.allow_upload(:avatar, MyApp.AvatarProfile,
         accept: ~w(.jpg .jpeg .png),
         max_entries: 1,
         max_file_size: 10_000_000
       )
    |> assign(:asset_topic, Rindle.LiveView.subscribe(:asset, "asset-id"))

  {:ok, socket}
end

def handle_event("save", _params, socket) do
  results =
    Rindle.LiveView.consume_uploaded_entries(socket, :avatar, fn _entry, meta ->
      {:ok, meta.asset_id}
    end)

  {:noreply, assign(socket, :uploaded_asset_ids, results)}
end

def handle_info({:rindle_event, type, payload}, socket) do
  case type do
    :variant_started -> {:noreply, assign(socket, :variant_status, payload.state)}
    :variant_progress -> {:noreply, assign(socket, :variant_progress, payload.progress)}
    :variant_ready -> {:noreply, assign(socket, :variant_status, payload.state)}
    :variant_failed -> {:noreply, assign(socket, :variant_error, payload)}
    :variant_cancelled -> {:noreply, assign(socket, :variant_status, payload.state)}
  end
end

The :external option is set automatically — you do not need to provide it.

Summary

Functions

Configures an upload on the socket with Rindle's external upload signer.

Consumes completed upload entries and verifies them through Rindle.

Subscribes the current process to a Rindle PubSub topic for a supported scope.

Unsubscribes the current process from a topic returned by subscribe/2.

Types

consume_func()

@type consume_func() :: (Phoenix.LiveView.UploadEntry.t(), map() -> consume_result())

consume_result()

@type consume_result() :: {:ok, term()} | {:postpone, term()}

subscription_scope()

@type subscription_scope() :: :variant | :asset | :upload_session

Functions

allow_upload(socket, name, profile, opts \\ [])

Configures an upload on the socket with Rindle's external upload signer.

Wraps LiveView upload configuration and sets the :external option to a function that initiates a Rindle upload session and returns a presigned PUT URL.

Parameters

  • socket - The LiveView socket
  • name - The upload name (atom), e.g. :avatar
  • profile - The Rindle profile module to use for storage/validation
  • opts - Options passed through to the LiveView upload configuration (for example :accept, :max_entries, :max_file_size). The :external key is set by Rindle and should not be provided.

Returns

The updated socket with the upload configured.

consume_uploaded_entries(socket, name, func)

@spec consume_uploaded_entries(Phoenix.LiveView.Socket.t(), atom(), consume_func()) ::
  list()

Consumes completed upload entries and verifies them through Rindle.

For each completed entry, calls Rindle.verify_completion/2 to confirm the object landed in storage, then invokes the user-provided function with the entry and its metadata.

Parameters

  • socket - The LiveView socket
  • name - The upload name (atom)
  • func - A 2-arity function fn entry, meta -> result called for each completed entry. The meta map includes :session_id and :asset_id from the upload session.

Returns

A list of results from the user function.

subscribe(atom, id)

@spec subscribe(subscription_scope(), term()) :: String.t()

Subscribes the current process to a Rindle PubSub topic for a supported scope.

Supported scopes are :variant, :asset, and :upload_session. The returned topic string can be passed back to unsubscribe/1 later.

unsubscribe(topic)

@spec unsubscribe(String.t()) :: :ok

Unsubscribes the current process from a topic returned by subscribe/2.