Class: DAV4Rack::RemoteFile

Inherits:
Rack::File
  • Object
show all
Defined in:
lib/dav4rack/remote_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, args = {}) ⇒ RemoteFile

path

path to file (Actual path, preferably a URL since this is a REMOTE file)

args

Hash of arguments:

:size -> Integer - number of bytes
:mime_type -> String - mime type
:last_modified -> String/Time - Time of last modification
:sendfile -> True or String to define sendfile header variation
:cache_directory -> Where to store cached files
:cache_ref -> Reference to be used for cache file name (useful for changing URLs like S3)
:sendfile_prefix -> String directory prefix. Eg: 'webdav' will result in: /wedav/#{path.sub('http://', '')}
:sendfile_fail_gracefully -> Boolean if true will simply proxy if unable to determine proper sendfile


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dav4rack/remote_file.rb', line 24

def initialize(path, args={})
  @path = path
  @args = args
  @heads = {}
  @cache_file = args[:cache_directory] ? cache_file_path : nil
  @redefine_prefix = nil
  if(@cache_file && File.exists?(@cache_file))
    @root = ''
    @path_info = @cache_file
    @path = @path_info
  elsif(args[:sendfile])
    @redefine_prefix = 'sendfile'
    @sendfile_header = args[:sendfile].is_a?(String) ? args[:sendfile] : nil
  else
    setup_remote
  end
  do_redefines(@redefine_prefix) if @redefine_prefix
end

Instance Attribute Details

#pathObject Also known as: to_path

Returns the value of attribute path.



10
11
12
# File 'lib/dav4rack/remote_file.rb', line 10

def path
  @path
end

Instance Method Details

#call(env) ⇒ Object

env

Environment variable hash

Process the call



45
46
47
# File 'lib/dav4rack/remote_file.rb', line 45

def call(env)
  serving(env)
end

#remote_eachObject

Get the remote file



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dav4rack/remote_file.rb', line 82

def remote_each
  if(@store)
    yield @store
  else
    @con.request_get(@call_path) do |res|
      res.read_body(@store) do |part|
        @cf.write part if @cf
        yield part
      end
    end
  end
end

#remote_serving(e) ⇒ Object

env

Environment variable hash

Return self to be processed



73
74
75
76
77
78
79
# File 'lib/dav4rack/remote_file.rb', line 73

def remote_serving(e)
  [200, {
         "Last-Modified"  => last_modified,
         "Content-Type"   => content_type,
         "Content-Length" => size
        }, self]
end

#sendfile_serving(env) ⇒ Object

env

Environment variable hash

Return an empty result with the proper header information



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dav4rack/remote_file.rb', line 51

def sendfile_serving(env)
  header = @sendfile_header || env['sendfile.type'] || env['HTTP_X_SENDFILE_TYPE']
  unless(header)
    raise 'Failed to determine proper sendfile header value' unless @args[:sendfile_fail_gracefully]
    setup_remote
    do_redefines('remote')
    call(env)
  end
  prefix = (@args[:sendfile_prefix] || env['HTTP_X_ACCEL_REMOTE_MAPPING']).to_s.sub(/^\//, '').sub(/\/$/, '')
  [200, {
         "Last-Modified" => last_modified,
         "Content-Type" => content_type,
         "Content-Length" => size,
         "Redirect-URL" => @path,
         "Redirect-Host" => @path.scan(%r{^https?://([^/\?]+)}).first.first,
         header => "/#{prefix}"
        },
  ['']]
end

#sizeObject

Size based on remote headers or given size



96
97
98
# File 'lib/dav4rack/remote_file.rb', line 96

def size
  @heads['content-length'] || @size
end