Class: Lux::Response::File
Defined Under Namespace
Classes: OPTS
Constant Summary collapse
- MIMME_TYPES =
{ css: 'text/css', doc: 'application/msword', eot: 'application/vnd.ms-fontobject', gif: 'image/gif', gz: 'application/x-gzip', html: 'text/html', ico: 'image/png', # image/x-icon jpeg: 'image/jpeg', jpg: 'image/jpeg', js: 'text/javascript', json: 'application/json', map: 'application/json', mp3: 'application/mp3', otf: 'application/font-otf', png: 'image/png', svg: 'image/svg+xml', text: 'text/plain', ttf: 'application/font-ttf', txt: 'text/plain', webp: 'image/webp', woff: 'application/x-font-woff', woff2: 'application/x-font-woff', xml: 'application/xml', zip: 'application/x-gzip' }
Class Method Summary collapse
Instance Method Summary collapse
- #etag(key) ⇒ Object
-
#initialize(in_opts = {}) ⇒ File
constructor
all parametars are optional :name - file name :content_type - string type :inline - sets disposition to inline if true :disposition - inline or attachment :content - raw file data.
- #is_static_file? ⇒ Boolean
- #send ⇒ Object
Constructor Details
#initialize(in_opts = {}) ⇒ File
all parametars are optional :name - file name :content_type - string type :inline - sets disposition to inline if true :disposition - inline or attachment :content - raw file data
58 59 60 61 62 63 64 65 66 |
# File 'lib/lux/response/lib/file.rb', line 58 def initialize in_opts = {} opts = OPTS.new **in_opts opts.disposition ||= opts.inline.class == TrueClass ? 'inline' : 'attachment' opts.file = Pathname.new(opts.file) unless opts.file.class == Pathname opts.path = opts.file.to_s opts.ext = opts.path.include?('.') ? opts.path.split('.').last.to_sym : nil @opts = opts end |
Class Method Details
.deliver_from_current ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/lux/response/lib/file.rb', line 7 def deliver_from_current path = './public' + Lux.current.request.path ext = path.split('.').last return unless ext.length > 1 && ext.length < 5 rack_file = new file: path, inline: true rack_file.send if rack_file.is_static_file? end |
.type(name) ⇒ Object
15 16 17 |
# File 'lib/lux/response/lib/file.rb', line 15 def type name MIMME_TYPES[name.to_sym] end |
Instance Method Details
#etag(key) ⇒ Object
76 77 78 79 |
# File 'lib/lux/response/lib/file.rb', line 76 def etag key response.headers['etag'] = '"%s"' % key response.body('not-modified', status: 304) if request.env['HTTP_IF_NONE_MATCH'] == key end |
#is_static_file? ⇒ Boolean
71 72 73 74 |
# File 'lib/lux/response/lib/file.rb', line 71 def is_static_file? return false unless @opts.ext @opts.file.exist? end |
#send ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lux/response/lib/file.rb', line 81 def send @opts.name ||= @opts.path.split('/').last if @opts.disposition == 'attachment' response.headers['content-disposition'] = 'attachment; filename=%s' % @opts.name end response.content_type(@opts.content_type || MIMME_TYPES[@opts.ext || '_'] || 'application/octet-stream') response.headers['access-control-allow-origin'] ||= '*' if @opts.content etag Crypt.sha1 @opts.content response.body @opts.content else raise Lux::Error.not_found('File not found') unless @opts.file.exist? file_mtime = @opts.file.mtime.utc.to_s response.headers['last-modified'] = file_mtime etag Crypt.sha1(@opts.path + (@opts.content || file_mtime.to_s)) response.body @opts.file.read end end |