Sha256: 9e2bd1932a058250fe7abd5988e3e55f7deeeef9525023509fecfeea17b64406
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
class PreviewController < ActionController::Base Languages = [:ruby, :python, :java, :js, :scss, :sass, :haml, :json, :go, :sql, :yaml, :c, :coffee, :properties, :clojure] def index clazz, id = params[:type].classify.constantize, params[:id] r = clazz.find(id) if r mime = r.content_type type, ext = mime[/^[^\/]+/].to_sym, mime[/(?<=\/)(x-)?(.+)/, 2].to_sym if Languages.include?(ext) @code = CodeRay.scan(r.content, ext).div(:line_numbers => :table, :css => :class) render 'code', layout: 'coderay' elsif [:pdf, :html].include?(ext) render_native(r.content, mime) else case type when :text render_native(r.content, 'text/plain') when :image, :video, :audio render_native(r.content, mime) else @resource = r render 'download', layout: 'coderay' end end else render text: "#{r} or #{r.content_type} is not acceptable.", content_type: 'text/plain' end end def open case RUBY_PLATFORM when /darwin/i open_osx_file when /linux/i open_linux_file when /cygwin|mswin|mingw|bccwin|wince|emx/ open_windows_file else #other end head :no_content end def download clazz, id = params[:type].classify.constantize, params[:id] r = clazz.find(id) if r render_native(r.content, r.content_type || 'application/octet-stream') else head :no_content end end private def open_osx_file path = params[:path] app = 'Preview' if /^text\/.+$/ =~ params[:mime] app = 'TextMate' end system %Q{open -a #{app} "#{path}"} end def render_native(content, content_type) response.headers['Cache-Control'] = "public, max-age=#{12.hours.to_i}" response.headers['Content-Type'] = content_type response.headers['Content-Disposition'] = 'inline' render text: content, content_type: content_type end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mulberry_preview-0.0.8 | app/controllers/preview_controller.rb |