2010-11-17 95 views
3

我在Rails應用程序中使用Paperclip 2.3.5將PDF文檔存儲在Amazon S3上。對於每個PDF,ImageMagick都會生成一個JPG縮略圖。林」在模型中使用此配置:回形針:PDF縮略圖在S3上有錯誤的content_type

has_attached_file :file, 
        :styles => { :thumb => { :geometry => "200x200>", 
              :format => :jpg 
             } }, 
        :whiny => false, 
        :storage => :s3, 
        :s3_credentials => "#{RAILS_ROOT}/config/s3.yml", 
        :s3_permissions => 'authenticated-read', 
        :s3_headers => { 'Expires' => 1.year.from_now.httpdate }, 
        :url => "s3.amazonaws.com", 
        :path => "documents/:id/:style/:basename.:extension", 
        :bucket => 'mybucket' 

但是有問題:所生成的縮略圖上傳到S3與CONTENT_TYPE‘應用程序/ PDF格式’,這是錯誤的,因爲它是一個JPG(你可以看到S3上的文件的內容類型和Cyber​​duck等S3探索工具)。對於原始PDF文件,此content_type正確,但不適用於縮略圖。這會在一些瀏覽器(例如Chrome或Safari)中造成麻煩,而這些瀏覽器不會顯示內聯縮略圖。

請注意:存儲在我的數據庫(字段「file_content_type」)中的content_type是「application/pdf」,它仍然是正確的,因爲它是原始文件的content_type。

我如何可以覆蓋一個縮略圖CONTENT_TYPE如果它應該是從原始文件有什麼不同?

+0

這聽起來像一個回形針中的錯誤,爲什麼不提交關於這個問題的錯誤報告? – Ariejan 2010-11-18 10:03:43

+0

是一個回形針錯誤,拉請求被關閉:https://github.com/thoughtbot/paperclip/pull/414 – MoMolog 2013-01-21 13:07:50

回答

0

我必須克服這一點,不是最完美的解決方案,但我分叉回形針按住補丁在我自己的混帳回購協議 - https://github.com/svetzal/paperclip

這是回形針的直接替代品,只是把你的environment.rb

寶石 'twm_paperclip':LIB => '回形針'

3

這是我們如何固定它brighterplanet.com/research,其中有PDF文檔和PNG預覽:

has_attached :pdf_document, 
    :storage => :s3, 
    # [... other settings ...] 
    # PDFs work better in Windows 7/IE if you give them content-type: attachment 
    :s3_headers => { 'Content-Disposition' => 'attachment' }, 
    :styles => { :preview => { :geometry => '135', :format => :png } } 

after_save :fix_thumbnail 
def fix_thumbnail(force = false) 
    # application/pdf and application/x-pdf have both been seen... 
    return unless force or pdf_document_content_type.include?('pdf') 

    # set content type and disposition 
    s3 = AWS::S3.new(YAML.load(File.read("#{RAILS_ROOT}/config/aws_s3.yml"))) 
    t = s3.buckets[PAPERCLIP_BUCKET].objects[pdf_document.path(:thumbnail)] 
    content = t.read 
    t.write(:data => content, :content_type => 'image/png', :content_disposition => 'inline', :acl => :public_read) 

    nil 
end 
+0

有一個類似的問題,這對我工作。唯一的區別是我必須使用'before _#{attachment} _post_process'而不是'after_save'。 – Zubin 2012-01-30 05:13:12