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}} <- with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
Upload.store(pic.file, type: key, description: pic.alt) do 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 end
else else
Logger.debug("No picture upload")
args args
end end
end) end)
@ -200,12 +202,17 @@ defmodule Mobilizon.GraphQL.Resolvers.Group do
{:error, :file_too_large} -> {:error, :file_too_large} ->
{:error, dgettext("errors", "The provided picture is too heavy")} {: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 case API.Groups.update_group(args) do
{:ok, _activity, %Actor{type: :Group} = group} -> {:ok, _activity, %Actor{type: :Group} = group} ->
{:ok, 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")} {:error, dgettext("errors", "Failed to update the group")}
end end
end end

View file

@ -72,9 +72,9 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
) )
query query
|> Repo.all() |> Repo.all(timeout: :infinity)
|> Enum.filter(fn %Media{file: %File{url: url}} -> |> 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) end)
|> Enum.chunk_by(fn %Media{file: %File{url: url}} -> |> Enum.chunk_by(fn %Media{file: %File{url: url}} ->
url url
@ -91,7 +91,7 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
@spec is_media_orphan?(Media.t(), DateTime.t()) :: boolean() @spec is_media_orphan?(Media.t(), DateTime.t()) :: boolean()
def is_media_orphan?(%Media{id: media_id}, expiration_date) do def is_media_orphan?(%Media{id: media_id}, expiration_date) do
query = media_query =
from(m in Media, from(m in Media,
as: :media, as: :media,
distinct: true, distinct: true,
@ -103,6 +103,13 @@ defmodule Mobilizon.Service.CleanOrphanMedia do
where: fragment(@union_query) 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
end end

View file

@ -137,29 +137,4 @@ defmodule Mix.Tasks.Mobilizon.Media.CleanOrphanTest do
size: 13_120 size: 13_120
} }
end 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 end