Class: PreviewController

Inherits:
ActionController::Base
  • Object
show all
Defined in:
app/controllers/preview_controller.rb

Constant Summary collapse

Languages =
[:ruby, :python, :java, :js, :scss, :sass, :haml, :json, 
:go, :sql, :yaml, :c, :coffee, :properties, :clojure]

Instance Method Summary collapse

Instance Method Details

#downloadObject



47
48
49
50
51
52
53
54
55
# File 'app/controllers/preview_controller.rb', line 47

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

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/preview_controller.rb', line 6

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

#openObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/preview_controller.rb', line 33

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