Beacon.MediaLibrary (Beacon v0.5.0)

View Source

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.

Returns information about the uploaded file.

Gets a single asset by clauses.

Returns true if the given Asset is an image.

Returns the list of all uploaded assets of site.

Returns the contents of an uploaded file as a binary.

Same as read_binary/1 but raises if the file is not found.

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 the URL of the first registered provider for a given asset

Returns the URL for the given asset registered on provider under provider_key.

Returns the URL of a media file_name previously uploaded to the Media Library.

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.

Example

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.

Example

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.

file_stat(path, node)

@spec file_stat(String.t(), Node.t()) :: {:ok, File.Stat.t()} | {:error, any()}

Returns information about the uploaded file.

See File.stat/2 for more information.

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.

Example

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

read_binary(metadata)

@spec read_binary(Beacon.MediaLibrary.UploadMetadata.t()) ::
  {:ok, binary()} | {:error, any()}

Returns the contents of an uploaded file as a binary.

read_binary!(metadata)

@spec read_binary!(Beacon.MediaLibrary.UploadMetadata.t()) :: binary()

Same as read_binary/1 but raises if the file is not found.

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()}

Soft deletes a asset.

Example

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(Beacon.MediaLibrary.Asset.t()) :: String.t() | nil

Returns the URL of the first registered provider for a given asset

Note that if multiple URLs exist due to various providers, only the first will be returned.

url_for(asset, provider_key)

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

Returns the URL for the given asset registered on provider under provider_key.

Example

Given a site config with a registered provider:

[
  site: :my_site,
  assets: [
    {"image/*", [providers: [MyApp.Provider.CDN], validations: []]}
  ],
  ...
]

And that provider registers itself under the "cnd" key:

defmodule MyApp.Provider.CDN do
  def provider_key, do: "cdn"
end

Then you can fetch the URL from that provider with:

iex> url_for(asset, "cdn")
"https://assets.mysite.com/logo-361ae598-4150-4ce5-814d-363c2afd1993.webp"

url_for_asset(site, file_name)

@spec url_for_asset(Beacon.Types.Site.t(), String.t()) :: String.t()

Returns the URL of a media file_name previously uploaded to the Media Library.

This function is a convenience for get_asset_by/2 and url_for/1, see their docs for more info.

Example

iex> url_for_asset(:my_site, "logo.webp")
"https://mysite.com/__beacon_media__/logo.webp"

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.