Rails媒体文件流通过send_data或send_file方法接受字节范围请求
我有以下问题。声音在公共文件夹中隐藏,因为只有某些用户有权访问声音文件。所以我做了一个特定的方法,它的作用就像一个声音url,但首先计算当前用户是否允许访问这个文件。
文件通过 send_data 方法发送。问题是,如果它甚至可以工作的话,它的工作速度会非常慢...我用来播放声音的 jplayer 插件的开发人员告诉我,我应该能够接受字节范围请求以使其正常工作...
如何通过使用 send_data 或 send_file 发送文件来在 Rails 控制器中执行此操作?
谢谢, 马库斯
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.
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...
How can I do this within a rails controller by sending the file with send_data or send_file?
Thanks,
Markus
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经能够使用 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:
我使用了加勒特的答案并对其进行了修改(包括一两个错误修复)。我还使用
send_data
而不是从文件中读取:I used Garrett's answer and modified it (including one or two bug fixes). I also used
send_data
instead of reading from a file:这是我的版本。我使用 gem 'ogginfo-rb' 来计算正确提供 ogg 文件所需的持续时间。
ps 我总是有三种格式 - wav、mp3、ogg。
Here is my version. I use gem 'ogginfo-rb' to calculate the duration which is required to serve ogg files properly.
p.s. I always have three formats - wav, mp3, ogg.
另一个修改版本 - 我试图下载一个 zip 文件作为二进制内容,这对我有用 -
Another amended version - I was trying to download a zip file as binary content and this is what worked for me -