add key to handle IPFS /api/v0/add custom params
This commit is contained in:
parent
95bc4713b3
commit
d20f39f9a1
3 changed files with 22 additions and 6 deletions
|
@ -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)
|
* `post_gateway_url`: URL with port of POST Gateway (unauthenticated)
|
||||||
* `get_gateway_url`: URL of public GET Gateway
|
* `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
|
### Example
|
||||||
|
|
||||||
|
@ -24,7 +25,8 @@ config :pleroma, :modules, runtime_dir: "instance/modules"
|
||||||
|
|
||||||
config :pleroma, Pleroma.Uploaders.IPFS,
|
config :pleroma, Pleroma.Uploaders.IPFS,
|
||||||
post_gateway_url: "http://localhost:5001",
|
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,
|
config :pleroma, Pleroma.Upload,
|
||||||
uploader: Pleroma.Uploaders.IPFS
|
uploader: Pleroma.Uploaders.IPFS
|
||||||
|
|
|
@ -23,6 +23,18 @@ defmodule Pleroma.Uploaders.IPFS do
|
||||||
get_final_url("/api/v0/files/rm")
|
get_final_url("/api/v0/files/rm")
|
||||||
end
|
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}"
|
@placeholder "{CID}"
|
||||||
def placeholder, do: @placeholder
|
def placeholder, do: @placeholder
|
||||||
|
|
||||||
|
@ -44,8 +56,10 @@ defmodule Pleroma.Uploaders.IPFS 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)
|
||||||
|
|
||||||
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} ->
|
{:ok, ret} ->
|
||||||
|
Logger.error(ret)
|
||||||
|
|
||||||
case Jason.decode(ret.body) do
|
case Jason.decode(ret.body) do
|
||||||
{:ok, ret} ->
|
{:ok, ret} ->
|
||||||
if Map.has_key?(ret, "Hash") do
|
if Map.has_key?(ret, "Hash") do
|
||||||
|
|
|
@ -72,7 +72,7 @@ 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: IPFS.api_add_params() ->
|
||||||
{:ok,
|
{:ok,
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
|
@ -87,7 +87,7 @@ 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: IPFS.api_add_params() ->
|
||||||
{:error, "IPFS Gateway upload failed"}
|
{:error, "IPFS Gateway upload failed"}
|
||||||
end do
|
end do
|
||||||
assert capture_log(fn ->
|
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
|
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: IPFS.api_add_params() ->
|
||||||
{: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 ->
|
||||||
|
@ -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
|
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: IPFS.api_add_params() ->
|
||||||
{: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"}
|
||||||
|
|
Loading…
Reference in a new issue