instance method send_blob_byte_range_data

Ruby on Rails 8.0.4

Since v7.0.10 Private

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

send_blob_byte_range_data(blob, range_header, disposition: nil)

Stream the blob in byte ranges specified through the header

Parameters

blob req
range_header req
disposition key = nil
Source
# File activestorage/app/controllers/concerns/active_storage/streaming.rb, line 14
    def send_blob_byte_range_data(blob, range_header, disposition: nil)
      ranges = Rack::Utils.get_byte_ranges(range_header, blob.byte_size)

      return head(:range_not_satisfiable) if ranges.blank? || ranges.all?(&:blank?)

      if ranges.length == 1
        range = ranges.first
        content_type = blob.content_type_for_serving
        data = blob.download_chunk(range)

        response.headers["Content-Range"] = "bytes #{range.begin}-#{range.end}/#{blob.byte_size}"
      else
        boundary = SecureRandom.hex
        content_type = "multipart/byteranges; boundary=#{boundary}"
        data = +""

        ranges.compact.each do |range|
          chunk = blob.download_chunk(range)

          data << "\r\n--#{boundary}\r\n"
          data << "Content-Type: #{blob.content_type_for_serving}\r\n"
          data << "Content-Range: bytes #{range.begin}-#{range.end}/#{blob.byte_size}\r\n\r\n"
          data << chunk
        end

        data << "\r\n--#{boundary}--\r\n"
      end

      response.headers["Accept-Ranges"] = "bytes"
      response.headers["Content-Length"] = data.length.to_s

      send_data(
        data,
        disposition: blob.forced_disposition_for_serving || disposition || DEFAULT_BLOB_STREAMING_DISPOSITION,
        filename: blob.filename.sanitized,
        status: :partial_content,
        type: content_type
      )
    end

Defined in activestorage/app/controllers/concerns/active_storage/streaming.rb line 14 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveStorage::Streaming

Type at least 2 characters to search.

↑↓ navigate · open · esc close