diff --git a/README.md b/README.md index 2d1ed91..8e79520 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/pleroma/uploaders/ipfs.ex b/lib/pleroma/uploaders/ipfs.ex index 2e8683e..4b179f0 100644 --- a/lib/pleroma/uploaders/ipfs.ex +++ b/lib/pleroma/uploaders/ipfs.ex @@ -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 diff --git a/test/pleroma/uploaders/ipfs_test.exs b/test/pleroma/uploaders/ipfs_test.exs index 52bfaf4..a47176c 100644 --- a/test/pleroma/uploaders/ipfs_test.exs +++ b/test/pleroma/uploaders/ipfs_test.exs @@ -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"}