Class: Utopia::Static::LocalFile
- Inherits:
-
Object
- Object
- Utopia::Static::LocalFile
- Defined in:
- lib/utopia/static/local_file.rb
Overview
Represents a local file on disk which can be served directly, or passed upstream to sendfile.
Constant Summary collapse
- CONTENT_LENGTH =
Rack::CONTENT_LENGTH
- CONTENT_RANGE =
'Content-Range'.freeze
Instance Attribute Summary collapse
-
#etag ⇒ Object
readonly
Returns the value of attribute etag.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #bytesize ⇒ Object (also: #size)
- #each ⇒ Object
-
#empty? ⇒ Boolean
This reflects whether calling each would yield anything.
- #full_path ⇒ Object
-
#initialize(root, path) ⇒ LocalFile
constructor
A new instance of LocalFile.
- #modified?(env) ⇒ Boolean
- #mtime_date ⇒ Object
- #serve(env, response_headers) ⇒ Object
-
#to_path ⇒ Object
Fit in with Rack::Sendfile.
Constructor Details
Instance Attribute Details
#etag ⇒ Object (readonly)
Returns the value of attribute etag.
24 25 26 |
# File 'lib/utopia/static/local_file.rb', line 24 def etag @etag end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/utopia/static/local_file.rb', line 23 def path @path end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
25 26 27 |
# File 'lib/utopia/static/local_file.rb', line 25 def range @range end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
22 23 24 |
# File 'lib/utopia/static/local_file.rb', line 22 def root @root end |
Instance Method Details
#bytesize ⇒ Object Also known as: size
40 41 42 |
# File 'lib/utopia/static/local_file.rb', line 40 def bytesize File.size(full_path) end |
#each ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/utopia/static/local_file.rb', line 51 def each File.open(full_path, "rb") do |file| file.seek(@range.begin) remaining = @range.end - @range.begin+1 while remaining > 0 break unless part = file.read([8192, remaining].min) remaining -= part.length yield part end end end |
#empty? ⇒ Boolean
This reflects whether calling each would yield anything.
45 46 47 |
# File 'lib/utopia/static/local_file.rb', line 45 def empty? bytesize == 0 end |
#full_path ⇒ Object
32 33 34 |
# File 'lib/utopia/static/local_file.rb', line 32 def full_path File.join(@root, @path.components) end |
#modified?(env) ⇒ Boolean
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/utopia/static/local_file.rb', line 66 def modified?(env) if modified_since = env['HTTP_IF_MODIFIED_SINCE'] return false if File.mtime(full_path) <= Time.parse(modified_since) end if = env['HTTP_IF_NONE_MATCH'] = .split(/\s*,\s*/) return false if .include?(etag) || .include?('*') end return true end |
#mtime_date ⇒ Object
36 37 38 |
# File 'lib/utopia/static/local_file.rb', line 36 def mtime_date File.mtime(full_path).httpdate end |
#serve(env, response_headers) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/utopia/static/local_file.rb', line 82 def serve(env, response_headers) ranges = Rack::Utils.get_byte_ranges(env['HTTP_RANGE'], size) response = [200, response_headers, self] # puts "Requesting ranges: #{ranges.inspect} (#{size})" if ranges == nil or ranges.size != 1 # No ranges, or multiple ranges (which we don't support). # TODO: Support multiple byte-ranges, for now just send entire file: response[0] = 200 response[1][CONTENT_LENGTH] = size.to_s @range = 0...size else # Partial content: @range = ranges[0] partial_size = @range.size response[0] = 206 response[1][CONTENT_LENGTH] = partial_size.to_s response[1][CONTENT_RANGE] = "bytes #{@range.min}-#{@range.max}/#{size}" end return response end |
#to_path ⇒ Object
Fit in with Rack::Sendfile
28 29 30 |
# File 'lib/utopia/static/local_file.rb', line 28 def to_path full_path end |