Sha256: 056e78467802d7f7dd15e96d10e6f646417104fb7a5bf7711ea53daa74a962e4

Contents?: true

Size: 1.54 KB

Versions: 54

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.zero?
      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

54 entries across 54 versions & 1 rubygems

Version Path
active-fedora-15.0.1 lib/active_fedora/file/streaming.rb
active-fedora-15.0.0 lib/active_fedora/file/streaming.rb
active-fedora-14.0.1 lib/active_fedora/file/streaming.rb
active-fedora-14.0.0 lib/active_fedora/file/streaming.rb
active-fedora-13.3.0 lib/active_fedora/file/streaming.rb
active-fedora-12.2.4 lib/active_fedora/file/streaming.rb
active-fedora-13.2.7 lib/active_fedora/file/streaming.rb
active-fedora-13.2.5 lib/active_fedora/file/streaming.rb
active-fedora-13.2.4 lib/active_fedora/file/streaming.rb
active-fedora-12.2.3 lib/active_fedora/file/streaming.rb
active-fedora-13.2.3 lib/active_fedora/file/streaming.rb
active-fedora-13.2.2 lib/active_fedora/file/streaming.rb
active-fedora-13.2.0 lib/active_fedora/file/streaming.rb
active-fedora-13.1.3 lib/active_fedora/file/streaming.rb
active-fedora-11.5.6 lib/active_fedora/file/streaming.rb
active-fedora-12.2.2 lib/active_fedora/file/streaming.rb
active-fedora-11.2.1 lib/active_fedora/file/streaming.rb
active-fedora-12.2.1 lib/active_fedora/file/streaming.rb
active-fedora-12.0.3 lib/active_fedora/file/streaming.rb
active-fedora-11.5.5 lib/active_fedora/file/streaming.rb