2018-12-23 15:04:54 -05:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 10:41:47 -05:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 15:04:54 -05:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-23 11:40:45 -05:00
|
|
|
defmodule Pleroma.Plugs.UploadedMedia do
|
|
|
|
@moduledoc """
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Plug.Conn
|
2019-07-10 05:25:58 -04:00
|
|
|
import Pleroma.Web.Gettext
|
2018-11-23 11:40:45 -05:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
@behaviour Plug
|
|
|
|
# no slashes
|
|
|
|
@path "media"
|
|
|
|
|
|
|
|
def init(_opts) do
|
|
|
|
static_plug_opts =
|
|
|
|
[]
|
|
|
|
|> Keyword.put(:from, "__unconfigured_media_plug")
|
|
|
|
|> Keyword.put(:at, "/__unconfigured_media_plug")
|
|
|
|
|> Plug.Static.init()
|
|
|
|
|
|
|
|
%{static_plug_opts: static_plug_opts}
|
|
|
|
end
|
|
|
|
|
2019-02-06 14:19:39 -05:00
|
|
|
def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
2019-03-12 02:10:19 -04:00
|
|
|
conn =
|
|
|
|
case fetch_query_params(conn) do
|
|
|
|
%{query_params: %{"name" => name}} = conn ->
|
2019-03-12 02:21:13 -04:00
|
|
|
name = String.replace(name, "\"", "\\\"")
|
|
|
|
|
2019-03-12 02:10:19 -04:00
|
|
|
conn
|
2019-03-14 15:02:48 -04:00
|
|
|
|> put_resp_header("content-disposition", "filename=\"#{name}\"")
|
2019-03-12 02:10:19 -04:00
|
|
|
|
|
|
|
conn ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
2019-06-14 11:45:05 -04:00
|
|
|
config = Pleroma.Config.get(Pleroma.Upload)
|
2018-11-23 11:40:45 -05:00
|
|
|
|
|
|
|
with uploader <- Keyword.fetch!(config, :uploader),
|
|
|
|
proxy_remote = Keyword.get(config, :proxy_remote, false),
|
|
|
|
{:ok, get_method} <- uploader.get_file(file) do
|
|
|
|
get_media(conn, get_method, proxy_remote, opts)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
conn
|
2019-07-10 05:25:58 -04:00
|
|
|
|> send_resp(:internal_server_error, dgettext("errors", "Failed"))
|
2018-11-23 11:40:45 -05:00
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _opts), do: conn
|
|
|
|
|
|
|
|
defp get_media(conn, {:static_dir, directory}, _, opts) do
|
|
|
|
static_opts =
|
|
|
|
Map.get(opts, :static_plug_opts)
|
|
|
|
|> Map.put(:at, [@path])
|
|
|
|
|> Map.put(:from, directory)
|
|
|
|
|
|
|
|
conn = Plug.Static.call(conn, static_opts)
|
|
|
|
|
|
|
|
if conn.halted do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
2019-07-10 06:40:34 -04:00
|
|
|
|> send_resp(:not_found, dgettext("errors", "Not found"))
|
2018-11-23 11:40:45 -05:00
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, {:url, url}, true, _) do
|
|
|
|
conn
|
|
|
|
|> Pleroma.ReverseProxy.call(url, Pleroma.Config.get([Pleroma.Upload, :proxy_opts], []))
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, {:url, url}, _, _) do
|
|
|
|
conn
|
|
|
|
|> Phoenix.Controller.redirect(external: url)
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, unknown, _, _) do
|
|
|
|
Logger.error("#{__MODULE__}: Unknown get startegy: #{inspect(unknown)}")
|
|
|
|
|
|
|
|
conn
|
2019-07-10 06:40:34 -04:00
|
|
|
|> send_resp(:internal_server_error, dgettext("errors", "Internal Error"))
|
2018-11-23 11:40:45 -05:00
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|