Sha256: 282ded8e8a29e4eef415f03d176658c956a7a912eba4e1606763edb43400062a

Contents?: true

Size: 1012 Bytes

Versions: 2

Compression:

Stored size: 1012 Bytes

Contents

# encoding: UTF-8

module Mojito::Rendering
	
	module File
		require 'pathname'
		require 'mime/types'
		require 'time'
		
		def file!(filename)
			path = Pathname === filename ? filename : Pathname.new(filename.to_s)
			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

2 entries across 2 versions & 1 rubygems

Version Path
mojito-0.1.1 lib/mojito/rendering/file.rb
mojito-0.1.0 lib/mojito/rendering/file.rb