Rachel D Roy
Rachel D Roy

Reputation: 1035

Rails MIME::Types.type_for(photoshop_1354320001.psd) - MIME Type not found?

I'm using Rails 3, Paperclip(3.3.0), aws-sdk (1.7.1).

My paperclip attachments are being stored securely on S3.

attachment.rb

  has_attached_file :attachment,
    :storage => :s3,
    :s3_credentials => "#{Rails.root}/config/s3.yml",
    :s3_protocol => 'https',
    :s3_permissions => :private,  # Sets the file, not the folder as private in S3
    :use_timestamp => false,
    :default_style => :original, # NEEDS to be original or download_url method below wont work
    :default_url => '/images/:attachment/default_:style.png',
    :path => "/:rails_env/private/s/:s_id/uuploaded_files/:basename.:extension"

In order to download the files I generate a secure URL like so:

  def authenticated_url(style = nil, expires_in = 1.hour)
    mime_type = MIME::Types.type_for(self.attachment_file_name)[0]
    attachment.s3_object(style).url_for(:read, :secure => true, :response_content_type => mime_type.to_s, :expires => expires_in).to_s
  end

The problem is for PSDs: This is return empty:

Rails MIME::Types.type_for('photoshop_1354320001.psd')

In the code it looks like:

mime_type = MIME::Types.type_for(self.attachment_file_name)[0]

It works for other files fine but not PSDs. Any idea why and how to resolve?

Thanks

Upvotes: 0

Views: 945

Answers (1)

Ilya O.
Ilya O.

Reputation: 1500

Sure. MIME::Types lets you specify custom types.

Stick this into an initializer

# Not quite sure what the appropriate MIMEtype for PSDs are,
# but this is the gist of it.
# .PSB is a larger version of .PSD supporting up to 300000x300000 px
psd_mime_type = MIME::Type.new('image/x-photoshop') do |t|
    t.extensions  = %w(psd psb)
    t.encoding    = '8bit'
end

MIME::Types.add psd_mime_type

Now MIME::Types.type_for "test.psd" should give you "image/x-photoshop".

Upvotes: 3

Related Questions