forked from potsda.mn/mobilizon
f90089e1bf
Use Upload Media logic from Pleroma Backend changes for picture upload Move AS <-> Model conversion to separate module Front changes Downgrade apollo-client: https://github.com/Akryum/vue-apollo/issues/577 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
96 lines
2.3 KiB
Elixir
96 lines
2.3 KiB
Elixir
# 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
|
|
|
|
defmodule MobilizonWeb.Plugs.UploadedMedia do
|
|
@moduledoc """
|
|
Serves uploaded media files
|
|
"""
|
|
|
|
import Plug.Conn
|
|
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
|
|
|
|
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, "\"", "\\\"")
|
|
|
|
conn
|
|
|> put_resp_header("content-disposition", "filename=\"#{name}\"")
|
|
|
|
conn ->
|
|
conn
|
|
end
|
|
|
|
config = Mobilizon.CommonConfig.get([MobilizonWeb.Upload])
|
|
|
|
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 =
|
|
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
|
|
|> send_resp(404, "Not found")
|
|
|> halt()
|
|
end
|
|
end
|
|
|
|
defp get_media(conn, {:url, url}, true, _) do
|
|
conn
|
|
|> MobilizonWeb.ReverseProxy.call(
|
|
url,
|
|
Mobilizon.CommonConfig.get([Mobilizon.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
|
|
|> send_resp(500, "Internal Error")
|
|
|> halt()
|
|
end
|
|
end
|