added tests for ipfs uploader. adapted changelog.md accordingly. improved ipfs uploader with external suggestions

fix lint description.exs
This commit is contained in:
Claudio Maradonna 2022-04-11 15:10:01 +02:00
parent fa2a6d5d6b
commit 43dfa58ebd
No known key found for this signature in database
GPG Key ID: 0CBA58694C5680D9
7 changed files with 116 additions and 15 deletions

View File

@ -32,6 +32,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- PleromaAPI: Add `GET /api/v1/pleroma/birthdays` API endpoint - PleromaAPI: Add `GET /api/v1/pleroma/birthdays` API endpoint
- Make backend-rendered pages translatable. This includes emails. Pages returned as a HTTP response are translated using the language specified in the `userLanguage` cookie, or the `Accept-Language` header. Emails are translated using the `language` field when registering. This language can be changed by `PATCH /api/v1/accounts/update_credentials` with the `language` field. - Make backend-rendered pages translatable. This includes emails. Pages returned as a HTTP response are translated using the language specified in the `userLanguage` cookie, or the `Accept-Language` header. Emails are translated using the `language` field when registering. This language can be changed by `PATCH /api/v1/accounts/update_credentials` with the `language` field.
- Uploadfilter `Pleroma.Upload.Filter.Exiftool.ReadDescription` returns description values to the FE so they can pre fill the image description field - Uploadfilter `Pleroma.Upload.Filter.Exiftool.ReadDescription` returns description values to the FE so they can pre fill the image description field
- Uploader: Add support for uploading attachments using IPFS
### Fixed ### Fixed
- Subscription(Bell) Notifications: Don't create from Pipeline Ingested replies - Subscription(Bell) Notifications: Don't create from Pipeline Ingested replies

View File

@ -147,7 +147,8 @@ config :pleroma, :config_description, [
type: :string, type: :string,
description: "GET Gateway URL", description: "GET Gateway URL",
suggestions: [ suggestions: [
"get_gateway_url" "https://ipfs.mydomain.com/<%= cid %>",
"https://<%= cid %>.ipfs.mydomain.com/"
] ]
}, },
%{ %{
@ -155,7 +156,7 @@ config :pleroma, :config_description, [
type: :string, type: :string,
description: "POST Gateway URL", description: "POST Gateway URL",
suggestions: [ suggestions: [
"post_gateway_url" "http://localhost:5001/"
] ]
} }
] ]

View File

@ -58,10 +58,6 @@ config :pleroma, Pleroma.Web.ApiSpec.CastAndValidate, strict: true
# https://dashbit.co/blog/speeding-up-re-compilation-of-elixir-projects # https://dashbit.co/blog/speeding-up-re-compilation-of-elixir-projects
config :phoenix, :plug_init_mode, :runtime config :phoenix, :plug_init_mode, :runtime
config :pleroma, Pleroma.Uploaders.IPFS,
post_gateway_url: nil,
get_gateway_url: nil
if File.exists?("./config/dev.secret.exs") do if File.exists?("./config/dev.secret.exs") do
import_config "dev.secret.exs" import_config "dev.secret.exs"
else else

View File

@ -614,6 +614,19 @@ config :ex_aws, :s3,
host: "s3.eu-central-1.amazonaws.com" host: "s3.eu-central-1.amazonaws.com"
``` ```
#### Pleroma.Uploaders.IPFS
* `post_gateway_url`: URL with port of POST Gateway (unauthenticated)
* `get_gateway_url`: URL of public GET Gateway
Example:
```elixir
config :pleroma, Pleroma.Uploaders.IPFS,
post_gateway_url: "http://localhost:5001",
get_gateway_url: "http://<%= cid %>.ipfs.mydomain.com"
```
### Upload filters ### Upload filters
#### Pleroma.Upload.Filter.AnonymizeFilename #### Pleroma.Upload.Filter.AnonymizeFilename

View File

@ -235,10 +235,8 @@ defmodule Pleroma.Upload do
"" ""
end end
uploader = Config.get([Pleroma.Upload, :uploader]) if String.contains?(base_url, "<%= cid %>") do
EEx.eval_string(base_url, cid: path)
if uploader == Pleroma.Uploaders.IPFS && String.contains?(base_url, "{CID}") do
String.replace(base_url, "{CID}", path)
else else
[base_url, path] [base_url, path]
|> Path.join() |> Path.join()

