From 7f78bd7e376b3f1c9d6bbf1420d7c324b4508beb Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 28 Jan 2020 16:38:44 +0300 Subject: [PATCH 1/4] Fix __MODULE__ usage --- lib/pleroma/workers/attachments_cleanup_worker.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index a738df90a..ce6998a54 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -73,7 +73,7 @@ def perform( _ -> "" end - base_url = Pleroma.Config.get([__MODULE__, :base_url], Pleroma.Web.base_url()) + base_url = Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()) file_path = String.trim_leading(href, "#{base_url}/#{prefix}") From dea968463de1139f0d6b6ff59298b1920f58f2f3 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 28 Jan 2020 16:52:20 +0300 Subject: [PATCH 2/4] Add logging --- lib/pleroma/workers/attachments_cleanup_worker.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index ce6998a54..c6a033e9c 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -8,6 +8,8 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do alias Pleroma.Object alias Pleroma.Repo + require Logger + use Pleroma.Workers.WorkerHelper, queue: "attachments_cleanup" @impl Oban.Worker @@ -77,6 +79,7 @@ def perform( file_path = String.trim_leading(href, "#{base_url}/#{prefix}") + Logger.warn("Deleting file #{file_path} (orig: #{href}, base_url: #{base_url})") uploader.delete_file(file_path) end From 804a702f97e7294feb3d4bdb527a461c40c05d80 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 28 Jan 2020 17:07:46 +0300 Subject: [PATCH 3/4] Fix base_url string trimming --- lib/pleroma/workers/attachments_cleanup_worker.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index c6a033e9c..4f7a1a193 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -75,7 +75,11 @@ def perform( _ -> "" end - base_url = Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()) + base_url = + String.trim_trailing( + Pleroma.Config.get([Pleroma.Upload, :base_url], Pleroma.Web.base_url()), + "/" + ) file_path = String.trim_leading(href, "#{base_url}/#{prefix}") From d18a2e4e360bd6e630101a7dd2f4ae0902ac43a1 Mon Sep 17 00:00:00 2001 From: Roman Chvanikov Date: Tue, 28 Jan 2020 17:22:24 +0300 Subject: [PATCH 4/4] Add test for custom base_url when deleting an attachment --- .../workers/attachments_cleanup_worker.ex | 3 -- test/object_test.exs | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/workers/attachments_cleanup_worker.ex b/lib/pleroma/workers/attachments_cleanup_worker.ex index 4f7a1a193..2cbc6b64d 100644 --- a/lib/pleroma/workers/attachments_cleanup_worker.ex +++ b/lib/pleroma/workers/attachments_cleanup_worker.ex @@ -8,8 +8,6 @@ defmodule Pleroma.Workers.AttachmentsCleanupWorker do alias Pleroma.Object alias Pleroma.Repo - require Logger - use Pleroma.Workers.WorkerHelper, queue: "attachments_cleanup" @impl Oban.Worker @@ -83,7 +81,6 @@ def perform( file_path = String.trim_leading(href, "#{base_url}/#{prefix}") - Logger.warn("Deleting file #{file_path} (orig: #{href}, base_url: #{base_url})") uploader.delete_file(file_path) end diff --git a/test/object_test.exs b/test/object_test.exs index 9b4e6f0bf..c6b2bc399 100644 --- a/test/object_test.exs +++ b/test/object_test.exs @@ -177,6 +177,39 @@ test "with objects that have legacy data.url attribute" do assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") end + + test "With custom base_url" do + Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local) + Pleroma.Config.put([Pleroma.Upload, :base_url], "https://sub.domain.tld/dir/") + + file = %Plug.Upload{ + content_type: "image/jpg", + path: Path.absname("test/fixtures/image.jpg"), + filename: "an_image.jpg" + } + + user = insert(:user) + + {:ok, %Object{} = attachment} = + Pleroma.Web.ActivityPub.ActivityPub.upload(file, actor: user.ap_id) + + %{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} = + note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}}) + + uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) + + path = href |> Path.dirname() |> Path.basename() + + assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}") + + Object.delete(note) + + ObanHelpers.perform(all_enqueued(worker: Pleroma.Workers.AttachmentsCleanupWorker)) + + assert Object.get_by_id(attachment.id) == nil + + assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") + end end describe "normalizer" do