2022-04-07 16:25:02 +00:00
|
|
|
# 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 Tesla.Multipart
|
|
|
|
|
2024-05-27 13:22:18 +00:00
|
|
|
@config_impl Application.compile_env(:pleroma, [__MODULE__, :config_impl], Pleroma.Config)
|
|
|
|
|
2022-06-09 17:24:13 +00:00
|
|
|
defp get_final_url(method) do
|
2024-05-27 13:22:18 +00:00
|
|
|
config = @config_impl.get([__MODULE__])
|
2022-05-09 10:15:40 +00:00
|
|
|
post_base_url = Keyword.get(config, :post_gateway_url)
|
|
|
|
|
|
|
|
Path.join([post_base_url, method])
|
|
|
|
end
|
|
|
|
|
2022-06-09 21:38:50 +00:00
|
|
|
def put_file_endpoint do
|
2022-06-09 17:24:13 +00:00
|
|
|
get_final_url("/api/v0/add")
|
|
|
|
end
|
|
|
|
|
2022-06-09 21:38:50 +00:00
|
|
|
def delete_file_endpoint do
|
2022-06-09 17:24:13 +00:00
|
|
|
get_final_url("/api/v0/files/rm")
|
|
|
|
end
|
|
|
|
|
|
|
|
@placeholder "{CID}"
|
|
|
|
def placeholder, do: @placeholder
|
|
|
|
|
2022-04-07 16:25:02 +00:00
|
|
|
@impl true
|
|
|
|
def get_file(file) do
|
|
|
|
b_url = Pleroma.Upload.base_url()
|
|
|
|
|
2022-04-16 07:38:49 +00:00
|
|
|
if String.contains?(b_url, @placeholder) do
|
|
|
|
{:ok, {:url, String.replace(b_url, @placeholder, URI.decode(file))}}
|
2022-04-07 16:25:02 +00:00
|
|
|
else
|
2022-04-11 13:10:01 +00:00
|
|
|
{:error, "IPFS Get URL doesn't contain 'cid' placeholder"}
|
2022-04-07 16:25:02 +00:00
|
|
|
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)
|
|
|
|
|
2022-06-09 17:24:13 +00:00
|
|
|
case Pleroma.HTTP.post(put_file_endpoint(), mp, [], params: ["cid-version": "1"]) do
|
2022-04-07 16:25:02 +00:00
|
|
|
{:ok, ret} ->
|
|
|
|
case Jason.decode(ret.body) do
|
|
|
|
{:ok, ret} ->
|
2022-04-11 13:10:01 +00:00
|
|
|
if Map.has_key?(ret, "Hash") do
|
|
|
|
{:ok, {:file, ret["Hash"]}}
|
|
|
|
else
|
2022-05-09 10:15:40 +00:00
|
|
|
{:error, "JSON doesn't contain Hash key"}
|
2022-04-11 13:10:01 +00:00
|
|
|
end
|
2022-04-07 16:25:02 +00:00
|
|
|
|
|
|
|
error ->
|
|
|
|
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
|
|
|
{:error, "JSON decode failed"}
|
|
|
|
end
|
|
|
|
|
|
|
|
error ->
|
|
|
|
Logger.error("#{__MODULE__}: #{inspect(error)}")
|
2022-04-11 13:10:01 +00:00
|
|
|
{:error, "IPFS Gateway upload failed"}
|
2022-04-07 16:25:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def delete_file(file) do
|
2022-06-09 17:24:13 +00:00
|
|
|
case Pleroma.HTTP.post(delete_file_endpoint(), "", [], params: [arg: file]) do
|
2024-05-27 13:22:18 +00:00
|
|
|
{:ok, %{status: 204}} -> :ok
|
2022-04-07 16:25:02 +00:00
|
|
|
error -> {:error, inspect(error)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|