मैंने पेपरक्लिप को .docx
फ़ाइल की सामग्री_टाइप application/vnd.openxmlformats-officedocument.wordprocessingml.document
के रूप में पढ़ने की अनुमति देने के लिए एक एमआईएमई प्रकार पंजीकृत किया है।रेल 4.2, पेपरक्लिप मणि। एमओएमई प्रकार पंजीकृत होने के बावजूद .docx प्रकार को संलग्न करने में विफल होने के कारण सामग्री/टाइप 'सामग्री/ज़िप' के रूप में पढ़ता है
हालांकि परीक्षणों में सामग्री_ प्रकार अभी भी application/zip
के रूप में पढ़ा जा रहा है।
कोई विचार क्यों? चीजों को और अधिक निराशाजनक बनाने के लिए, .pptx
और .xlsx
माइम प्रकार पंजीकृत हैं, वे परीक्षण पास (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
एप्लिकेशन/मॉडल/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
मदद के लिए धन्यवाद।
संपादित करें:
bkunzi01 द्वारा प्रदान की लिंक के अनुसार, मैं निम्नलिखित कोड की कोशिश की। इसका कोई प्रभाव नहीं पड़ा, .docx
फ़ाइलें अभी भी .pptx
और .xlsx
पास होने पर सत्यापन विफल हो गईं।
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'
}
पेपरक्लिप में फ़ाइलटाइप स्पूफ़िंग नामक एक सुविधा है जिसे कुछ ब्राउज़र ब्रेक करते हैं (जैसे पीडीएफ ओक्टेट-स्ट्रीम आदि के रूप में पढ़ा जा रहा है) जो एक भूमिका निभा सकता है। यहां अच्छा लेखन: https://robots.thoughtbot.com/prevent-spoofing-with-paperclip – bkunzi01
सुझाव के लिए धन्यवाद, दुर्भाग्य से यह काम नहीं किया है। मैंने इस पर ध्यान दिए बिना लेख को पढ़ा और यह मेरी समझ में वृद्धि हुई। –
यह बिल्कुल प्रासंगिक नहीं हो सकता है - लेकिन पिछले हफ्ते मैंने नवीनतम पेपरक्लिप मणि के साथ एक समान समस्या में भाग लिया जहां यह फ़ाइल को 'सीएसवी' के रूप में नहीं पढ़ेगा, और घंटों के बाद यह समस्या '.csv' के बिना फाइलों के साथ थी। अंत में फ़ाइल नाम में, एक csv के रूप में नहीं पढ़ा जा रहा था, भले ही content_type को 'text/csv' के रूप में सेट किया गया हो। मुझे यह सुनिश्चित करना था कि फाइल '.csv' के साथ समाप्त हो। – gwalshington