spc-pleroma/lib/pleroma/uploaders/mdii.ex

38 lines
1.2 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 15:41:47 +00:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2018-11-17 09:14:42 +00:00
defmodule Pleroma.Uploaders.MDII do
2019-08-10 18:46:26 +00:00
@moduledoc "Represents uploader for https://github.com/hakaba-hitoyo/minimal-digital-image-infrastructure"
2018-11-17 09:14:42 +00:00
alias Pleroma.Config
2019-05-25 04:24:21 +00:00
alias Pleroma.HTTP
2018-11-17 09:14:42 +00:00
2018-11-15 05:19:10 +00:00
@behaviour Pleroma.Uploaders.Uploader
2018-11-23 16:40:45 +00:00
# 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
2018-11-29 20:11:45 +00:00
def put_file(upload) do
2018-12-09 09:12:48 +00:00
cgi = Config.get([Pleroma.Uploaders.MDII, :cgi])
files = Config.get([Pleroma.Uploaders.MDII, :files])
2018-11-15 05:19:10 +00:00
2018-11-29 20:11:45 +00:00
{:ok, file_data} = File.read(upload.tempfile)
2018-11-15 05:19:10 +00:00
2018-11-29 20:11:45 +00:00
extension = String.split(upload.name, ".") |> List.last()
2018-11-16 11:41:12 +00:00
query = "#{cgi}?#{extension}"
2018-11-15 05:19:10 +00:00
with {:ok, %{status: 200, body: body}} <-
2019-05-25 04:24:21 +00:00
HTTP.post(query, file_data, [], adapter: [pool: :default]) do
2018-11-16 11:22:36 +00:00
remote_file_name = String.split(body) |> List.first()
2018-11-16 11:41:12 +00:00
public_url = "#{files}/#{remote_file_name}.#{extension}"
2018-11-23 16:40:45 +00:00
{:ok, {:url, public_url}}
2018-11-17 11:16:25 +00:00
else
2018-11-29 20:11:45 +00:00
_ -> Pleroma.Uploaders.Local.put_file(upload)
2018-11-15 05:38:45 +00:00
end
2018-11-15 05:19:10 +00:00
end
end