Return the complete `upload` from Pleroma.Uploaders.put_file/2
ci/woodpecker/pr/woodpecker Pipeline is pending Details

To move more logic to the Uploader modules, it's better to keep the same
`upload` and manipulate that while going through the upload pipeline, so
that's how it's now done.

Since we expect the `upload`, I removed the option for returning just `:ok`
from the uploaders.

It was possible to bypass the logic in the `get_url` function, but that was
unused. Since it makes even less sense when returning `upload`, I removed that
In a later stage I want to return the url from the uploader module, which
should obsolete this function.

I saw a warning from `Pleroma.Upload.Filter.MogrifyTest` which I now fixed.
This commit is contained in:
ilja 2023-03-26 13:52:22 +02:00
parent 5db2e998c5
commit 2edb0944ba
8 changed files with 23 additions and 30 deletions

View File

@ -87,7 +87,7 @@ defmodule Pleroma.Upload do
{_, true} <-
{:description_limit,
String.length(description) <= Config.get([:instance, :description_limit])},
{:ok, file} <- Pleroma.Uploaders.Uploader.put_file(uploader, upload) do
{:ok, upload} <- Pleroma.Uploaders.Uploader.put_file(uploader, upload) do
{:ok,
%{
"id" => Utils.generate_object_id(),
@ -97,7 +97,7 @@ defmodule Pleroma.Upload do
%{
"type" => "Link",
"mediaType" => upload.content_type,
"href" => get_url(upload, file)
"href" => get_url(upload.name, upload.path)
}
|> Maps.put_if_present("width", upload.width)
|> Maps.put_if_present("height", upload.height)
@ -110,9 +110,7 @@ defmodule Pleroma.Upload do
{:error, :description_too_long}
{:error, error} ->
Logger.error(
"#{__MODULE__} store (using #{inspect(uploader)}) failed: #{inspect(error)}"
)
Logger.error("#{__MODULE__} store (using #{inspect(uploader)}) failed: #{inspect(error)}")
{:error, error}
end
@ -214,7 +212,7 @@ defmodule Pleroma.Upload do
tmp_path
end
defp get_url(%__MODULE__{name: name}, {:file, path}) do
defp get_url(name, path) do
base_url = base_url()
path =
@ -229,7 +227,5 @@ defmodule Pleroma.Upload do
|> Path.join()
end
defp get_url(_upload, {:url, url}), do: url
def base_url(), do: Config.get([__MODULE__, :uploader]).base_url()
end

View File

@ -30,7 +30,7 @@ defmodule Pleroma.Uploaders.Local do
File.cp!(upload.tempfile, result_file)
end
:ok
{:ok, upload}
end
def upload_path do

View File

@ -47,7 +47,7 @@ defmodule Pleroma.Uploaders.S3 do
case ExAws.request(op) do
{:ok, _} ->
{:ok, {:file, s3_name}}
{:ok, Map.put(upload, :path, s3_name)}
error ->
Logger.error("#{__MODULE__}: #{inspect(error)}")

View File

@ -34,26 +34,23 @@ defmodule Pleroma.Uploaders.Uploader do
* `:wait_callback` will wait for an http post request at `/api/pleroma/upload_callback/:upload_path` and call the uploader's `http_callback/3` method.
"""
@type file_spec :: {:file | :url, String.t()}
@callback put_file(upload :: struct()) ::
:ok | {:ok, file_spec()} | {:error, String.t()} | :wait_callback
:ok | {:ok, Pleroma.Upload} | {:error, String.t()} | :wait_callback
@callback delete_file(file :: String.t()) :: :ok | {:error, String.t()}
@callback base_url() :: String.t()
@callback http_callback(Plug.Conn.t(), Map.t()) ::
{:ok, Plug.Conn.t()}
| {:ok, Plug.Conn.t(), file_spec()}
@callback http_callback(Plug.Conn.t(), Map.t(), Pleroma.Upload) ::
{:ok, Plug.Conn.t(), Pleroma.Upload}
| {:error, Plug.Conn.t(), String.t()}
@optional_callbacks http_callback: 2
@optional_callbacks http_callback: 3
@spec put_file(module(), upload :: struct()) :: {:ok, file_spec()} | {:error, String.t()}
@spec put_file(module(), upload :: struct()) :: {:ok, Pleroma.Upload} | {:error, String.t()}
def put_file(uploader, upload) do
case uploader.put_file(upload) do
:ok -> {:ok, {:file, upload.path}}
{:ok, %Pleroma.Upload{}} = ok -> ok
:wait_callback -> handle_callback(uploader, upload)
{:ok, _} = ok -> ok
{:error, _} = error -> error
end
end
@ -63,10 +60,10 @@ defmodule Pleroma.Uploaders.Uploader do
receive do
{__MODULE__, pid, conn, params} ->
case uploader.http_callback(conn, params) do
{:ok, conn, ok} ->
case uploader.http_callback(conn, params, upload) do
{:ok, conn, upload} ->
send(pid, {__MODULE__, conn})
{:ok, ok}
{:ok, upload}
{:error, conn, error} ->
send(pid, {__MODULE__, conn})

View File

@ -9,7 +9,7 @@ defmodule Pleroma.Upload.Filter.MogrifyTest do
alias Pleroma.Upload.Filter
test "apply mogrify filter" do
clear_config(Filter.Mogrify, args: [{"tint", "40"}])
clear_config([Filter.Mogrify, :args], [{"tint", "40"}])
File.cp!(
"test/fixtures/image.jpg",

View File

@ -37,8 +37,8 @@ defmodule Pleroma.UploadTest do
describe "Tried storing a file when http callback response success result" do
defmodule TestUploaderSuccess do
def http_callback(conn, _params),
do: {:ok, conn, {:file, "post-process-file.jpg"}}
def http_callback(conn, _params, upload),
do: {:ok, conn, Map.put(upload, :path, "post-process-file.jpg")}
def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
@ -74,7 +74,7 @@ defmodule Pleroma.UploadTest do
describe "Tried storing a file when http callback response error" do
defmodule TestUploaderError do
def http_callback(conn, _params), do: {:error, conn, "Errors"}
def http_callback(conn, _params, _upload), do: {:error, conn, "Errors"}
def put_file(upload), do: TestUploaderBase.put_file(upload, __MODULE__)
end

View File

@ -24,7 +24,7 @@ defmodule Pleroma.Uploaders.LocalTest do
tempfile: Path.absname("test/fixtures/image_tmp.jpg")
}
assert Local.put_file(file) == :ok
assert Local.put_file(file) == {:ok, file}
assert Path.join([Local.upload_path(), file_path])
|> File.exists?()
@ -43,8 +43,8 @@ defmodule Pleroma.Uploaders.LocalTest do
tempfile: Path.absname("test/fixtures/image_tmp.jpg")
}
:ok = Local.put_file(file)
local_path = Path.join([Local.upload_path(), file_path])
{:ok, file} = Local.put_file(file)
local_path = Path.join([Local.upload_path(), file.path])
assert File.exists?(local_path)
Local.delete_file(file_path)

View File

@ -67,7 +67,7 @@ defmodule Pleroma.Uploaders.S3Test do
test "save file", %{file_upload: file_upload} do
with_mock ExAws, request: fn _ -> {:ok, :ok} end do
assert S3.put_file(file_upload) == {:ok, {:file, "test_folder/image-tet.jpg"}}
assert S3.put_file(file_upload) == {:ok, file_upload}
end
end