View Source Beacon.MediaLibrary (Beacon v0.2.1)

Provides functions to upload and serve assets.

Summary

Functions

Returns an %Ecto.Changeset{} for tracking asset changes.

Returns an %Ecto.Changeset{} for updating an Asset's :usage_tag and/or :source_id.

Counts the total number of assets based on the amount of pages.

Gets a single asset by clauses.

Returns true if the given Asset is an image.

Returns the list of all uploaded assets of site.

This functions runs only the local persistence step of upload/1.

Same as save_asset/1 but raises an error if unsuccessful.

Search assets by file name.

This functions runs only the external upload step of upload/1.

Soft deletes a asset.

For a given asset and list of acceptable usage tags, returns a srcset for use in templates.

Uploads a given UploadMetadata and runs the :upload_asset lifecycle.

Returns a URL for a given asset.

Uses the given provider to determine the URL for a given asset.

Returns a list of all URLs to the given Asset.

Functions

Link to this function

change_asset(asset, attrs \\ %{})

View Source
@spec change_asset(Beacon.MediaLibrary.Asset.t(), map()) :: Changeset.t()
@spec change_asset(Beacon.MediaLibrary.Asset.t(), map()) :: Changeset.t()

Returns an %Ecto.Changeset{} for tracking asset changes.

Examples

iex> change_asset(asset)
%Ecto.Changeset{data: %Asset{}}
Link to this function

change_derivation(asset, attrs \\ %{})

View Source

Returns an %Ecto.Changeset{} for updating an Asset's :usage_tag and/or :source_id.

Examples

iex> change_asset(asset)
%Ecto.Changeset{data: %Asset{}}
Link to this function

count_assets(site, opts \\ [])

View Source
@spec count_assets(
  Beacon.Types.Site.t(),
  keyword()
) :: non_neg_integer()

Counts the total number of assets based on the amount of pages.

Options

  • :query - filter rows count by query. Defaults to nil, doesn't filter query.
Link to this function

get_asset_by(site, clauses)

View Source
@spec get_asset_by(
  Beacon.Types.Site.t(),
  keyword()
) :: Beacon.MediaLibrary.Asset.t() | nil

Gets a single asset by clauses.

Examples

iex> get_asset_by(site, file_name: "logo.webp")
%Asset{}
@spec is_image?(Beacon.MediaLibrary.Asset.t()) :: boolean()

Returns true if the given Asset is an image.

Accepted filetypes are .jpg .jpeg .png .gif .bmp .tif .tiff .webp

Link to this function

list_assets(site, opts \\ [])

View Source
@spec list_assets(
  Beacon.Types.Site.t(),
  keyword()
) :: [Beacon.MediaLibrary.Asset.t()]

Returns the list of all uploaded assets of site.

Options

  • :per_page - limit how many records are returned, or pass :infinity to return all records. Defaults to 20.
  • :page - returns records from a specfic page. Defaults to 1.
  • :query - search assets by file name. Defaults to nil, doesn't filter query.
  • :preloads - a list of preloads to load. Defaults to [:thumbnail].
  • :sort - column in which the result will be ordered by. Defaults to :file_name. Allowed values: :file_name, :media_type, :inserted_at, :updated_at.
@spec save_asset(Beacon.MediaLibrary.UploadMetadata.t()) ::
  {:ok, Beacon.MediaLibrary.UploadMetadata.t()} | {:error, Changeset.t()}

This functions runs only the local persistence step of upload/1.

Same as save_asset/1 but raises an error if unsuccessful.

Search assets by file name.

This functions runs only the external upload step of upload/1.

@spec soft_delete(Beacon.MediaLibrary.Asset.t()) ::
  {:ok, Beacon.MediaLibrary.Asset.t()} | :error

Soft deletes a asset.

Examples

iex> soft_delete(asset)
{:ok, %Asset{}}
Link to this function

srcset_for_image(asset, sources)

View Source
@spec srcset_for_image(Beacon.MediaLibrary.Asset.t(), [String.t()]) :: [String.t()]

For a given asset and list of acceptable usage tags, returns a srcset for use in templates.

Uploads a given UploadMetadata and runs the :upload_asset lifecycle.

Runs multiple steps:

@spec url_for(nil) :: nil
@spec url_for(Beacon.MediaLibrary.Asset.t()) :: String.t()

Returns a URL for a given asset.

If multiple URLs exist due to various providers, only the first will be returned.

Link to this function

url_for(asset, provider_key)

View Source
@spec url_for(nil, atom()) :: nil
@spec url_for(Beacon.MediaLibrary.Asset.t(), atom()) :: String.t()

Uses the given provider to determine the URL for a given asset.

@spec urls_for(Beacon.MediaLibrary.Asset.t()) :: [String.t()]

Returns a list of all URLs to the given Asset.

The number of URLs depends on how many providers are configured for the media type.