2020-11-26 10:41:13 +00:00
|
|
|
defmodule Mobilizon.Service.CleanOrphanMedia do
|
|
|
|
@moduledoc """
|
|
|
|
Service to clean orphan media
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Medias
|
|
|
|
alias Mobilizon.Medias.Media
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Clean orphan media
|
|
|
|
|
|
|
|
Remove media that is not attached to an entity, such as media uploads that were never used in entities.
|
|
|
|
|
|
|
|
Options:
|
|
|
|
* `grace_period` how old in hours can the media be before it's taken into account for deletion
|
|
|
|
* `dry_run` just return the media that would have been deleted, don't actually delete it
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@spec clean(Keyword.t()) :: {:ok, list(Media.t())}
|
2020-11-26 10:41:13 +00:00
|
|
|
def clean(opts \\ []) do
|
2021-12-15 11:58:31 +00:00
|
|
|
medias = Medias.find_media_to_clean(opts)
|
2020-11-26 10:41:13 +00:00
|
|
|
|
|
|
|
if Keyword.get(opts, :dry_run, false) do
|
|
|
|
{:ok, medias}
|
|
|
|
else
|
2021-08-22 14:17:20 +00:00
|
|
|
Enum.each(medias, fn media_list ->
|
|
|
|
Enum.each(media_list, fn media ->
|
|
|
|
Medias.delete_media(media, ignore_file_not_found: true)
|
|
|
|
end)
|
2020-11-26 10:41:13 +00:00
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, medias}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|