Fix deletion in subfolders
This commit is contained in:
parent
a8fa56a80f
commit
5ec5d1ee0f
|
@ -233,11 +233,19 @@ defmodule Pleroma.Object do
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|> Enum.map(fn {href, %{id: id, count: count}} ->
|
|> 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
|
with 1 <- count do
|
||||||
href
|
prefix =
|
||||||
|> Path.basename()
|
case Pleroma.Config.get([Pleroma.Upload, :base_url]) do
|
||||||
|> uploader.delete_file()
|
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
|
end
|
||||||
|
|
||||||
id
|
id
|
||||||
|
|
|
@ -57,7 +57,6 @@ defmodule Pleroma.Upload do
|
||||||
|
|
||||||
@spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()}
|
@spec store(source, options :: [option()]) :: {:ok, Map.t()} | {:error, any()}
|
||||||
def store(upload, opts \\ []) do
|
def store(upload, opts \\ []) do
|
||||||
IO.inspect(upload)
|
|
||||||
opts = get_opts(opts)
|
opts = get_opts(opts)
|
||||||
|
|
||||||
with {:ok, upload} <- prepare_upload(upload, opts),
|
with {:ok, upload} <- prepare_upload(upload, opts),
|
||||||
|
|
|
@ -37,9 +37,9 @@ defmodule Pleroma.Uploaders.Local do
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def delete_file(name) do
|
def delete_file(path) do
|
||||||
upload_path()
|
upload_path()
|
||||||
|> Path.join(name)
|
|> Path.join(path)
|
||||||
|> File.rm()
|
|> File.rm()
|
||||||
|> case do
|
|> case do
|
||||||
:ok -> :ok
|
:ok -> :ok
|
||||||
|
|
|
@ -69,8 +69,14 @@ defmodule Pleroma.ObjectTest do
|
||||||
|
|
||||||
assert cached_object.data["type"] == "Tombstone"
|
assert cached_object.data["type"] == "Tombstone"
|
||||||
end
|
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{
|
file = %Plug.Upload{
|
||||||
content_type: "image/jpg",
|
content_type: "image/jpg",
|
||||||
path: Path.absname("test/fixtures/image.jpg"),
|
path: Path.absname("test/fixtures/image.jpg"),
|
||||||
|
@ -85,15 +91,51 @@ defmodule Pleroma.ObjectTest do
|
||||||
%{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
|
%{data: %{"attachment" => [%{"url" => [%{"href" => href}]}]}} =
|
||||||
note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
|
note = insert(:note, %{user: user, data: %{"attachment" => [attachment.data]}})
|
||||||
|
|
||||||
|
uploads_dir = Pleroma.Config.get!([Pleroma.Uploaders.Local, :uploads])
|
||||||
|
|
||||||
path = href |> Path.dirname() |> Path.basename()
|
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)
|
Object.delete(note)
|
||||||
|
|
||||||
assert Object.get_by_id(attachment.id) == nil
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -49,23 +49,19 @@ defmodule Pleroma.Uploaders.MDIITest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "delete_file/1" do
|
describe "delete_file/1" do
|
||||||
setup do
|
test "locally stored file" 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
|
|
||||||
mock(fn
|
mock(fn
|
||||||
%{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} ->
|
%{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} ->
|
||||||
%Tesla.Env{status: 500}
|
%Tesla.Env{status: 500}
|
||||||
end)
|
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)
|
:ok = MDII.put_file(file_upload)
|
||||||
|
|
||||||
local_path = Path.join([Pleroma.Uploaders.Local.upload_path(), file_upload.path])
|
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)
|
refute File.exists?(local_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "file not stored locally", %{file_upload: file_upload} do
|
test "file not stored locally" do
|
||||||
mock(fn
|
mock(fn
|
||||||
%{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} ->
|
%{method: :post, url: "https://mdii.sakura.ne.jp/mdii-post.cgi?jpg"} ->
|
||||||
%Tesla.Env{status: 200, body: "mdii-image"}
|
%Tesla.Env{status: 200, body: "mdii-image"}
|
||||||
end)
|
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)
|
assert {:error, "enoent"} = MDII.delete_file(file_upload.path)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue