Skip to content

Commit

Permalink
add customizable asset delete callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
ddink committed Oct 7, 2024
1 parent bf09fb1 commit dda1bfa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/beacon/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ defmodule Beacon.Config do
[
{identifier :: atom(), fun :: (Ecto.Schema.t(), Beacon.MediaLibrary.UploadMetadata.t() -> {:cont, any()} | {:halt, Exception.t()})}
]}
| {:soft_delete_asset,
[
{identifier :: atom(), fun :: (Ecto.Schema.t() -> {:cont, any()} | {:halt, Exception.t()})}
]}

@typedoc """
Add extra fields to pages.
Expand Down
16 changes: 16 additions & 0 deletions lib/beacon/lifecycle/asset.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ defmodule Beacon.Lifecycle.Asset do
end

def variant_480w(asset, _metadata), do: {:cont, asset}

def delete_uploaded_asset(%MediaLibrary.Asset{site: site, media_type: media_type} = asset) do
config =
site
|> Beacon.Config.fetch!()
|> Beacon.Config.config_for_media_type(media_type)
|> Enum.into(%{})

if Map.get(config, :delete_asset_callbacks) do
Enum.reduce(config.delete_asset_callbacks, asset, fn
callback, asset -> callback.(asset)
end)
end

{:cont, asset}
end
end
8 changes: 6 additions & 2 deletions lib/beacon/media_library.ex
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,12 @@ defmodule Beacon.MediaLibrary do
)

case update do
{1, _} -> {:ok, repo(asset).reload(asset)}
_ -> :error
{1, _} ->
Lifecycle.Asset.delete_uploaded_asset(asset)
{:ok, repo(asset).reload(asset)}

_ ->
:error
end
end
end

0 comments on commit dda1bfa

Please sign in to comment.