Merge branch 'ipfs-dialyzer-fixes' into 'develop'
IPFS uploader: dialyzer fixes See merge request pleroma/pleroma!4129
This commit is contained in:
commit
acde8d0e0c
1
changelog.d/ipfs-dialyzer.skip
Normal file
1
changelog.d/ipfs-dialyzer.skip
Normal file
@ -0,0 +1 @@
|
|||||||
|
no comment
|
@ -83,8 +83,8 @@ config :ex_aws, :s3,
|
|||||||
scheme: "https://"
|
scheme: "https://"
|
||||||
|
|
||||||
config :pleroma, Pleroma.Uploaders.IPFS,
|
config :pleroma, Pleroma.Uploaders.IPFS,
|
||||||
post_gateway_url: nil,
|
post_gateway_url: "http://localhost:5001",
|
||||||
get_gateway_url: nil
|
get_gateway_url: "http://localhost:8080"
|
||||||
|
|
||||||
config :pleroma, :emoji,
|
config :pleroma, :emoji,
|
||||||
shortcode_globs: ["/emoji/custom/**/*.png"],
|
shortcode_globs: ["/emoji/custom/**/*.png"],
|
||||||
|
@ -37,7 +37,7 @@ defmodule Pleroma.HTTP do
|
|||||||
|
|
||||||
See `Pleroma.HTTP.request/5`
|
See `Pleroma.HTTP.request/5`
|
||||||
"""
|
"""
|
||||||
@spec post(Request.url(), String.t(), Request.headers(), keyword()) ::
|
@spec post(Request.url(), Tesla.Env.body(), Request.headers(), keyword()) ::
|
||||||
{:ok, Env.t()} | {:error, any()}
|
{:ok, Env.t()} | {:error, any()}
|
||||||
def post(url, body, headers \\ [], options \\ []),
|
def post(url, body, headers \\ [], options \\ []),
|
||||||
do: request(:post, url, body, headers, options)
|
do: request(:post, url, body, headers, options)
|
||||||
@ -56,7 +56,7 @@ defmodule Pleroma.HTTP do
|
|||||||
`{:ok, %Tesla.Env{}}` or `{:error, error}`
|
`{:ok, %Tesla.Env{}}` or `{:error, error}`
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@spec request(method(), Request.url(), String.t(), Request.headers(), keyword()) ::
|
@spec request(method(), Request.url(), Tesla.Env.body(), Request.headers(), keyword()) ::
|
||||||
{:ok, Env.t()} | {:error, any()}
|
{:ok, Env.t()} | {:error, any()}
|
||||||
def request(method, url, body, headers, options) when is_binary(url) do
|
def request(method, url, body, headers, options) when is_binary(url) do
|
||||||
uri = URI.parse(url)
|
uri = URI.parse(url)
|
||||||
|
@ -8,23 +8,10 @@ defmodule Pleroma.Uploaders.IPFS do
|
|||||||
|
|
||||||
alias Tesla.Multipart
|
alias Tesla.Multipart
|
||||||
|
|
||||||
|
@api_add "/api/v0/add"
|
||||||
|
@api_delete "/api/v0/files/rm"
|
||||||
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
|
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
|
||||||
|
|
||||||
defp get_final_url(method) do
|
|
||||||
config = @config_impl.get([__MODULE__])
|
|
||||||
post_base_url = Keyword.get(config, :post_gateway_url)
|
|
||||||
|
|
||||||
Path.join([post_base_url, method])
|
|
||||||
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}"
|
@placeholder "{CID}"
|
||||||
def placeholder, do: @placeholder
|
def placeholder, do: @placeholder
|
||||||
|
|
||||||
@ -40,26 +27,26 @@ defmodule Pleroma.Uploaders.IPFS do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def put_file(%Pleroma.Upload{} = upload) do
|
def put_file(%Pleroma.Upload{tempfile: tempfile}) do
|
||||||
mp =
|
mp =
|
||||||
Multipart.new()
|
Multipart.new()
|
||||||
|> Multipart.add_content_type_param("charset=utf-8")
|
|> Multipart.add_content_type_param("charset=utf-8")
|
||||||
|> Multipart.add_file(upload.tempfile)
|
|> Multipart.add_file(tempfile)
|
||||||
|
|
||||||
case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: ["cid-version": "1"]) do
|
endpoint = ipfs_endpoint(@api_add)
|
||||||
{:ok, ret} ->
|
|
||||||
case Jason.decode(ret.body) do
|
with {:ok, %{body: body}} when is_binary(body) <-
|
||||||
{:ok, ret} ->
|
Pleroma.HTTP.post(endpoint, mp, [], params: ["cid-version": "1"], pool: :upload),
|
||||||
if Map.has_key?(ret, "Hash") do
|
{_, {:ok, decoded}} <- {:json, Jason.decode(body)},
|
||||||
{:ok, {:file, ret["Hash"]}}
|
{_, true} <- {:hash, Map.has_key?(decoded, "Hash")} do
|
||||||
|
{:ok, {:file, decoded["Hash"]}}
|
||||||
else
|
else
|
||||||
|
{:hash, false} ->
|
||||||
{:error, "JSON doesn't contain Hash key"}
|
{:error, "JSON doesn't contain Hash key"}
|
||||||
end
|
|
||||||
|
|
||||||
error ->
|
{:json, error} ->
|
||||||
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
||||||
{:error, "JSON decode failed"}
|
{:error, "JSON decode failed"}
|
||||||
end
|
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
||||||
@ -69,9 +56,17 @@ defmodule Pleroma.Uploaders.IPFS do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def delete_file(file) do
|
def delete_file(file) do
|
||||||
case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do
|
endpoint = ipfs_endpoint(@api_delete)
|
||||||
|
|
||||||
|
case Pleroma.HTTP.post(endpoint, "", [], params: [arg: file]) do
|
||||||
{:ok, %{status: 204}} -> :ok
|
{:ok, %{status: 204}} -> :ok
|
||||||
error -> {:error, inspect(error)}
|
error -> {:error, inspect(error)}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp ipfs_endpoint(path) do
|
||||||
|
URI.parse(@config_impl.get([__MODULE__, :post_gateway_url]))
|
||||||
|
|> Map.put(:path, path)
|
||||||
|
|> URI.to_string()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -14,25 +14,6 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
|
|
||||||
alias Pleroma.UnstubbedConfigMock, as: Config
|
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||||
|
|
||||||
describe "get_final_url" do
|
|
||||||
setup do
|
|
||||||
Config
|
|
||||||
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|
||||||
[post_gateway_url: "http://localhost:5001"]
|
|
||||||
end)
|
|
||||||
|
|
||||||
:ok
|
|
||||||
end
|
|
||||||
|
|
||||||
test "it returns the final url for put_file" do
|
|
||||||
assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
|
|
||||||
end
|
|
||||||
|
|
||||||
test "it returns the final url for delete_file" do
|
|
||||||
assert IPFS.delete_file_endpoint() == "http://localhost:5001/api/v0/files/rm"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "get_file/1" do
|
describe "get_file/1" do
|
||||||
setup do
|
setup do
|
||||||
Config
|
Config
|
||||||
@ -71,8 +52,8 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
describe "put_file/1" do
|
describe "put_file/1" do
|
||||||
setup do
|
setup do
|
||||||
Config
|
Config
|
||||||
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :post_gateway_url] ->
|
||||||
[post_gateway_url: "http://localhost:5001"]
|
"http://localhost:5001"
|
||||||
end)
|
end)
|
||||||
|
|
||||||
file_upload = %Pleroma.Upload{
|
file_upload = %Pleroma.Upload{
|
||||||
@ -92,7 +73,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
|
|
||||||
test "save file", %{file_upload: file_upload} do
|
test "save file", %{file_upload: file_upload} do
|
||||||
with_mock Pleroma.HTTP,
|
with_mock Pleroma.HTTP,
|
||||||
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
post: fn "http://localhost:5001/api/v0/add",
|
||||||
|
_mp,
|
||||||
|
[],
|
||||||
|
params: ["cid-version": "1"],
|
||||||
|
pool: :upload ->
|
||||||
{:ok,
|
{:ok,
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
@ -107,7 +92,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
|
|
||||||
test "returns error", %{file_upload: file_upload} do
|
test "returns error", %{file_upload: file_upload} do
|
||||||
with_mock Pleroma.HTTP,
|
with_mock Pleroma.HTTP,
|
||||||
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
post: fn "http://localhost:5001/api/v0/add",
|
||||||
|
_mp,
|
||||||
|
[],
|
||||||
|
params: ["cid-version": "1"],
|
||||||
|
pool: :upload ->
|
||||||
{:error, "IPFS Gateway upload failed"}
|
{:error, "IPFS Gateway upload failed"}
|
||||||
end do
|
end do
|
||||||
assert capture_log(fn ->
|
assert capture_log(fn ->
|
||||||
@ -118,7 +107,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
|
|
||||||
test "returns error if JSON decode fails", %{file_upload: file_upload} do
|
test "returns error if JSON decode fails", %{file_upload: file_upload} do
|
||||||
with_mock Pleroma.HTTP, [],
|
with_mock Pleroma.HTTP, [],
|
||||||
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
post: fn "http://localhost:5001/api/v0/add",
|
||||||
|
_mp,
|
||||||
|
[],
|
||||||
|
params: ["cid-version": "1"],
|
||||||
|
pool: :upload ->
|
||||||
{:ok, %Tesla.Env{status: 200, body: "invalid"}}
|
{:ok, %Tesla.Env{status: 200, body: "invalid"}}
|
||||||
end do
|
end do
|
||||||
assert capture_log(fn ->
|
assert capture_log(fn ->
|
||||||
@ -130,7 +123,11 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
|
|
||||||
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
|
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
|
||||||
with_mock Pleroma.HTTP, [],
|
with_mock Pleroma.HTTP, [],
|
||||||
post: fn "http://localhost:5001/api/v0/add", _mp, [], params: ["cid-version": "1"] ->
|
post: fn "http://localhost:5001/api/v0/add",
|
||||||
|
_mp,
|
||||||
|
[],
|
||||||
|
params: ["cid-version": "1"],
|
||||||
|
pool: :upload ->
|
||||||
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
|
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
|
||||||
end do
|
end do
|
||||||
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
|
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
|
||||||
@ -141,8 +138,8 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
|||||||
describe "delete_file/1" do
|
describe "delete_file/1" do
|
||||||
setup do
|
setup do
|
||||||
Config
|
Config
|
||||||
|> expect(:get, fn [Pleroma.Uploaders.IPFS] ->
|
|> expect(:get, fn [Pleroma.Uploaders.IPFS, :post_gateway_url] ->
|
||||||
[post_gateway_url: "http://localhost:5001"]
|
"http://localhost:5001"
|
||||||
end)
|
end)
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
|
Loading…
Reference in New Issue
Block a user