The*_*uad 5 elixir cowboy phoenix-framework
我目前正在使用phoenix开发一个网站,并有一个应在后台播放的视频部分。
尽管它在Chrome和Firefox上运行良好,但在Safari上却无法运行。
我怀疑这是因为牛仔没有正确提供HTTP范围请求。
有没有一种方法可以启用(如果默认情况下禁用)?
$ curl -H Range:bytes=16- -I http://localhost:4000/videos/vid_home_1.mp4
HTTP/1.1 200 OK
server: Cowboy
date: Tue, 12 Apr 2016 14:41:20 GMT
content-length: 633787
cache-control: public
etag: 480A03F
content-type: video/mp4
Run Code Online (Sandbox Code Playgroud)
nginx服务器显示为206时:
$ curl -H Range:bytes=16- -I http://localhost/videos/vid_home_1.mp4
HTTP/1.1 206 Partial Content
Server: nginx/1.8.0
Date: Tue, 12 Apr 2016 14:46:17 GMT
Content-Type: video/mp4
Content-Length: 633771
Last-Modified: Mon, 11 Apr 2016 12:26:26 GMT
Connection: keep-alive
ETag: "570b97f2-9abbb"
Content-Range: bytes 16-633786/633787
Run Code Online (Sandbox Code Playgroud)
我找到了一种用插头自己做的方法...所以如果有人想用 Phoenix / Elixir 提供范围请求,这就是你必须做的(这是非常基本的,不考虑 rfc)
defmodule Plug.Range do
@behaviour Plug
@allowed_methods ~w(GET HEAD)
import Plug.Conn
def init(options) do
options
end
def call(conn, _opts) do
if (Enum.empty?(Plug.Conn.get_req_header(conn, "range"))) do
conn
else
file_path = "priv/static" <> conn.request_path
if File.exists? file_path do
stats = File.stat! file_path
filesize = stats.size
req = Regex.run(~r/bytes=([0-9]+)-([0-9]+)?/, conn |> Plug.Conn.get_req_header("range") |> List.first)
{req_start, _} = req |> Enum.at(1) |> Integer.parse
{req_end, _} = req |> Enum.at(2, filesize |> to_string) |> Integer.parse
file_end = ( filesize - 2) |> to_string
length = req_end - req_start + 1
conn
|> Plug.Conn.put_resp_header("Content-Type", "video/mp4")
|> Plug.Conn.put_resp_header("Accept-Ranges", "bytes")
|> Plug.Conn.put_resp_header("Content-Range", "bytes #{req_start}-#{req_end}/#{filesize}")
|> Plug.Conn.send_file(206, file_path, req_start, length)
|> Plug.Conn.halt
else
conn
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,现在它只会发送“video/mp4”内容类型,但您可以轻松地让某些东西适用于所有情况......
最后,为了使 Plug 正常工作,您需要将其放置在 Project Endpoint 文件中的 Plug.static 之前。
希望它可以帮助某人...
编辑:对于那些感兴趣的人,我为此创建了一个 github/hex.pm 包:
Hex link
github link
| 归档时间: |
|
| 查看次数: |
645 次 |
| 最近记录: |