Module: Cms::S3::ContentController

Defined in:
lib/bcms_s3/s3_module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller_class) ⇒ Object



41
42
43
44
# File 'lib/bcms_s3/s3_module.rb', line 41

def self.included(controller_class)
  controller_class.alias_method_chain :render_page_with_caching, :s3
  controller_class.alias_method_chain :try_to_stream_file, :s3
end

Instance Method Details

#render_page_with_caching_with_s3Object



45
46
47
48
# File 'lib/bcms_s3/s3_module.rb', line 45

def render_page_with_caching_with_s3
  render_page
  response.headers['Cache-Control'] = 'public, max-age=300' if Cms::S3.heroku_caching
end

#try_to_stream_file_with_s3Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bcms_s3/s3_module.rb', line 49

def try_to_stream_file_with_s3
  split = @paths.last.to_s.split('.')
  ext  = split.size > 1 ? split.last.to_s.downcase : nil

  #Only try to stream cache file if it has an extension
  unless ext.blank?

    #Check access to file
    @attachment    = ::Attachment.find_live_by_file_path(@path)
    if @attachment
      raise Cms::Errors::AccessDenied unless current_user.able_to_view?(@attachment)

      if Cms::S3.enabled
        #get the file off S3
        if Cms::S3.options[:s3_cname]
          redirect_to("http://#{Cms::S3.options[:s3_cname]}/#{@attachment.file_location}")
        else
          redirect_to("http://#{Cms::S3.options[:bucket]}.s3.amazonaws.com/#{@attachment.file_location}")
        end
      else
        #Construct a path to where this file would be if it were cached
        @file = @attachment.full_file_location

        #Stream the file if it exists
        if @path != "/" && File.exists?(@file)
          send_file(@file, 
          :filename    => @attachment.file_name,
          :type        => @attachment.file_type,
          :disposition => "inline"
          )
        end
      end
    end
  end
end