live_beats/lib/live_beats_web/controllers/file_controller.ex

97 lines
2.8 KiB
Elixir
Raw Normal View History

2021-11-06 03:02:31 +00:00
defmodule LiveBeatsWeb.FileController do
2021-11-15 18:06:04 +00:00
@moduledoc """
Serves files based on short-term token grants.
"""
2021-11-06 03:02:31 +00:00
use LiveBeatsWeb, :controller
alias LiveBeats.MediaLibrary
require Logger
2022-01-27 19:41:26 +00:00
def show(conn, %{"id" => filename_uuid, "token" => token}) do
path = MediaLibrary.local_filepath(filename_uuid)
mime_type = MIME.from_path(path)
case Phoenix.Token.decrypt(conn, "file", token, max_age: :timer.minutes(1)) do
2022-01-27 18:03:42 +00:00
{:ok, %{vsn: 1, uuid: ^filename_uuid, ip: ip, size: size}} ->
2022-01-27 19:41:26 +00:00
if local_file?(filename_uuid, ip) do
Logger.info("serving file from #{server_ip()}")
do_send_file(conn, path)
else
Logger.info("proxying file to #{ip} from #{server_ip()}")
2022-01-27 18:03:42 +00:00
proxy_file(conn, ip, mime_type, size)
end
{:ok, _} ->
send_resp(conn, :unauthorized, "")
{:error, _} ->
send_resp(conn, :unauthorized, "")
2021-11-12 03:42:10 +00:00
end
end
defp do_send_file(conn, path) do
# accept-ranges headers required for chrome to seek via currentTime
conn
2021-11-15 18:06:04 +00:00
|> put_resp_header("content-type", MIME.from_path(path))
2021-11-12 03:42:10 +00:00
|> put_resp_header("accept-ranges", "bytes")
|> send_file(200, path)
2021-11-06 03:02:31 +00:00
end
2022-01-27 18:03:42 +00:00
defp proxy_file(conn, ip, mime_type, content_length) do
uri = conn |> request_url() |> URI.parse()
port = LiveBeatsWeb.Endpoint.config(:http)[:port]
2022-01-27 19:41:26 +00:00
path = uri.path <> "?" <> uri.query <> "&from=#{server_ip()}"
{:ok, ipv6} = :inet.parse_address(String.to_charlist(ip))
{:ok, req} = Mint.HTTP.connect(:http, ipv6, port, file_server_opts())
{:ok, req, request_ref} = Mint.HTTP.request(req, "GET", path, [], "")
conn
|> put_resp_header("content-type", mime_type)
|> put_resp_header("accept-ranges", "bytes")
2022-01-27 19:41:26 +00:00
|> put_resp_header("content-length", to_string(content_length))
|> send_chunked(200)
|> stream(req, request_ref)
end
defp stream(conn, req, ref) do
receive do
{:tcp, _, _} = msg ->
{:ok, req, responses} = Mint.HTTP.stream(req, msg)
new_conn =
Enum.reduce(responses, conn, fn
{:data, ^ref, data}, acc -> chunk!(acc, data)
{:done, ^ref}, acc -> halt(acc)
{:status, ^ref, 200}, acc -> acc
{:headers, ^ref, _}, acc -> acc
end)
if new_conn.halted do
new_conn
else
stream(new_conn, req, ref)
end
end
end
defp chunk!(conn, data) do
{:ok, conn} = chunk(conn, data)
conn
end
defp local_file?(_filename_uuid, ip) do
# TODO cache locally
ip == server_ip()
end
defp server_ip, do: LiveBeats.config([:files, :server_ip])
defp file_server_opts do
[
hostname: LiveBeats.config([:files, :hostname]) || "localhost",
transport_opts: LiveBeats.config([:files, :transport_opts]) || []
]
end
2021-11-06 03:02:31 +00:00
end