Sha256: 135fd7b26afb945a52e0a35613884ccb2bb2fcd946dab084efed6b1771836c4d

Contents?: true

Size: 975 Bytes

Versions: 21

Compression:

Stored size: 975 Bytes

Contents

# frozen_string_literal: true

module PagesCore
  module RangedResponse
    extend ActiveSupport::Concern

    def send_ranged_data(data, options = {})
      response.header["Content-Length"] = data.length
      response.header["Accept-Ranges"] = "bytes"

      content_range_headers(data.length) if ranged_request?

      send_data(ranged_data(data),
                options.merge(status: ranged_request? ? 206 : 200))
    end

    private

    def content_range(length)
      Rack::Utils.byte_ranges(request.headers, length)[0]
    end

    def content_range_headers(size)
      bytes = content_range(size)
      response.header["Content-Length"] = bytes.end - bytes.begin + 1
      response.header["Content-Range"] =
        "bytes #{bytes.begin}-#{bytes.end}/#{size}"
    end

    def ranged_data(data)
      return data unless ranged_request?

      data[content_range(data.length)]
    end

    def ranged_request?
      request.headers["HTTP_RANGE"]
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
pages_core-3.15.5 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.15.4 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.15.3 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.15.2 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.15.1 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.14.0 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.13.0 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.7 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.6 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.5 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.4 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.3 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.2 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.1 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.12.0 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.11.3 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.11.2 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.11.1 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.11.0 app/controllers/concerns/pages_core/ranged_response.rb
pages_core-3.10.2 app/controllers/concerns/pages_core/ranged_response.rb