ipfs: refactor final_url generation. add tests for final_url
fix lint
This commit is contained in:
parent
44659ecd65
commit
7c1af86f97
|
@ -12,6 +12,13 @@ defmodule Pleroma.Uploaders.IPFS do
|
||||||
@placeholder "{CID}"
|
@placeholder "{CID}"
|
||||||
def placeholder, do: @placeholder
|
def placeholder, do: @placeholder
|
||||||
|
|
||||||
|
def 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
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def get_file(file) do
|
def get_file(file) do
|
||||||
b_url = Pleroma.Upload.base_url()
|
b_url = Pleroma.Upload.base_url()
|
||||||
|
@ -25,15 +32,12 @@ def get_file(file) do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def put_file(%Pleroma.Upload{} = upload) do
|
def put_file(%Pleroma.Upload{} = upload) do
|
||||||
config = Config.get([__MODULE__])
|
|
||||||
post_base_url = Keyword.get(config, :post_gateway_url)
|
|
||||||
|
|
||||||
mp =
|
mp =
|
||||||
Multipart.new()
|
Multipart.new()
|
||||||
|> 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)
|
||||||
|
|
||||||
final_url = Path.join([post_base_url, "/api/v0/add"])
|
final_url = get_final_url("/api/v0/add")
|
||||||
|
|
||||||
case Pleroma.HTTP.post(final_url, mp, [], params: ["cid-version": "1"]) do
|
case Pleroma.HTTP.post(final_url, mp, [], params: ["cid-version": "1"]) do
|
||||||
{:ok, ret} ->
|
{:ok, ret} ->
|
||||||
|
@ -42,7 +46,7 @@ def put_file(%Pleroma.Upload{} = upload) do
|
||||||
if Map.has_key?(ret, "Hash") do
|
if Map.has_key?(ret, "Hash") do
|
||||||
{:ok, {:file, ret["Hash"]}}
|
{:ok, {:file, ret["Hash"]}}
|
||||||
else
|
else
|
||||||
{:error, "JSON doesn't contain Hash value"}
|
{:error, "JSON doesn't contain Hash key"}
|
||||||
end
|
end
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
|
@ -58,10 +62,7 @@ def put_file(%Pleroma.Upload{} = upload) do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def delete_file(file) do
|
def delete_file(file) do
|
||||||
config = Config.get([__MODULE__])
|
final_url = get_final_url("/api/v0/files/rm")
|
||||||
post_base_url = Keyword.get(config, :post_gateway_url)
|
|
||||||
|
|
||||||
final_url = Path.join([post_base_url, "/api/v0/files/rm"])
|
|
||||||
|
|
||||||
case Pleroma.HTTP.post(final_url, "", [], params: [arg: file]) do
|
case Pleroma.HTTP.post(final_url, "", [], params: [arg: file]) do
|
||||||
{:ok, %{status_code: 204}} -> :ok
|
{:ok, %{status_code: 204}} -> :ok
|
||||||
|
|
|
@ -23,6 +23,16 @@ defmodule Pleroma.Uploaders.IPFSTest do
|
||||||
clear_config([Pleroma.Uploaders.IPFS, :post_gateway_url], "http://localhost:5001")
|
clear_config([Pleroma.Uploaders.IPFS, :post_gateway_url], "http://localhost:5001")
|
||||||
end
|
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"
|
||||||
|
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"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "get_file/1" do
|
describe "get_file/1" do
|
||||||
test "it returns path to ipfs file with cid as subdomain" do
|
test "it returns path to ipfs file with cid as subdomain" do
|
||||||
assert IPFS.get_file("testcid") == {
|
assert IPFS.get_file("testcid") == {
|
||||||
|
@ -62,7 +72,8 @@ test "save file", %{file_upload: file_upload} do
|
||||||
{:ok,
|
{:ok,
|
||||||
%Tesla.Env{
|
%Tesla.Env{
|
||||||
status: 200,
|
status: 200,
|
||||||
body: "{\"Hash\":\"bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi\"}"
|
body:
|
||||||
|
"{\"Name\":\"image-tet.jpg\",\"Size\":\"5000\", \"Hash\":\"bafybeicrh7ltzx52yxcwrvxxckfmwhqdgsb6qym6dxqm2a4ymsakeshwoi\"}"
|
||||||
}}
|
}}
|
||||||
end do
|
end do
|
||||||
assert IPFS.put_file(file_upload) ==
|
assert IPFS.put_file(file_upload) ==
|
||||||
|
|
Loading…
Reference in New Issue