rails media file stream accept byte range request through send_data or send_file method
Asked Answered
C

4

12

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

Cavalcade answered 20/7, 2011 at 9:3 Comment(3)
which web server are you using?Farthingale
apache 2 with passenger modulCavalcade
have you tried gist.github.com/mikhailov/3174601 ?Puttier
T
12

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)
Transept answered 29/9, 2011 at 23:3 Comment(1)
i just tried this and it works. altough the streaming options (:stream and :buffer_size) are not actually needed here.Barbate
A
4

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:

  def stream_data data, options={}
    range_start = 0
    file_size = data.length
    range_end = file_size - 1
    status_code = "200"

    if request.headers["Range"]
      status_code = "206"
      request.headers['range'].match(/bytes=(\d+)-(\d*)/).try do |match|
        range_start = match[1].to_i
        range_end = match[2].to_i unless match[2]&.empty?
      end
      response.header["Content-Range"] = "bytes #{range_start}-#{range_end}/#{file_size}"
    end

    response.header["Content-Length"] = (range_end - range_start + 1).to_s
    response.header["Accept-Ranges"] = "bytes"

    send_data(data[range_start, range_end],
              filename: options[:filename],
              type: options[:type],
              disposition: "inline",
              status: status_code)
  end
Alisaalisan answered 4/9, 2019 at 10:6 Comment(0)
C
2

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.

the_file = File.open(file_path)

file_begin = 0
file_size = the_file.size
file_end = file_size - 1

if request.headers['Range']
  status_code = :partial_content

  match = request.headers['range'].match(/bytes=(\d+)-(\d*)/)

  if match
    file_begin = match[1]
    file_end = match[1]  if match[2] and not match[2].empty?
  end

  response.headers['Content-Range'] = "bytes #{file_begin}-#{file_end.to_i + (match[2] == '1' ? 1 : 0)}/#{file_size}"
else
  status_code = :ok
end

response.headers['Content-Length'] = (file_end.to_i - file_begin.to_i + 1).to_s
response.headers['Last-Modified'] = the_file.mtime

response.headers['Cache-Control'] = 'public, must-revalidate, max-age=0'
response.headers['Pragma'] = 'no-cache'
response.headers['Accept-Ranges'] = 'bytes'
response.headers['Content-Transfer-Encoding'] = 'binary'

require 'ogginfo-rb'
ogginfo = Ogg::Info::open(the_file.path.gsub(/.mp3|.wav/,'.ogg'))
duration = ogginfo.duration.to_f

response.headers['Content-Duration'] = duration
response.headers['X-Content-Duration'] = duration

send_file file_path,
          filename: "#{call.id}.#{ext}",
          type: Mime::Type.lookup_by_extension(ext),
          status: status_code,
          disposition: 'inline',
          stream: 'true',
          buffer_size: 32768
Clutch answered 16/10, 2013 at 20:18 Comment(1)
The part around the 'Accept-Ranges' header is exactly what was needed, to instrument NGINX that it sends byte-ranges. We can not rely on Content-Duration, because we are sending not only audios, but some types of videos as well. So we skipped that part, and that seems to work just fine.Fervent
A
0

Another amended version - I was trying to download a zip file as binary content and this is what worked for me -

  def byte_range_response (request, response, content)
    file_begin = 0
    file_size = content.bytesize
    file_end = file_size - 1

    status_code = '206 Partial Content'
    match = request.headers['range'].match(/bytes=(\d+)-(\d*)/)
    if match
      file_begin = match[1]
      file_end = match[2] if match[2] && !match[2].empty?
    end
    content_length = file_end.to_i - file_begin.to_i + 1
    response.header['Content-Range'] = 'bytes ' + file_begin.to_s + '-' + file_end.to_s + '/' + file_size.to_s
    response.header['Content-Length'] = content_length.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_data get_partial_content(content, content_length, file_begin.to_i), type: 'application/octet-stream', status: status_code
  end

  def get_partial_content(content, content_length, offset)
    test_file = Tempfile.new(['test-file', '.zip'])
    test_file.puts(content)
    partial_content = IO.binread(test_file.path, content_length, offset)
    test_file.close
    test_file.unlink
    partial_content
  end
Ammunition answered 28/4, 2015 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.