add key to handle IPFS /api/v0/add custom params

This commit is contained in:
Claudio Maradonna 2022-09-23 15:32:14 +02:00
parent 95bc4713b3
commit d20f39f9a1
Signed by untrusted user who does not match committer: claudiomaradonna
GPG key ID: 0CBA58694C5680D9
3 changed files with 22 additions and 6 deletions

View file

@ -14,6 +14,7 @@ modified `upload.ex` Akkoma won't return the right base url back.**
* `post_gateway_url`: URL with port of POST Gateway (unauthenticated)
* `get_gateway_url`: URL of public GET Gateway
* `api_add_params`: List of params compatible with `/api/v0/add`: https://docs.ipfs.tech/reference/kubo/rpc/#api-v0-add, if not provided will be default to `['cid-version': 1]`
### Example
@ -24,7 +25,8 @@ config :pleroma, :modules, runtime_dir: "instance/modules"
config :pleroma, Pleroma.Uploaders.IPFS,
post_gateway_url: "http://localhost:5001",
get_gateway_url: "https://{CID}.ipfs.mydomain.com"
get_gateway_url: "https://{CID}.ipfs.mydomain.com",
api_add_params: ['cid-version': 1, hash: "blake3"]
config :pleroma, Pleroma.Upload,
uploader: Pleroma.Uploaders.IPFS

View file

@ -23,6 +23,18 @@ defmodule Pleroma.Uploaders.IPFS do
get_final_url("/api/v0/files/rm")
end
def api_add_params do
config = Config.get([__MODULE__])
default_params = ["cid-version": "1"]
user_params = Keyword.get(config, :api_add_params, default_params)
if is_list(user_params) do
user_params
else
default_params
end
end
@placeholder "{CID}"
def placeholder, do: @placeholder
@ -44,8 +56,10 @@ defmodule Pleroma.Uploaders.IPFS do
|> Multipart.add_content_type_param("charset=utf-8")
|> Multipart.add_file(upload.tempfile)
case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: ["cid-version": "1"]) do
case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: api_add_params()) do
{:ok, ret} ->
Logger.error(ret)
case Jason.decode(ret.body) do
{:ok, ret} ->
if Map.has_key?(ret, "Hash") do

View file

@ -72,7 +72,7 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "save file", %{file_upload: file_upload} do
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: IPFS.api_add_params() ->
{:ok,
%Tesla.Env{
status: 200,
@ -87,7 +87,7 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error", %{file_upload: file_upload} do
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: IPFS.api_add_params() ->
{:error, "IPFS Gateway upload failed"}
end do
assert capture_log(fn ->
@ -98,7 +98,7 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error if JSON decode fails", %{file_upload: file_upload} do
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: IPFS.api_add_params() ->
{:ok, %Tesla.Env{status: 200, body: "invalid"}}
end do
assert capture_log(fn ->
@ -110,7 +110,7 @@ defmodule Pleroma.Uploaders.IPFSTest do
test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
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: IPFS.api_add_params() ->
{:ok, %Tesla.Env{status: 200, body: "{\"key\": \"value\"}"}}
end do
assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}