View Source Beacon.MediaLibrary (Beacon v0.3.3)

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

change_asset(asset, attrs \\ %{})

@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{}}

change_derivation(asset, attrs \\ %{})

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

Examples

iex> change_asset(asset)
%Ecto.Changeset{data: %Asset{}}

count_assets(site, opts \\ [])

@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.

get_asset_by(site, clauses)

@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{}

is_image?(map)

@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

list_assets(site, opts \\ [])

@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.

save_asset(metadata)

@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.

save_asset!(metadata)

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

search(site, query)

Search assets by file name.

send_to_cdns(metadata)

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

soft_delete(asset)

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

Soft deletes a asset.

Examples

iex> soft_delete(asset)
{:ok, %Asset{}}

srcset_for_image(asset, sources)

@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.

upload(metadata)

Uploads a given UploadMetadata and runs the :upload_asset lifecycle.

Runs multiple steps:

url_for(asset)

@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.

url_for(asset, provider_key)

@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.

urls_for(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.