From 2b99b482581739bf584f695f64d4492ca6250c67 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 20 Nov 2021 18:30:18 +0100 Subject: [PATCH 1/4] Set database timeout to infinity when trying to detect orphan media Signed-off-by: Thomas Citharel --- lib/service/clean_orphan_media.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/service/clean_orphan_media.ex b/lib/service/clean_orphan_media.ex index b02d2953f..fd619bee2 100644 --- a/lib/service/clean_orphan_media.ex +++ b/lib/service/clean_orphan_media.ex @@ -72,7 +72,7 @@ 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) end) From 2154457be39a3c0c72385087aac048ec10501290 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 20 Nov 2021 18:30:51 +0100 Subject: [PATCH 2/4] Take profile files into account when deleting orphan media Signed-off-by: Thomas Citharel --- lib/service/clean_orphan_media.ex | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/service/clean_orphan_media.ex b/lib/service/clean_orphan_media.ex index fd619bee2..136815eb0 100644 --- a/lib/service/clean_orphan_media.ex +++ b/lib/service/clean_orphan_media.ex @@ -74,7 +74,7 @@ defmodule Mobilizon.Service.CleanOrphanMedia do query |> 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 From e8da59f4a56b39df4db97eb8944d9c7e9164ac58 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 20 Nov 2021 18:36:26 +0100 Subject: [PATCH 3/4] Cleanup clean_orphan_test.exs Signed-off-by: Thomas Citharel --- test/tasks/media/clean_orphan_test.exs | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/test/tasks/media/clean_orphan_test.exs b/test/tasks/media/clean_orphan_test.exs index e0b2f2bd3..073dc1ded 100644 --- a/test/tasks/media/clean_orphan_test.exs +++ b/test/tasks/media/clean_orphan_test.exs @@ -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 From 07a11d792c3f4c120b771b080e83703d5c4d10c3 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 20 Nov 2021 18:44:55 +0100 Subject: [PATCH 4/4] Fix updating a group's avatar and banner picture Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/group.ex | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/graphql/resolvers/group.ex b/lib/graphql/resolvers/group.ex index d0532067e..fed9d320f 100644 --- a/lib/graphql/resolvers/group.ex +++ b/lib/graphql/resolvers/group.ex @@ -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