Fix deletion in subfolders

This commit is contained in:
Roman Chvanikov 2019-12-15 15:46:58 +03:00
parent a8fa56a80f
commit 5ec5d1ee0f
5 changed files with 77 additions and 24 deletions

View File

@ -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

View File

@ -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),

View File

@ -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

View File

@ -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

View File

@ -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