2019-05-22 14:12:11 +02:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/plugs/uploaded_media.ex
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Plugs.UploadedMedia do
|
2019-05-22 14:12:11 +02:00
|
|
|
@moduledoc """
|
|
|
|
Serves uploaded media files
|
|
|
|
"""
|
|
|
|
|
2019-09-08 00:05:54 +02:00
|
|
|
@behaviour Plug
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
import Plug.Conn
|
2019-09-08 00:05:54 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Config
|
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.{ReverseProxy, Upload}
|
|
|
|
|
2019-05-22 14:12:11 +02:00
|
|
|
require Logger
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def call(%{request_path: <<"/", @path, "/", file::binary>>} = conn, opts) do
|
|
|
|
conn =
|
|
|
|
case fetch_query_params(conn) do
|
|
|
|
%{query_params: %{"name" => name}} = conn ->
|
|
|
|
name = String.replace(name, "\"", "\\\"")
|
|
|
|
|
2020-01-23 00:55:07 +01:00
|
|
|
put_resp_header(conn, "content-disposition", "filename=\"#{name}\"")
|
2019-05-22 14:12:11 +02:00
|
|
|
|
|
|
|
conn ->
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
config = Config.get([Upload])
|
2019-05-22 14:12:11 +02: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
|
|
|
|
|> send_resp(500, "Failed")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _opts), do: conn
|
|
|
|
|
|
|
|
defp get_media(conn, {:static_dir, directory}, _, opts) do
|
|
|
|
static_opts =
|
2019-09-22 13:41:24 +02:00
|
|
|
opts
|
|
|
|
|> Map.get(:static_plug_opts)
|
2019-05-22 14:12:11 +02:00
|
|
|
|> Map.put(:at, [@path])
|
|
|
|
|> Map.put(:from, directory)
|
|
|
|
|
|
|
|
conn = Plug.Static.call(conn, static_opts)
|
|
|
|
|
|
|
|
if conn.halted do
|
|
|
|
conn
|
|
|
|
else
|
|
|
|
conn
|
|
|
|
|> send_resp(404, "Not found")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_media(conn, {:url, url}, true, _) do
|
2020-01-28 19:18:33 +01:00
|
|
|
ReverseProxy.call(conn, url, Config.get([Mobilizon.Upload, :proxy_opts], []))
|
2019-05-22 14:12:11 +02:00
|
|
|
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
|
|
|
|
|> send_resp(500, "Internal Error")
|
|
|
|
|> halt()
|
|
|
|
end
|
|
|
|
end
|