Tôi đã đăng ký một Loại MIME để cho phép Kẹp giấy để đọc một content_type .docx
của tệp là application/vnd.openxmlformats-officedocument.wordprocessingml.document
.Rails 4.2, Paperclip gem. Không đính kèm loại .docx, hãy đọc content_type là 'application/zip' mặc dù loại MIME đang được đăng ký
Tuy nhiên trong các thử nghiệm content_type vẫn đang được đọc là application/zip
.
Bất kỳ ý tưởng nào tại sao? Để làm cho mọi thứ trở nên khó chịu hơn, các loại .pptx
và .xlsx
mime có đã được đăng ký, những thử nghiệm đó vượt qua (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
Thanks for the help.
Edit:
Theo liên kết được cung cấp bởi bkunzi01 Tôi đã thử các mã sau. Nó không có hiệu lực, .docx
tệp vẫn không xác thực trong khi .pptx
và .xlsx
vượt qua.
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'
}
Kẹp giấy có tính năng gọi là giả mạo một số trình duyệt (như pdf được đọc dưới dạng octet-stream, v.v.) có thể đóng vai trò. Viết tốt ở đây: https://robots.thoughtbot.com/prevent-spoofing-with-paperclip – bkunzi01
Cảm ơn bạn đã gợi ý, tiếc là nó đã không hoạt động. Tôi đọc qua bài báo bất kể và nó làm tăng sự hiểu biết của tôi. –
Điều này có thể không liên quan chút nào - nhưng tuần trước tôi gặp phải vấn đề tương tự với viên đá PaperClip mới nhất, nơi nó sẽ không đọc tập tin dưới dạng 'csv', và sau giờ phát hành tập tin không có' .csv' trong tên tệp ở cuối, không được đọc dưới dạng csv, ngay cả khi content_type được đặt là 'text/csv'. Tôi phải đảm bảo tệp đã kết thúc bằng '.csv'. – gwalshington