Sha256: ba97b68525f131f8b5e9f0e035eb9b925071b14bb9f7cbf4af0f8ff06bd40cdc

Contents?: true

Size: 992 Bytes

Versions: 4

Compression:

Stored size: 992 Bytes

Contents

# encoding: UTF-8

module Mojito::Rendering
	
	module File
		require 'pathname'
		require 'mime/types'
		require 'time'
		require 'where'
		
		def file!(filename)
			path = Where.cdir(1) + filename
			restrict_path! path if respond_to? :restrict_path!
			if path.readable? and path.file?
				body = FileResponse.new path
				halt! [response.status, response.headers.merge(body.compute_headers), body]
			else
				not_found!
			end
		end
		
		class FileResponse
			
			def initialize(pathname)
				@pathname = pathname
			end
			
			def each(&block)
				@pathname.open do |f|
					yield f.read
				end
			end
			
			def compute_headers
				{ 'Content-Type' => mime_type.to_s, 'Content-Length' => size.to_s, 'Last-Modified' => @pathname.mtime.rfc2822 }
			end
			
			def mime_type
				MIME::Types.type_for(@pathname.to_s).first || MIME::Types['application/octet-stream'].first
			end
			
			def size
				@pathname.size
			end
			
			def to_path
				@pathname.to_s
			end
			
		end
		
	end
	
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mojito-0.2.6 lib/mojito/rendering/file.rb
mojito-0.2.5 lib/mojito/rendering/file.rb
mojito-0.2.4 lib/mojito/rendering/file.rb
mojito-0.2.3 lib/mojito/rendering/file.rb