diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/object.ex | 34 | ||||
-rw-r--r-- | lib/pleroma/web/media_proxy/invalidation.ex | 26 | ||||
-rw-r--r-- | lib/pleroma/web/media_proxy/invalidations/http.ex | 40 | ||||
-rw-r--r-- | lib/pleroma/web/media_proxy/invalidations/script.ex | 41 | ||||
-rw-r--r-- | lib/pleroma/workers/attachments_cleanup_worker.ex | 49 |
5 files changed, 30 insertions, 160 deletions
diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 546c4ea01..ab16bf2db 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -9,13 +9,11 @@ defmodule Pleroma.Object do import Ecto.Changeset alias Pleroma.Activity - alias Pleroma.Config alias Pleroma.Object alias Pleroma.Object.Fetcher alias Pleroma.ObjectTombstone alias Pleroma.Repo alias Pleroma.User - alias Pleroma.Workers.AttachmentsCleanupWorker require Logger @@ -190,37 +188,27 @@ defmodule Pleroma.Object do def delete(%Object{data: %{"id" => id}} = object) do with {:ok, _obj} = swap_object_with_tombstone(object), deleted_activity = Activity.delete_all_by_object_ap_id(id), - {:ok, _} <- invalid_object_cache(object) do - cleanup_attachments( - Config.get([:instance, :cleanup_attachments]), - %{"object" => object} - ) + {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"), + {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do + with true <- Pleroma.Config.get([:instance, :cleanup_attachments]) do + {:ok, _} = + Pleroma.Workers.AttachmentsCleanupWorker.enqueue("cleanup_attachments", %{ + "object" => object + }) + end {:ok, object, deleted_activity} end end - @spec cleanup_attachments(boolean(), %{required(:object) => map()}) :: - {:ok, Oban.Job.t() | nil} - def cleanup_attachments(true, %{"object" => _} = params) do - AttachmentsCleanupWorker.enqueue("cleanup_attachments", params) - end - - def cleanup_attachments(_, _), do: {:ok, nil} - - def prune(%Object{data: %{"id" => _id}} = object) do + def prune(%Object{data: %{"id" => id}} = object) do with {:ok, object} <- Repo.delete(object), - {:ok, _} <- invalid_object_cache(object) do + {:ok, true} <- Cachex.del(:object_cache, "object:#{id}"), + {:ok, _} <- Cachex.del(:web_resp_cache, URI.parse(id).path) do {:ok, object} end end - def invalid_object_cache(%Object{data: %{"id" => id}}) do - with {:ok, true} <- Cachex.del(:object_cache, "object:#{id}") do - Cachex.del(:web_resp_cache, URI.parse(id).path) - end - end - def set_cache(%Object{data: %{"id" => ap_id}} = object) do Cachex.put(:object_cache, "object:#{ap_id}", object) {:ok, object} diff --git a/lib/pleroma/web/media_proxy/invalidation.ex b/lib/pleroma/web/media_proxy/invalidation.ex deleted file mode 100644 index c037ff13e..000000000 --- a/lib/pleroma/web/media_proxy/invalidation.ex +++ /dev/null @@ -1,26 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.MediaProxy.Invalidation do - @moduledoc false - - @callback purge(list(String.t()), map()) :: {:ok, String.t()} | {:error, String.t()} - - alias Pleroma.Config - - @spec purge(list(String.t())) :: {:ok, String.t()} | {:error, String.t()} - def purge(urls) do - [:media_proxy, :invalidation, :enabled] - |> Config.get() - |> do_purge(urls) - end - - defp do_purge(true, urls) do - provider = Config.get([:media_proxy, :invalidation, :provider]) - options = Config.get(provider) - provider.purge(urls, options) - end - - defp do_purge(_, _), do: :ok -end diff --git a/lib/pleroma/web/media_proxy/invalidations/http.ex b/lib/pleroma/web/media_proxy/invalidations/http.ex deleted file mode 100644 index 07248df6e..000000000 --- a/lib/pleroma/web/media_proxy/invalidations/http.ex +++ /dev/null @@ -1,40 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.MediaProxy.Invalidation.Http do - @moduledoc false - @behaviour Pleroma.Web.MediaProxy.Invalidation - - require Logger - - @impl Pleroma.Web.MediaProxy.Invalidation - def purge(urls, opts) do - method = Map.get(opts, :method, :purge) - headers = Map.get(opts, :headers, []) - options = Map.get(opts, :options, []) - - Logger.debug("Running cache purge: #{inspect(urls)}") - - Enum.each(urls, fn url -> - with {:error, error} <- do_purge(method, url, headers, options) do - Logger.error("Error while cache purge: url - #{url}, error: #{inspect(error)}") - end - end) - - {:ok, "success"} - end - - defp do_purge(method, url, headers, options) do - case Pleroma.HTTP.request(method, url, "", headers, options) do - {:ok, %{status: status} = env} when 400 <= status and status < 500 -> - {:error, env} - - {:error, error} = error -> - error - - _ -> - {:ok, "success"} - end - end -end diff --git a/lib/pleroma/web/media_proxy/invalidations/script.ex b/lib/pleroma/web/media_proxy/invalidations/script.ex deleted file mode 100644 index 6be782132..000000000 --- a/lib/pleroma/web/media_proxy/invalidations/script.ex +++ /dev/null @@ -1,41 +0,0 @@ -# Pleroma: A lightweight social networking server -# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> -# SPDX-License-Identifier: AGPL-3.0-only - -defmodule Pleroma.Web.MediaProxy.Invalidation.Script do - @moduledoc false - - @behaviour Pleroma.Web.MediaProxy.Invalidation - - require Logger - - @impl Pleroma.Web.MediaProxy.Invalidation - def purge(urls, %{script_path: script_path} = _options) do - args = - urls - |> List.wrap() - |> Enum.uniq() - |> Enum.join(" ") - - path = Path.expand(script_path) - - Logger.debug("Running cache purge: #{inspect(urls)}, #{path}") - - case do_purge(path, [args]) do - {result, exit_status} when exit_status > 0 -> - Logger.error("Error while cache purge: #{inspect(result)}") - {:error, inspect(result)} - - _ -> - {:ok, "success"} - end - end - - def purge(_, _), do: {:error, "not found script path"} - - defp do_purge(path, args) do - System.cmd(path, args) - rescue - error -> {inspect(error), 1} - end -end diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index 49352db2a..3c5820a86 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -27,20 +27,8 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do uploader = Pleroma.Config.get([Pleroma.Upload, :uploader]) - prefix = - case Pleroma.Config.get([Pleroma.Upload, :base_url]) do - nil -> "media" - _ -> "" - end - - base_url = - String.trim_trailing( - Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()), - "/" - ) - # find all objects for copies of the attachments, name and actor doesn't matter here - object_ids_and_hrefs = + delete_ids = from(o in Object, where: fragment( @@ -79,28 +67,29 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do |> Enum.map(fn {href, %{id: id, count: count}} -> # only delete files that have single instance with 1 <- count do - href - |> String.trim_leading("#{base_url}/#{prefix}") - |> uploader.delete_file() + prefix = + case Pleroma.Config.get([Pleroma.Upload, :base_url]) do + nil -> "media" + _ -> "" + end - {id, href} - else - _ -> {id, nil} - end - end) + base_url = + String.trim_trailing( + Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()), + "/" + ) - object_ids = Enum.map(object_ids_and_hrefs, fn {id, _} -> id end) + file_path = String.trim_leading(href, "#{base_url}/#{prefix}") - from(o in Object, where: o.id in ^object_ids) - |> Repo.delete_all() + uploader.delete_file(file_path) + end - object_ids_and_hrefs - |> Enum.filter(fn {_, href} -> not is_nil(href) end) - |> Enum.map(&elem(&1, 1)) - |> Pleroma.Web.MediaProxy.Invalidation.purge() + id + end) - {:ok, :success} + from(o in Object, where: o.id in ^delete_ids) + |> Repo.delete_all() end - def perform(%{"op" => "cleanup_attachments", "object" => _object}, _job), do: {:ok, :skip} + def perform(%{"op" => "cleanup_attachments", "object" => _object}, _job), do: :ok end |