Sha256: b575d44c3eae89fd1e1f200c7a93c9fddc638c838ac55b5d7cab1ca42e458253
Contents?: true
Size: 1.27 KB
Versions: 3
Compression:
Stored size: 1.27 KB
Contents
require 'time' require 'rack/utils' module MongoMapperExt class FileServer def initialize(app) @app = app end def call(env) if env["PATH_INFO"] =~ /^\/_files\/([^\/?]+)/ @model = $1.classify.constantize rescue nil return forbidden if @model.nil? dup._call(env) else @app.call(env) end end def _call(env) request = Rack::Request.new(env) params = request.GET @file = @model.find_file_from_params(params, request) return not_found if @file.nil? if @file.present? serving else not_found end end def forbidden body = "Forbidden\n" [403, {"Content-Type" => "text/plain", "Content-Length" => body.size.to_s, "X-Cascade" => "pass"}, [body]] end def serving body = self [200, { "Last-Modified" => Time.now.httpdate, "Content-Type" => @file.content_type, "Content-Length" => @file.size.to_s }, body] end def not_found body = "File not found: #{@path_info}\n" [404, {"Content-Type" => "text/plain", "Content-Length" => body.size.to_s, "X-Cascade" => "pass"}, [body]] end def each f = @file.get while part = f.read(8192) yield part break if part.empty? end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
mongomapper_ext-0.5.2 | lib/mongomapper_ext/file_server.rb |
mongomapper_ext-0.5.1 | lib/mongomapper_ext/file_server.rb |
mongomapper_ext-0.5.0 | lib/mongomapper_ext/file_server.rb |