Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85806964
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
6 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/lib/pleroma/uploaders/ipfs.ex b/lib/pleroma/uploaders/ipfs.ex
index 7a7481d81..9f6f26e2e 100644
--- a/lib/pleroma/uploaders/ipfs.ex
+++ b/lib/pleroma/uploaders/ipfs.ex
@@ -1,72 +1,76 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Uploaders.IPFS do
@behaviour Pleroma.Uploaders.Uploader
require Logger
alias Pleroma.Config
alias Tesla.Multipart
- @placeholder "{CID}"
- def placeholder, do: @placeholder
-
- def get_final_url(method) do
+ defp get_final_url(method) do
config = Config.get([__MODULE__])
post_base_url = Keyword.get(config, :post_gateway_url)
Path.join([post_base_url, method])
end
+ def put_file_endpoint() do
+ get_final_url("/api/v0/add")
+ end
+
+ def delete_file_endpoint() do
+ get_final_url("/api/v0/files/rm")
+ end
+
+ @placeholder "{CID}"
+ def placeholder, do: @placeholder
+
@impl true
def get_file(file) do
b_url = Pleroma.Upload.base_url()
if String.contains?(b_url, @placeholder) do
{:ok, {:url, String.replace(b_url, @placeholder, URI.decode(file))}}
else
{:error, "IPFS Get URL doesn't contain 'cid' placeholder"}
end
end
@impl true
def put_file(%Pleroma.Upload{} = upload) do
mp =
Multipart.new()
|> Multipart.add_content_type_param("charset=utf-8")
|> Multipart.add_file(upload.tempfile)
- final_url = get_final_url("/api/v0/add")
-
- case Pleroma.HTTP.post(final_url, mp, [], params: ["cid-version": "1"]) do
+ case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: ["cid-version": "1"]) do
{:ok, ret} ->
case Jason.decode(ret.body) do
{:ok, ret} ->
if Map.has_key?(ret, "Hash") do
{:ok, {:file, ret["Hash"]}}
else
{:error, "JSON doesn't contain Hash key"}
end
error ->
Logger.error("#{__MODULE__}: #{inspect(error)}")
{:error, "JSON decode failed"}
end
error ->
Logger.error("#{__MODULE__}: #{inspect(error)}")
{:error, "IPFS Gateway upload failed"}
end
end
@impl true
def delete_file(file) do
- final_url = get_final_url("/api/v0/files/rm")
-
- case Pleroma.HTTP.post(final_url, "", [], params: [arg: file]) do
+ case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do
{:ok, %{status_code: 204}} -> :ok
error -> {:error, inspect(error)}
end
end
end
diff --git a/test/pleroma/uploaders/ipfs_test.exs b/test/pleroma/uploaders/ipfs_test.exs
index d567272d2..f2b880d9f 100644
--- a/test/pleroma/uploaders/ipfs_test.exs
+++ b/test/pleroma/uploaders/ipfs_test.exs
@@ -1,99 +1,119 @@
# 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_final_url" do
test "it returns the final url for put_file" do
- assert IPFS.get_final_url("/api/v0/add") == "http://localhost:5001/api/v0/add"
+ assert IPFS.put_file_endpoint() == "http://localhost:5001/api/v0/add"
end
test "it returns the final url for delete_file" do
- assert IPFS.get_final_url("/api/v0/files/rm") == "http://localhost:5001/api/v0/files/rm"
+ assert IPFS.delete_file_endpoint() == "http://localhost:5001/api/v0/files/rm"
end
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:
"{\"Name\":\"image-tet.jpg\",\"Size\":\"5000\", \"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
+
+ test "returns error if JSON decode fails", %{file_upload: file_upload} do
+ with_mocks([
+ {Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]},
+ {Jason, [], [decode: fn _ -> {:error, "JSON decode failed"} end]}
+ ]) do
+ assert capture_log(fn ->
+ assert IPFS.put_file(file_upload) == {:error, "JSON decode failed"}
+ end) =~ "Elixir.Pleroma.Uploaders.IPFS: {:error, \"JSON decode failed\"}"
+ end
+ end
+
+ test "returns error if JSON body doesn't contain Hash key", %{file_upload: file_upload} do
+ with_mocks([
+ {Pleroma.HTTP, [], [post: fn _, _, _, _ -> {:ok, %Tesla.Env{status: 200, body: ''}} end]},
+ {Jason, [], [decode: fn _ -> {:ok, %{}} end]}
+ ]) do
+ assert IPFS.put_file(file_upload) == {:error, "JSON doesn't contain Hash key"}
+ 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
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Oct 13, 5:12 PM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1786836
Default Alt Text
(6 KB)
Attached To
Mode
rPUBE pleroma-upstream
Attached
Detach File
Event Timeline
Log In to Comment