Merge branch 'bugs' into 'main'

Orphan media fixes

See merge request framasoft/mobilizon!1115
This commit is contained in:
Thomas Citharel 2021-11-20 18:36:55 +00:00
commit 93acdeda20
3 changed files with 21 additions and 32 deletions

View File

@ -120,9 +120,11 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
Upload.store(pic.file, type: key, description: pic.alt) do
Map.put(args, key, %{"name" => name, "url" => url, "mediaType" => content_type})
Logger.debug("Uploaded #{name} to #{url}")
Map.put(args, key, %{name: name, url: url, content_type: content_type})
end
else
Logger.debug("No picture upload")
args
end
end)
@ -200,12 +202,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:error, :file_too_large} ->
{:error, dgettext("errors", "The provided picture is too heavy")}
map when is_map(map) ->
args when is_map(args) ->
case API.Groups.update_group(args) do
{:ok, _activity, %Actor{type: :Group} = group} ->
{:ok, group}
{:error, _err} ->
{:error, %Ecto.Changeset{} = changeset} ->
{:error, changeset}
{:error, err} ->
Logger.info("Failed to update group #{inspect(group_id)}")
Logger.debug(inspect(err))
{:error, dgettext("errors", "Failed to update the group")}
end
end

View File

@ -72,9 +72,9 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
)
query
|> Repo.all()
|> Repo.all(timeout: :infinity)
|> Enum.filter(fn %Media{file: %File{url: url}} ->
is_all_media_orphan?(url, expiration_date)
!url_is_also_a_profile_file?(url) && is_all_media_orphan?(url, expiration_date)
end)
|> Enum.chunk_by(fn %Media{file: %File{url: url}} ->
url
@ -91,7 +91,7 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
@spec is_media_orphan?(Media.t(), DateTime.t()) :: boolean()
def is_media_orphan?(%Media{id: media_id}, expiration_date) do
query =
media_query =
from(m in Media,
as: :media,
distinct: true,
@ -103,6 +103,13 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
where: fragment(@union_query)
)
Repo.exists?(query)
Repo.exists?(media_query)
end
@spec url_is_also_a_profile_file?(String.t()) :: nil
defp url_is_also_a_profile_file?(url) when is_binary(url) do
Actor
|> where([a], fragment("avatar->>'url'") == ^url or fragment("banner->>'url'") == ^url)
|> Repo.exists?()
end
end

View File

@ -137,29 +137,4 @@ defmodule Mix.Tasks.Mobilizon.Media.CleanOrphanTest do
size: 13_120
}
end
defp create_file do
File.cp!("test/fixtures/picture.png", "test/fixtures/picture_tmp.png")
file = %Plug.Upload{
content_type: "image/png",
path: Path.absname("test/fixtures/picture_tmp.png"),
filename: "image.png"
}
{:ok, data} = Mobilizon.Web.Upload.store(file)
%{
content_type: "image/png",
name: "image.png",
url: url
} = data
%Mobilizon.Medias.File{
name: "My Media",
url: url,
content_type: "image/png",
size: 13_120
}
end
end