Sha256: ad7ee66e7bd6bde2a5f43eb3538ce29fa93513a83bdb956d2ab71f63fb4af1e5

Contents?: true

Size: 1.54 KB

Versions: 14

Compression:

Stored size: 1.54 KB

Contents

module ActiveFedora::File::Streaming
  # @param range [String] the Range HTTP header
  # @return [Stream] an object that responds to each
  def stream(range = nil)
    uri = URI.parse(self.uri)
    FileBody.new(uri, headers(range, authorization_key))
  end

  # @param range [String] from #stream
  # @param key [String] from #authorization_key
  # @return [Hash]
  def headers(range, key, result = {})
    result["Range"] = range if range
    result["Authorization"] = key if key
    result
  end

  class FileBody
    attr_reader :uri, :headers
    def initialize(uri, headers)
      @uri = uri
      @headers = headers
    end

    def each(no_of_requests_limit = 3, &block)
      raise ArgumentError, 'HTTP redirect too deep' if no_of_requests_limit == 0
      Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
        request = Net::HTTP::Get.new uri, headers
        http.request request do |response|
          case response
          when Net::HTTPSuccess
            response.read_body do |chunk|
              yield chunk
            end
          when Net::HTTPRedirection
            no_of_requests_limit -= 1
            @uri = URI(response["location"])
            each(no_of_requests_limit, &block)
          else
            raise "Couldn't get data from Fedora (#{uri}). Response: #{response.code}"
          end
        end
      end
    end
  end

  private

    # @return [String] current authorization token from Ldp::Client
    def authorization_key
      ldp_source.client.http.headers.fetch("Authorization", nil)
    end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
active-fedora-10.3.0 lib/active_fedora/file/streaming.rb
active-fedora-10.3.0.rc2 lib/active_fedora/file/streaming.rb
active-fedora-10.3.0.rc1 lib/active_fedora/file/streaming.rb
active-fedora-10.2.1 lib/active_fedora/file/streaming.rb
active-fedora-10.2.0 lib/active_fedora/file/streaming.rb
active-fedora-10.1.0 lib/active_fedora/file/streaming.rb
active-fedora-10.1.0.rc1 lib/active_fedora/file/streaming.rb
active-fedora-10.0.0 lib/active_fedora/file/streaming.rb
active-fedora-10.0.0.beta4 lib/active_fedora/file/streaming.rb
active-fedora-10.0.0.beta3 lib/active_fedora/file/streaming.rb
active-fedora-10.0.0.beta2 lib/active_fedora/file/streaming.rb
active-fedora-10.0.0.beta1 lib/active_fedora/file/streaming.rb
active-fedora-9.13.0 lib/active_fedora/file/streaming.rb
active-fedora-9.12.0 lib/active_fedora/file/streaming.rb