1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Plugs.UploadedMedia do
19 |> Keyword.put(:from, "__unconfigured_media_plug")
20 |> Keyword.put(:at, "/__unconfigured_media_plug")
23 %{static_plug_opts: static_plug_opts}
26 def call(conn = %{request_path: <<"/", @path, "/", file::binary>>}, opts) do
27 config = Pleroma.Config.get([Pleroma.Upload])
29 with uploader <- Keyword.fetch!(config, :uploader),
30 proxy_remote = Keyword.get(config, :proxy_remote, false),
31 {:ok, get_method} <- uploader.get_file(file) do
32 get_media(conn, get_method, proxy_remote, opts)
36 |> send_resp(500, "Failed")
41 def call(conn, _opts), do: conn
43 defp get_media(conn, {:static_dir, directory}, _, opts) do
45 Map.get(opts, :static_plug_opts)
46 |> Map.put(:at, [@path])
47 |> Map.put(:from, directory)
49 conn = Plug.Static.call(conn, static_opts)
55 |> send_resp(404, "Not found")
60 defp get_media(conn, {:url, url}, true, _) do
62 |> Pleroma.ReverseProxy.call(url, Pleroma.Config.get([Pleroma.Upload, :proxy_opts], []))
65 defp get_media(conn, {:url, url}, _, _) do
67 |> Phoenix.Controller.redirect(external: url)
71 defp get_media(conn, unknown, _, _) do
72 Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
75 |> send_resp(500, "Internal Error")