X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fuploaders%2Fmdii.ex;h=c36f3d61d9761cc1a02eb3f6b647fb1daa56b31c;hb=e4292cbfad47e59c76461fa201bab3e5f791962b;hp=f1f996fca4549594cfefe3308ee97993e402d0f3;hpb=4fbfacf5e129ad3f60e67f5ade2489f4bae48f56;p=akkoma diff --git a/lib/pleroma/uploaders/mdii.ex b/lib/pleroma/uploaders/mdii.ex index f1f996fca..c36f3d61d 100644 --- a/lib/pleroma/uploaders/mdii.ex +++ b/lib/pleroma/uploaders/mdii.ex @@ -1,23 +1,37 @@ -defmodule Pleroma.Uploaders.Mdii do - @behaviour Pleroma.Uploaders.Uploader +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Uploaders.MDII do + @moduledoc "Represents uploader for https://github.com/hakaba-hitoyo/minimal-digital-image-infrastructure" - @httpoison Application.get_env(:pleroma, :httpoison) + alias Pleroma.Config + alias Pleroma.HTTP - def put_file(name, uuid, path, content_type, _should_dedupe) do - settings = Application.get_env(:pleroma, Pleroma.Uploaders.Mdii) - host_name = Keyword.fetch!(settings, :host_name) + @behaviour Pleroma.Uploaders.Uploader + + # MDII-hosted images are never passed through the MediaPlug; only local media. + # Delegate to Pleroma.Uploaders.Local + def get_file(file) do + Pleroma.Uploaders.Local.get_file(file) + end - {:ok, file_data} = File.read(path) + def put_file(upload) do + cgi = Config.get([Pleroma.Uploaders.MDII, :cgi]) + files = Config.get([Pleroma.Uploaders.MDII, :files]) - File.rm!(path) + {:ok, file_data} = File.read(upload.tempfile) - extension = Regex.replace(~r/^image\//, content_type, "") - query = "https://#{host_name}/mdii-post.cgi?#{extension}" + extension = String.split(upload.name, ".") |> List.last() + query = "#{cgi}?#{extension}" - with {:ok, %{status_code: 200, body: body}} <- @httpoison.post(query, file_data) do - remote_file_name = List.first(String.split(body)) - public_url = "https://#{host_name}/#{remote_file_name}.#{extension}" - {:ok, public_url} + with {:ok, %{status: 200, body: body}} <- + HTTP.post(query, file_data, [], adapter: [pool: :default]) do + remote_file_name = String.split(body) |> List.first() + public_url = "#{files}/#{remote_file_name}.#{extension}" + {:ok, {:url, public_url}} + else + _ -> Pleroma.Uploaders.Local.put_file(upload) end end end