相关:Great post about serving video files, the same stuff can be applied here.
答案 0 :(得分:0)
def send_audio(conn, headers, audio) do
audio_path = build_audio_path(audio)
offset = get_offset(headers)
file_size = get_file_size(audio_path)
conn
|> Plug.Conn.put_resp_header("content-type", audio.content_type)
|> Plug.Conn.put_resp_header("content-range", "bytes #{offset}-#{file_size-1}/#{file_size}")
|> Plug.Conn.send_file(206, audio_path, offset, file_size - offset)
end
def get_offset(headers) do
case List.keyfind(headers, "range", 0) do
{"range", "bytes=" <> start_pos} ->
String.split(start_pos, "-") |> hd |> String.to_integer
nil ->
0
end
end