0

我正在使用 Rails 3、Paperclip(3.3.0)、aws-sdk (1.7.1)。

我的回形针附件安全地存储在 S3 上。

附件.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"

为了下载文件,我生成了一个安全 URL,如下所示:

  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

问题在于 PSD:这是返回空的:

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

在代码中它看起来像:

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

它适用于其他文件,但不适用于 PSD。知道为什么以及如何解决吗?

谢谢

4

1 回答 1

3

当然。MIME::Types 允许您指定自定义类型。

将其粘贴到初始化程序中

# 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

现在MIME::Types.type_for "test.psd"应该给你"image/x-photoshop"

于 2012-12-01T02:43:58.250 回答