Class: PaperclipDatabaseStorage::AttachmentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/paperclip_database_storage/attachments_controller.rb

Instance Method Summary collapse

Instance Method Details

#get_attachmentObject

Raises:

  • (ActionController::RoutingError)


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/paperclip_database_storage/attachments_controller.rb', line 2

def get_attachment
  conditions = {}
  conditions[:attached_type] = params[:class].singularize.camelize(:upper) if params[:class]
  conditions[:attached_id] = params[:id] if params[:id]
  conditions[:attached_id] ||= params[:id_partition].gsub(/\//, '').to_i if params[:id_partition]
  conditions[:attachment_name] = params[:attachment].singularize if params[:attachment]
  conditions[:style] = params[:style] if params[:style]

  attachments = PaperclipDatabaseStorage::Attachment.where(conditions)

  raise ActionController::RoutingError.new('Image not Found') if attachments.empty?
  raise ActionController::RoutingError.new('Too many images found. Check your route definition') if attachments.length > 1

  attachment = attachments.first

  original_filename = attachment.attached.send(attachment.attached.attachment_definitions.select { |k, v| v[:storage] == :database }.keys.first).original_filename
  original_extension = File.extname(original_filename)
  filename = params[:filename] || original_filename
  filename = "#{filename}#{original_extension}" unless filename =~ /#{original_extension}$/
  file_data = attachment.base64_encoded ? Base64.decode64(attachment.file_data) : attachment.file_data

  send_data file_data,
    :type => attachment.content_type,
    :disposition => (attachment.content_type.strip =~ /^image/ ? 'inline' : 'attachment'),
    :filename => filename
end