Class: Utopia::Static::LocalFile

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(root, path) ⇒ LocalFile

Returns a new instance of LocalFile.



31
32
33
34
35
36
37
# File 'lib/utopia/static/local_file.rb', line 31

def initialize(root, path)
	@root = root
	@path = path
	@etag = Digest::SHA1.hexdigest("#{File.size(full_path)}#{mtime_date}")

	@range = nil
end

Instance Attribute Details

#etagObject (readonly)

Returns the value of attribute etag.



41
42
43
# File 'lib/utopia/static/local_file.rb', line 41

def etag
  @etag
end

#pathObject (readonly)

Returns the value of attribute path.



40
41
42
# File 'lib/utopia/static/local_file.rb', line 40

def path
  @path
end

#rangeObject (readonly)

Returns the value of attribute range.



42
43
44
# File 'lib/utopia/static/local_file.rb', line 42

def range
  @range
end

#rootObject (readonly)

Returns the value of attribute root.



39
40
41
# File 'lib/utopia/static/local_file.rb', line 39

def root
  @root
end

Instance Method Details

#bytesizeObject Also known as: size



57
58
59
# File 'lib/utopia/static/local_file.rb', line 57

def bytesize
	File.size(full_path)
end

#eachObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/utopia/static/local_file.rb', line 68

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.

Returns:

  • (Boolean)


62
63
64
# File 'lib/utopia/static/local_file.rb', line 62

def empty?
	bytesize == 0
end

#full_pathObject



49
50
51
# File 'lib/utopia/static/local_file.rb', line 49

def full_path
	File.join(@root, @path.components)
end

#modified?(env) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/utopia/static/local_file.rb', line 83

def modified?(env)
	if modified_since = env['HTTP_IF_MODIFIED_SINCE']
		return false if File.mtime(full_path) <= Time.parse(modified_since)
	end

	if etags = env['HTTP_IF_NONE_MATCH']
		etags = etags.split(/\s*,\s*/)
		return false if etags.include?(etag) || etags.include?('*')
	end

	return true
end

#mtime_dateObject



53
54
55
# File 'lib/utopia/static/local_file.rb', line 53

def mtime_date
	File.mtime(full_path).httpdate
end

#serve(env, response_headers) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/utopia/static/local_file.rb', line 99

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_pathObject

Fit in with Rack::Sendfile



45
46
47
# File 'lib/utopia/static/local_file.rb', line 45

def to_path
	full_path
end