Compare commits

...

3 commits

Author SHA1 Message Date
Claudio Maradonna
70ad169511
update README for akkoma commit alignment 2023-03-18 10:19:43 +01:00
Claudio Maradonna
4eb388f52b
align upload.ex to latest commit 2641dcdd15 2022-10-11 17:23:36 +02:00
Claudio Maradonna
d20f39f9a1
add key to handle IPFS /api/v0/add custom params 2022-09-23 15:32:14 +02:00
4 changed files with 29 additions and 8 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,8 +25,14 @@ 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
```
### Akkoma commit alignment group by branch
- master: fef4bae0067fcd6e102e3d625390e8ae909c6bb8
- develop: fef4bae0067fcd6e102e3d625390e8ae909c6bb8

View file

@ -3,8 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Upload do
@behaviour Pleroma.Upload
@moduledoc """
Manage user uploads
@ -38,6 +36,7 @@ defmodule Pleroma.Upload do
alias Ecto.UUID
alias Pleroma.Config
alias Pleroma.Maps
alias Pleroma.Web.ActivityPub.Utils
require Logger
@type source ::
@ -90,6 +89,7 @@ defmodule Pleroma.Upload do
{:ok, url_spec} <- Pleroma.Uploaders.Uploader.put_file(opts.uploader, upload) do
{:ok,
%{
"id" => Utils.generate_object_id(),
"type" => opts.activity_type,
"mediaType" => upload.content_type,
"url" => [

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"}