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.
For resumable browser uploads against a mounted Rindle.Upload.TusPlug,
use allow_tus_upload/4 and keep consume_uploaded_entries/3 as the
completion gate. For full Phoenix / LiveView router, parser, CORS, and
client-uploader setup, see guides/resumable_uploads.md:
socket =
Rindle.LiveView.allow_tus_upload(socket, :video, MyApp.VideoProfile,
path: "/uploads/tus",
secret_key_base:
Application.compile_env!(:my_app, MyAppWeb.Endpoint)[:secret_key_base],
accept: ~w(.mp4),
max_entries: 1
)
Summary
Functions
Configures a LiveView external upload backed by
Rindle.Streaming.create_direct_upload/2.
Configures a LiveView external upload backed by Rindle's tus edge.
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 | :provider_asset | :upload_session
Functions
@spec allow_direct_upload(Phoenix.LiveView.Socket.t(), atom(), module(), keyword()) :: Phoenix.LiveView.Socket.t()
Configures a LiveView external upload backed by
Rindle.Streaming.create_direct_upload/2.
Requires a :cors_origin option as either a binary origin string or a
1-arity function that receives the socket.
@spec allow_tus_upload(Phoenix.LiveView.Socket.t(), atom(), module(), keyword()) :: Phoenix.LiveView.Socket.t()
Configures a LiveView external upload backed by Rindle's tus edge.
Requires:
:path- the mounted tus route, such as"/uploads/tus":secret_key_base- the same secret used to mountRindle.Upload.TusPlug
Optional:
:actor- either a binary or a 1-arity function receiving the socket. When present, the value is embedded into the signed tus token so an adopter-configured resume authorizer can enforce same-user resume.
For full Phoenix / LiveView router, parser, CORS, and client-uploader
setup, see guides/resumable_uploads.md.
@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.