Sha256: 9c4d457c3b48a0944359abbfa25403ca6ce4a701ff9318f363f32c5ca3779925

Contents?: true

Size: 1.18 KB

Versions: 18

Compression:

Stored size: 1.18 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
      Net::HTTP.start(uri.host, uri.port) do |http|
        request = Net::HTTP::Get.new uri, headers
        http.request request do |response|
          raise "Couldn't get data from Fedora (#{uri}). Response: #{response.code}" unless response.is_a?(Net::HTTPSuccess)
          response.read_body do |chunk|
            yield chunk
          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

18 entries across 18 versions & 1 rubygems

Version Path
active-fedora-9.7.3 lib/active_fedora/file/streaming.rb
active-fedora-9.7.2 lib/active_fedora/file/streaming.rb
active-fedora-9.10.4 lib/active_fedora/file/streaming.rb
active-fedora-9.10.3 lib/active_fedora/file/streaming.rb
active-fedora-9.10.2 lib/active_fedora/file/streaming.rb
active-fedora-9.10.1 lib/active_fedora/file/streaming.rb
active-fedora-9.10.0 lib/active_fedora/file/streaming.rb
active-fedora-9.10.0.pre2 lib/active_fedora/file/streaming.rb
active-fedora-9.10.0.pre1 lib/active_fedora/file/streaming.rb
active-fedora-9.9.1 lib/active_fedora/file/streaming.rb
active-fedora-9.9.0 lib/active_fedora/file/streaming.rb
active-fedora-9.8.2 lib/active_fedora/file/streaming.rb
active-fedora-9.8.1 lib/active_fedora/file/streaming.rb
active-fedora-9.8.0 lib/active_fedora/file/streaming.rb
active-fedora-9.7.1 lib/active_fedora/file/streaming.rb
active-fedora-9.7.0 lib/active_fedora/file/streaming.rb
active-fedora-9.6.2 lib/active_fedora/file/streaming.rb
active-fedora-9.6.1 lib/active_fedora/file/streaming.rb