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
endThe :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
@type consume_func() :: (Phoenix.LiveView.UploadEntry.t(), map() -> consume_result())
@type subscription_scope() :: :variant | :asset | :upload_session
Functions
@spec allow_upload(Phoenix.LiveView.Socket.t(), atom(), module(), keyword()) :: Phoenix.LiveView.Socket.t()
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 socketname- The upload name (atom), e.g.:avatarprofile- The Rindle profile module to use for storage/validationopts- Options passed through to the LiveView upload configuration (for example:accept,:max_entries,:max_file_size). The:externalkey is set by Rindle and should not be provided.
Returns
The updated socket with the upload configured.
@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 socketname- The upload name (atom)func- A 2-arity functionfn entry, meta -> resultcalled for each completed entry. Themetamap includes:session_idand:asset_idfrom the upload session.
Returns
A list of results from the user function.
@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.
@spec unsubscribe(String.t()) :: :ok
Unsubscribes the current process from a topic returned by subscribe/2.