Beacon.MediaLibrary (Beacon v0.5.1)
View SourceProvides 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
@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{}}
Returns an %Ecto.Changeset{} for updating an Asset's :usage_tag and/or :source_id.
Example
iex> change_asset(asset)
%Ecto.Changeset{data: %Asset{}}
@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 tonil, doesn't filter query.
@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.
@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{}
@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
@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:infinityto return all records. Defaults to 20.:page- returns records from a specific page. Defaults to 1.:query- search assets by file name. Defaults tonil, 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 read_binary(Beacon.MediaLibrary.UploadMetadata.t()) :: {:ok, binary()} | {:error, any()}
Returns the contents of an uploaded file as a binary.
@spec read_binary!(Beacon.MediaLibrary.UploadMetadata.t()) :: binary()
Same as read_binary/1 but raises if the file is not found.
@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.
@spec save_asset!(Beacon.MediaLibrary.UploadMetadata.t()) :: Beacon.MediaLibrary.UploadMetadata.t()
Same as save_asset/1 but raises an error if unsuccessful.
@spec search(Beacon.Types.Site.t(), String.t()) :: [Beacon.MediaLibrary.Asset.t()]
Search assets by file name.
@spec send_to_cdns(Beacon.MediaLibrary.UploadMetadata.t()) :: Beacon.MediaLibrary.UploadMetadata.t()
This functions runs only the external upload step of upload/1.
@spec soft_delete(Beacon.MediaLibrary.Asset.t()) :: {:ok, Beacon.MediaLibrary.Asset.t()}
Soft deletes a asset.
Example
iex> soft_delete(asset)
{:ok, %Asset{}}
@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.
@spec upload(Beacon.MediaLibrary.UploadMetadata.t()) :: Ecto.Schema.t()
Uploads a given UploadMetadata and runs the :upload_asset lifecycle.
Runs multiple steps:
- Upload to external service (see:
Beacon.MediaLibrary.Provider) - Persist the metadata to the local database
- Run the
:upload_assetlifecycle (see:Beacon.Config.lifecycle_stage/0)
@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.
@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"
endThen 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"
@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"
@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.