diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index c1fc6a65f..2452a7389 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -233,11 +233,19 @@ defmodule Pleroma.Object do end) end) |> Enum.map(fn {href, %{id: id, count: count}} -> - # only delete files that have a single instance + # only delete files that have single instance with 1 <- count do - href - |> Path.basename() - |> uploader.delete_file() + prefix = + case Pleroma.Config.get([Pleroma.Upload, :base_url]) do + nil -> "media" + _ -> "" + end + + base_url = Pleroma.Config.get([__MODULE__, :base_url], Pleroma.Web.base_url()) + + file_path = String.trim_leading(href, "#{base_url}/#{prefix}") + + uploader.delete_file(file_path) end id diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 9645a0de4..2e0986197 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -57,7 +57,6 @@ defmodule Pleroma.Upload do @spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()} def store(upload, opts \\ []) do - IO.inspect(upload) opts = get_opts(opts) with {:ok, upload} <- prepare_upload(upload, opts), diff --git a/lib/pleroma/uploaders/local.ex b/lib/pleroma/uploaders/local.ex index 488360684..2e6fe3292 100644 --- a/lib/pleroma/uploaders/local.ex +++ b/lib/pleroma/uploaders/local.ex @@ -37,9 +37,9 @@ defmodule Pleroma.Uploaders.Local do end @impl true - def delete_file(name) do + def delete_file(path) do upload_path() - |> Path.join(name) + |> Path.join(path) |> File.rm() |> case do :ok -> :ok diff --git a/test/object_test.exs b/test/object_test.exs index 579431524..b002c2bae 100644 --- a/test/object_test.exs +++ b/test/object_test.exs @@ -69,8 +69,14 @@ defmodule Pleroma.ObjectTest do assert cached_object.data["type"] == "Tombstone" end + end + + describe "delete attachments" do + clear_config([Pleroma.Upload]) + + test "in subdirectories" do + Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local) - test "deletes attachments" do file = %Plug.Upload{ content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), @@ -85,15 +91,51 @@ defmodule Pleroma.ObjectTest do %{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("test/uploads/#{path}") + assert {:ok, ["an_image.jpg"]} == File.ls("#{uploads_dir}/#{path}") Object.delete(note) assert Object.get_by_id(attachment.id) == nil - assert {:ok, ["an_image.jpg"]} == File.ls("test/uploads/#{path}") + assert {:ok, []} == File.ls("#{uploads_dir}/#{path}") + end + + test "with dedupe enabled" do + Pleroma.Config.put([Pleroma.Upload, :uploader], Pleroma.Uploaders.Local) + Pleroma.Config.put([Pleroma.Upload, :filters], [Pleroma.Upload.Filter.Dedupe]) + + uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads]) + + File.mkdir_p!(uploads_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]}}) + + filename = Path.basename(href) + + assert {:ok, files} = File.ls(uploads_dir) + assert filename in files + + Object.delete(note) + + assert Object.get_by_id(attachment.id) == nil + assert {:ok, files} = File.ls(uploads_dir) + refute filename in files end end diff --git a/test/uploaders/mdii_test.exs b/test/uploaders/mdii_test.exs index 809f688e9..f9271c21d 100644 --- a/test/uploaders/mdii_test.exs +++ b/test/uploaders/mdii_test.exs @@ -49,23 +49,19 @@ defmodule Pleroma.Uploaders.MDIITest do end describe "delete_file/1" do - setup do - file_upload = %Pleroma.Upload{ - name: "mdii-image.jpg", - content_type: "image/jpg", - path: "test_folder/mdii-image.jpg", - tempfile: Path.absname("test/fixtures/image_tmp.jpg") - } - - [file_upload: file_upload] - end - - test "locally stored file", %{file_upload: file_upload} do + test "locally stored file" do mock(fn %{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} -> %Tesla.Env{status: 500} end) + file_upload = %Pleroma.Upload{ + name: "mdii-image.jpg", + content_type: "image/jpg", + path: "test_folder/mdii-image1.jpg", + tempfile: Path.absname("test/fixtures/image_tmp.jpg") + } + :ok = MDII.put_file(file_upload) local_path = Path.join([Pleroma.Uploaders.Local.upload_path(), file_upload.path]) @@ -74,13 +70,21 @@ defmodule Pleroma.Uploaders.MDIITest do refute File.exists?(local_path) end - test "file not stored locally", %{file_upload: file_upload} do + test "file not stored locally" do mock(fn %{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} -> %Tesla.Env{status: 200, body: "mdii-image"} end) - MDII.put_file(file_upload) + file_upload = %Pleroma.Upload{ + name: "mdii-image.jpg", + content_type: "image/jpg", + path: "test_folder/mdii-image2.jpg", + tempfile: Path.absname("test/fixtures/image_tmp.jpg") + } + + {:ok, {:url, _}} = MDII.put_file(file_upload) + assert {:error, "enoent"} = MDII.delete_file(file_upload.path) end end