View File

@ -13,10 +13,10 @@ defmodule Pleroma.Uploaders.IPFS do
def get_file(file) do def get_file(file) do
b_url = Pleroma.Upload.base_url() b_url = Pleroma.Upload.base_url()
if String.contains?(b_url, "{CID}") do if String.contains?(b_url, "<%= cid %>") do
{:ok, {:url, String.replace(b_url, "{CID}", URI.decode(file))}} {:ok, {:url, EEx.eval_string(b_url, cid: URI.decode(file))}}
else else
{:error, "IPFS Get URL doesn't contain '{CID}' placeholder"} {:error, "IPFS Get URL doesn't contain 'cid' placeholder"}
end end
end end
@ -36,7 +36,11 @@ defmodule Pleroma.Uploaders.IPFS do
{:ok, ret} -> {:ok, ret} ->
case Jason.decode(ret.body) do case Jason.decode(ret.body) do
{:ok, ret} -> {:ok, ret} ->
{:ok, {:file, ret["Hash"]}} if Map.has_key?(ret, "Hash") do
{:ok, {:file, ret["Hash"]}}
else
{:error, "JSON doesn't contain Hash value"}
end
error -> error ->
Logger.error("#{__MODULE__}: #{inspect(error)}") Logger.error("#{__MODULE__}: #{inspect(error)}")
@ -45,7 +49,7 @@ defmodule Pleroma.Uploaders.IPFS do
error -> error ->
Logger.error("#{__MODULE__}: #{inspect(error)}") Logger.error("#{__MODULE__}: #{inspect(error)}")
{:error, "IPFS Gateway Upload failed"} {:error, "IPFS Gateway upload failed"}
end end
end end

View File

@ -0,0 +1,88 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Uploaders.IPFSTest do
use Pleroma.DataCase
alias Pleroma.Uploaders.IPFS
alias Tesla.Multipart
import Mock
import ExUnit.CaptureLog
setup do
clear_config([Pleroma.Upload, :uploader], Pleroma.Uploaders.IPFS)
clear_config([Pleroma.Uploaders.IPFS])
clear_config(
[Pleroma.Uploaders.IPFS, :get_gateway_url],
"https://<%= cid %>.ipfs.mydomain.com"
)
clear_config([Pleroma.Uploaders.IPFS, :post_gateway_url], "http://localhost:5001")
end
describe "get_file/1" do
test "it returns path to ipfs file with cid as subdomain" do
assert IPFS.get_file("testcid") == {
:ok,
{:url, "https://testcid.ipfs.mydomain.com"}
}
end
test "it returns path to ipfs file with cid as path" do
clear_config(
[Pleroma.Uploaders.IPFS, :get_gateway_url],
"https://ipfs.mydomain.com/ipfs/<%= cid %>"
)
assert IPFS.get_file("testcid") == {
:ok,
{:url, "https://ipfs.mydomain.com/ipfs/testcid"}
}
end
end
describe "put_file/1" do
setup do
file_upload = %Pleroma.Upload{
name: "image-tet.jpg",
content_type: "image/jpeg",
path: "test_folder/image-tet.jpg",
tempfile: Path.absname("test/instance_static/add/shortcode.png")
}
[file_upload: file_upload]
end
test "save file", %{file_upload: file_upload} do
with_mock Pleroma.HTTP,
post: fn _, _, _, _ ->
{:ok,
%Tesla.Env{
status: 200,
body: "{\"Hash\":\"bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi\"}"
}}
end do
assert IPFS.put_file(file_upload) ==
{:ok, {:file, "bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi"}}
end
end
test "returns error", %{file_upload: file_upload} do
with_mock Pleroma.HTTP, post: fn _, _, _, _ -> {:error, "IPFS Gateway upload failed"} end do
assert capture_log(fn ->
assert IPFS.put_file(file_upload) == {:error, "IPFS Gateway upload failed"}
end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"IPFS Gateway upload failed\"}"
end
end
end
describe "delete_file/1" do
test_with_mock "deletes file", Pleroma.HTTP,
post: fn _, _, _, _ -> {:ok, %{status_code: 204}} end do
assert :ok = IPFS.delete_file("image.jpg")
end
end
end