加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rails媒体文件流通过send_data或send_file方法

发布时间:2020-12-16 19:02:00 所属栏目:百科 来源:网络整理
导读:我有以下问题.声音在公共文件夹中隐藏,因为只有某些用户应该有权访问声音文件.所以我做了一个特定的方法,它就像一个声音网址,但首先计算,是否允许当前用户访问该文件. 该文件由send_data方法发送.问题是,如果它的工作原理我工作得很慢……我用来播放声音的jp
我有以下问题.声音在公共文件夹中隐藏,因为只有某些用户应该有权访问声音文件.所以我做了一个特定的方法,它就像一个声音网址,但首先计算,是否允许当前用户访问该文件.

该文件由send_data方法发送.问题是,如果它的工作原理我工作得很慢……我用来播放声音的jplayer插件的开发者告诉我,我应该能够接受字节范围请求以使其正常工作…

如何通过send_data或send_file发送文件在rails控制器中执行此操作?

谢谢,
马库斯

解决方法

我已经能够使用send_file成功提供文件了.虽然我有一个故障,寻找歌曲的早期部分会导致一个新的请求,使歌曲从0:00重新开始,而不是从搜索栏的真实位置.到目前为止,这是我为我工作的:
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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读