mdii.ex 954 Bytes
Newer Older
hakabahitoyo's avatar
hakabahitoyo committed
1
2
3
defmodule Pleroma.Uploaders.MDII do
  alias Pleroma.Config

Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
4
5
  @behaviour Pleroma.Uploaders.Uploader

Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
6
7
  @httpoison Application.get_env(:pleroma, :httpoison)

href's avatar
href committed
8
9
10
11
12
13
  # 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

href's avatar
href committed
14
  def put_file(upload) do
Maksim's avatar
Maksim committed
15
16
    cgi = Config.get([Pleroma.Uploaders.MDII, :cgi])
    files = Config.get([Pleroma.Uploaders.MDII, :files])
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
17

href's avatar
href committed
18
    {:ok, file_data} = File.read(upload.tempfile)
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
19

href's avatar
href committed
20
    extension = String.split(upload.name, ".") |> List.last()
hakabahitoyo's avatar
hakabahitoyo committed
21
    query = "#{cgi}?#{extension}"
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
22

Maksim's avatar
Maksim committed
23
    with {:ok, %{status: 200, body: body}} <- @httpoison.post(query, file_data) do
hakabahitoyo's avatar
hakabahitoyo committed
24
      remote_file_name = String.split(body) |> List.first()
hakabahitoyo's avatar
hakabahitoyo committed
25
      public_url = "#{files}/#{remote_file_name}.#{extension}"
href's avatar
href committed
26
      {:ok, {:url, public_url}}
hakabahitoyo's avatar
hakabahitoyo committed
27
    else
href's avatar
href committed
28
      _ -> Pleroma.Uploaders.Local.put_file(upload)
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
29
    end
Hakaba Hitoyo's avatar
Hakaba Hitoyo committed
30
31
  end
end