Module: Cms::S3::AttachmentsController

Defined in:
lib/bcms_s3/s3_module.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller_class) ⇒ Object



11
12
13
# File 'lib/bcms_s3/s3_module.rb', line 11

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

Instance Method Details

#show_with_s3Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bcms_s3/s3_module.rb', line 15

def show_with_s3
  @attachment = ::Attachment.find(params[:id])
  @attachment = @attachment.as_of_version(params[:version]) if params[:version]
  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