ipfs: small refactor and more tests
This commit is contained in:
parent
7c1af86f97
commit
98f268e5ec
|
@ -9,16 +9,24 @@ defmodule Pleroma.Uploaders.IPFS do
|
||||||
alias Pleroma.Config
|
alias Pleroma.Config
|
||||||
alias Tesla.Multipart
|
alias Tesla.Multipart
|
||||||
|
|
||||||
@placeholder "{CID}"
|
defp get_final_url(method) do
|
||||||
def placeholder, do: @placeholder
|
|
||||||
|
|
||||||
def get_final_url(method) do
|
|
||||||
config = Config.get([__MODULE__])
|
config = Config.get([__MODULE__])
|
||||||
post_base_url = Keyword.get(config, :post_gateway_url)
|
post_base_url = Keyword.get(config, :post_gateway_url)
|
||||||
|
|
||||||
Path.join([post_base_url, method])
|
Path.join([post_base_url, method])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def put_file_endpoint() do
|
||||||
|
get_final_url("/api/v0/add")
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete_file_endpoint() do
|
||||||
|
get_final_url("/api/v0/files/rm")
|
||||||
|
end
|
||||||
|
|
||||||
|
@placeholder "{CID}"
|
||||||
|
def placeholder, do: @placeholder
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def get_file(file) do
|
def get_file(file) do
|
||||||
b_url = Pleroma.Upload.base_url()
|
b_url = Pleroma.Upload.base_url()
|
||||||
|
@ -37,9 +45,7 @@ def put_file(%Pleroma.Upload{} = upload) do
|
||||||
|> Multipart.add_content_type_param("charset=utf-8")
|
|> Multipart.add_content_type_param("charset=utf-8")
|
||||||
|> Multipart.add_file(upload.tempfile)
|
|> Multipart.add_file(upload.tempfile)
|
||||||
|
|
||||||
final_url = get_final_url("/api/v0/add")
|
case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: ["cid-version": "1"]) do
|
||||||
|
|
||||||
case Pleroma.HTTP.post(final_url, mp, [], params: ["cid-version": "1"]) do
|
|
||||||
{:ok, ret} ->
|
{:ok, ret} ->
|
||||||
case Jason.decode(ret.body) do
|
case Jason.decode(ret.body) do
|
||||||
{:ok, ret} ->
|
{:ok, ret} ->
|
||||||
|
@ -62,9 +68,7 @@ def put_file(%Pleroma.Upload{} = upload) do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def delete_file(file) do
|
def delete_file(file) do
|
||||||
final_url = get_final_url("/api/v0/files/rm")
|
case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do
|
||||||
|
|
||||||
case Pleroma.HTTP.post(final_url, "", [], params: [arg: file]) do
|
|
||||||
{:ok, %{status_code: 204}} -> :ok
|
{:ok, %{status_code: 204}} -> :ok
|
||||||
error -> {:error, inspect(error)}
|
error -> {:error, inspect(error)}
|
||||||
end
|
end
|
||||||
|
|
|
@ -25,11 +25,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
||||||
|
|
||||||
describe "get_final_url" do
|
describe "get_final_url" do
|
||||||
test "it returns the final url for put_file" do
|
test "it returns the final url for put_file" do
|
||||||
assert IPFS.get_final_url("/api/v0/add") == "http://localhost:5001/api/v0/add"
|
assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it returns the final url for delete_file" do
|
test "it returns the final url for delete_file" do
|
||||||
assert IPFS.get_final_url("/api/v0/files/rm") == "http://localhost:5001/api/v0/files/rm"
|
assert IPFS.delete_file_endpoint() == "http://localhost:5001/api/v0/files/rm"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,6 +88,26 @@ test "returns error", %{file_upload: file_upload} do
|
||||||
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}"
|
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "returns error if JSON decode fails", %{file_upload: file_upload} do
|
||||||
|
with_mocks([
|
||||||
|
{Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]},
|
||||||
|
{Jason, [], [decode: fn _ -> {:error, "JSON decode failed"} end]}
|
||||||
|
]) do
|
||||||
|
assert capture_log(fn ->
|
||||||
|
assert IPFS.put_file(file_upload) == {:error, "JSON decode failed"}
|
||||||
|
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"JSON decode failed\"}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
|
||||||
|
with_mocks([
|
||||||
|
{Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]},
|
||||||
|
{Jason, [], [decode: fn _ -> {:ok, %{}} end]}
|
||||||
|
]) do
|
||||||
|
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "delete_file/1" do
|
describe "delete_file/1" do
|
||||||
|
|
Loading…
Reference in New Issue