问题描述
我有以下问题.声音从公共文件夹中隐藏,因为只有某些用户应该有权访问声音文件.所以我做了一个方法,它的作用就像一个声音的url,但首先计算当前用户是否被允许访问这个文件.
I have the following problem. Sounds are hidden from the public folder, cause there are only certain Users who should have access to the sound files. So I made a certain method, which acts like a sound url, but calculates first, whether the current user is allowed to access this file.
文件由 send_data 方法发送.问题只是,即使它可以正常工作,我的工作也会很慢......我用来播放声音的 jplayer 插件的开发人员告诉我,我应该能够接受字节范围请求以使其正常工作...
The file gets sent by the send_data method. The problem is just, that I it works quite slow if it works even... The developer of the jplayer plugin, which I use to play the sound, told me that I should be able to accept byte range requests to make it work properly...
如何在 Rails 控制器中通过 send_data 或 send_file 发送文件来做到这一点?
How can I do this within a rails controller by sending the file with send_data or send_file?
谢谢,马库斯
推荐答案
我已经能够使用 send_file 成功地提供文件.虽然我遇到了一个问题,但寻找歌曲的较早部分会导致一个新的请求,这使得歌曲从 0:00 重新开始,而不是从搜索栏的真实位置重新开始.到目前为止,这是我为我工作的:
I've been able to serve up the files with some success using send_file. Although I have one hitch, seeking to an earlier part of the song causes a new request which makes the song restart from 0:00 instead of the true location from the seekbar. This is what I have working for me so far:
file_begin = 0
file_size = @media.file_file_size
file_end = file_size - 1
if !request.headers["Range"]
status_code = "200 OK"
else
status_code = "206 Partial Content"
match = request.headers['range'].match(/bytes=(d+)-(d*)/)
if match
file_begin = match[1]
file_end = match[1] if match[2] && !match[2].empty?
end
response.header["Content-Range"] = "bytes " + file_begin.to_s + "-" + file_end.to_s + "/" + file_size.to_s
end
response.header["Content-Length"] = (file_end.to_i - file_begin.to_i + 1).to_s
response.header["Last-Modified"] = @media.file_updated_at.to_s
response.header["Cache-Control"] = "public, must-revalidate, max-age=0"
response.header["Pragma"] = "no-cache"
response.header["Accept-Ranges"]= "bytes"
response.header["Content-Transfer-Encoding"] = "binary"
send_file(DataAccess.getUserMusicDirectory(current_user.public_token) + @media.sub_path,
:filename => @media.file_file_name,
:type => @media.file_content_type,
:disposition => "inline",
:status => status_code,
:stream => 'true',
:buffer_size => 4096)
这篇关于rails 媒体文件流通过 send_data 或 send_file 方法接受字节范围请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!