View Source Beacon.MediaLibrary (Beacon v0.3.0)
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
@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{}}
Returns an %Ecto.Changeset{} for updating an Asset's :usage_tag and/or :source_id.
Examples
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 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
@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 specfic 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 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()} | :error
Soft deletes a asset.
Examples
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(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.
@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.