2017-07-27 2 views
9

Ich habe einen MIME-Typ registriert, damit Paperclip den Inhaltstyp .docx der Datei application/vnd.openxmlformats-officedocument.wordprocessingml.document lesen kann.Schienen 4.2, Büroklammer gem. Fehler beim Anhängen des DOCX-Typs, liest content_type als 'application/zip', obwohl der MIME-Typ registriert ist

In Tests wird der content_type jedoch immer noch als application/zip gelesen.

Irgendeine Idee warum? Um die Dinge frustrierender zu machen, wurden die .pptx und .xlsx MIME-Typen registriert, diese Tests bestanden (urgh).

config/initializers/mime_types.rb

Mime::Type.register 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', :docx 
Mime::Type.register 'application/vnd.openxmlformats-officedocument.presentationml.presentation', :pptx 
Mime::Type.register 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :xlsx 

app/models/attachment.rb

class Attachment < ActiveRecord::Base 
    include FormatFile 

    # Constants: 
    VALID_FILE_NAMES = [/[^a-z0-9\-]+/i].freeze 
    VALID_IMAGE_TYPES = %w[image/jpeg image/jpg image/png image/x-icon image/bnp].freeze 
    VALID_CONTENT_TYPES = %w[ 
    image/jpeg 
    image/jpg 
    image/gif 
    image/png 
    image/bmp 
    image/x-icon 
    text/plain 
    text/csv 
    application/xml 
    application/pdf 
    application/msword 
    application/vnd.openxmlformats-officedocument.wordprocessingml.document 
    application/vnd.openxmlformats-officedocument.wordprocessingml.template 
    application/vnd.ms-word.document.macroEnabled.12 
    application/vnd.ms-word.template.macroEnabled.12 
    application/vnd.ms-excel 
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
    application/vnd.openxmlformats-officedocument.spreadsheetml.template 
    application/vnd.ms-excel.sheet.macroEnabled.12 
    application/vnd.ms-excel.template.macroEnabled.12 
    application/vnd.ms-excel.addin.macroEnabled.12 
    application/vnd.ms-excel.sheet.binary.macroEnabled.12 
    application/vnd.ms-powerpoint 
    application/vnd.openxmlformats-officedocument.presentationml.presentation 
    application/vnd.openxmlformats-officedocument.presentationml.template 
    application/vnd.openxmlformats-officedocument.presentationml.slideshow 
    application/vnd.ms-powerpoint.addin.macroEnabled.12 
    application/vnd.ms-powerpoint.presentation.macroEnabled.12 
    application/vnd.ms-powerpoint.template.macroEnabled.12 
    application/vnd.ms-powerpoint.slideshow.macroEnabled.12 
    ].freeze 

    # Associations: 
    belongs_to :attachable, polymorphic: true 

    # Paperclip attachments 
    has_attached_file :attachment, 
        styles:  { 
         medium: ['300x300#', :png], 
         thumb: ['100x100#', :png], 
         original: ['500x500>', :png] 
        }, 
        default_url: '/images/:style/missing.png', 
        url:   '/system/:class/:attachment/:id_partition/:style/:hash.:extension', 
        path:  ':rails_root/public:url', 
        hash_secret: '623629947a471569fe9808ab386f6e866abde5f582485beaa24fa12032b28a21b6ee94c018fe531484bb438a7376d4a00b4bc35598de34c01f0e40b1dbb37df5' 

    # Validations: 
    validates_attachment :attachment, 
         content_type: {content_type: VALID_CONTENT_TYPES}, 
         file_name: {matches: VALID_FILE_NAMES}, 
         size:   {in: 0..5.megabytes} 
    validates_with AttachmentPresenceValidator, attributes: :attachment 

    # Callbacks: 
    before_post_process :skip_all_non_images 
    before_validation do 
    sanitize_filename attachment_file_name 
    end 

    # Don't shrink and create different styles for anything which isn't an image 
    def skip_all_non_images 
    VALID_IMAGE_TYPES.include?(attachment_content_type) 
    end 
end 

Danke für die Hilfe.

Edit:

Gemäß dem von bkunzi01 angegebenen Link habe ich versucht, den folgenden Code ein. Es hat keine Wirkung, .docx Dateien noch Validierung fehlgeschlagen, während .pptx und .xlsx übergeben.

config/application.rb

Paperclip.options[:content_type_mappings] = { 
     docx: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 
     pptx: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 
     xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
    } 
+0

Paperclip hat eine Funktion namens Dateityp Spoofing, dass einige Browser brechen (wie pdfs als octet-stream etc. gelesen werden), die eine Rolle spielen können. Gute Beschreibung hier: https://robots.thoughtbot.com/prevent-spoofing-with-paperclip – bkunzi01

+0

Danke für den Vorschlag, leider hat es nicht funktioniert. Ich habe den Artikel trotzdem durchgelesen und es hat mein Verständnis erhöht. –

+0

Dies ist vielleicht überhaupt nicht relevant - aber letzte Woche stieß ich auf ein ähnliches Problem mit dem neuesten PaperClip-Juwel, wo es die Datei nicht als 'csv' lesen würde, und außerhalb der Stunden war das Problem mit Dateien ohne' .csv' im Dateinamen am Ende wurden, nicht als csv gelesen, auch wenn der content_type als 'text/csv' eingestellt wurde. Ich musste sicherstellen, dass die Datei mit '.csv 'endete. – gwalshington

Antwort

2

Es scheint, dass die neue Version von Büroklammer dieses Problem verursacht wurde. Downgrade auf Version 4.3.1 sollte Ihr Problem lösen

+0

Wenn Patrick deine Antwort akzeptiert, werde ich dir das Kopfgeld geben. Nebenbei: Haben Sie dieses Problem den Entwicklern gemeldet? –

+0

Agi - Nein, habe ich noch nicht. Ich habe das heute herausgefunden und werde heute ein Problem einreichen. Vielen Dank! – richi

+1

Vielleicht posten Sie hier den Link zum Fehlerbericht – kvantour

Verwandte Themen