PgLargeObjects.UploadWriter (PgLargeObjects v0.2.1)

View Source

LiveView UploadWriter streaming data to Postgres large object.

This module can be used with Phoenix.LiveView.allow_upload/3 to specify that file uploads by clients should be streamed straight to large objects in the database. Pass the :repo option in the second element of the tuple returned by the function passed to :writer to indicate which database the object should be created in, e.g.:

socket
|> allow_upload(:avatar,
  accept: :any,
  writer: fn _name, _entry, _socket ->
    {PgLargeObjects.UploadWriter, repo: MyApp.Repo}
  end
)

The object ID of the uploaded file is available in the meta data available to the callback given to Phoenix.LiveView.consume_uploaded_entries/3:

consume_uploaded_entries(socket, :photo, fn meta, _entry ->
  %{object_id: object_id} = meta

  # Store `object_id` in database to retain handle to uploaded data.

  {:ok, nil}
end)

See Phoenix.LiveView.UploadWriter for further information.