是否可以使用Active Storage添加验证以仅接受.pdf和.doc文件?
答案 0 :(得分:12)
目前,您必须编写自己的验证程序,查看附件的MIME类型:
class Item
has_one_attached :document
validate :correct_document_mime_type
private
def correct_document_mime_type
if document.attached? && !document.content_type.in?(%w(application/msword application/pdf))
document.purge # delete the uploaded file
errors.add(:document, 'Must be a PDF or a DOC file')
end
end
end
此外,还有一些有用的快捷方法image?
,audio?
,video?
和text?
可以检查多种mime类型。
答案 1 :(得分:6)
有一个宝石可以为活动存储提供验证
$SP().list("My List").get({
fields:"Title",//whatever fields you want
where:"Author = '[Me]'"//[Me] is the current user that is logged in
}).then(function(row) {
console.log(row[0].getAttribute("Title"));
});
https://github.com/aki77/activestorage-validator
gem 'activestorage-validator'
答案 2 :(得分:2)
由于ActiveStorage还没有验证,我在表单中发现了以下帮助。
<div class="field">
<%= f.label :deliverable %>
<%= f.file_field :deliverable, direct_upload: true,
accept: 'application/pdf,
application/zip,application/vnd.openxmlformats-officedocument.wordprocessingml.document' %>
</div>
答案 3 :(得分:1)
class Book < ApplicationRecord
has_one_attached :image
has_many_attached :documents
validate :image_validation
validate :documents_validation
def documents_validation
error_message = ''
documents_valid = true
if documents.attached?
documents.each do |document|
if !document.blob.content_type.in?(%w(application/xls application/odt application/ods pdf application/tar application/tar.gz application/docx application/doc application/rtf application/txt application/rar application/zip application/pdf image/jpeg image/jpg image/png))
documents_valid = false
error_message = 'The document wrong format'
elsif document.blob.byte_size > (100 * 1024 * 1024) && document.blob.content_type.in?(%w(application/xls application/odt application/ods pdf application/tar application/tar.gz application/docx application/doc application/rtf application/txt application/rar application/zip application/pdf image/jpeg image/jpg image/png))
documents_valid = false
error_message = 'The document oversize limited (100MB)'
end
end
end
unless documents_valid
errors.add(:documents, error_message)
self.documents.purge
DestroyInvalidationRecordsJob.perform_later('documents', 'Book', self.id)
end
end
def image_validation
if image.attached?
if !image.blob.content_type.in?(%w(image/jpeg image/jpg image/png))
image.purge_later
errors.add(:image, 'The image wrong format')
elsif image.blob.content_type.in?(%w(image/jpeg image/jpg image/png)) && image.blob.byte_size > (5 * 1024 * 1024) # Limit size 5MB
image.purge_later
errors.add(:image, 'The image oversize limited (5MB)')
end
elsif image.attached? == false
image.purge_later
errors.add(:image, 'The image required.')
end
end
end
在工作中摧毁
class DestroyInvalidationRecordsJob < ApplicationJob
queue_as :default
def perform(record_name, record_type, record_id)
attachments = ActiveStorage::Attachment.where(name: record_name, record_type: record_type, record_id: record_id)
attachments.each do |attachment|
blob = ActiveStorage::Blob.find(attachment.blob_id)
attachment.destroy
blob.destroy if blob.present?
end
end
end
答案 4 :(得分:0)
我正在使用ActiveStorage直接上传。由于验证器尚不存在,所以我只是重写了Local
方法:
DirectUploadsController Create
还需要覆盖路由:
# This is a kind of monkey patch which overrides the default create so I can do some validation.
# Active Storage validation wont be released until Rails 6.
class DirectUploadsController < ActiveStorage::DirectUploadsController
def create
puts "Do validation here"
super
end
end
答案 5 :(得分:0)
+1,用于将doc
与ActiveStorage一起使用
在模型中,您可以通过以下方式验证docx
,pdf
和has_one_attached :cv
validates :cv, blob: { content_type: ['application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/pdf'], size_range: 0..5.megabytes }
格式:
{{1}}
答案 6 :(得分:0)
我正在看https://gist.github.com/lorenadl/a1eb26efdf545b4b2b9448086de3961d
在您看来,您必须执行以下操作
<div class="field">
<%= f.label :deliverable %>
<%= f.file_field :deliverable, direct_upload: true,
accept: 'application/pdf,
application/zip,application/vnd.openxmlformats-officedocument.wordprocessingml.document' %>
</div>
现在在您的模型上,您可以执行以下操作
class User < ApplicationRecord
has_one_attached :document
validate :check_file_type
private
def check_file_type
if document.attached? && !document.content_type.in?(%w(application/msword application/pdf))
document.purge # delete the uploaded file
errors.add(:document, 'Must be a PDF or a DOC file')
end
end
end
我希望这对您有帮助
答案 7 :(得分:0)
并不总是需要为几行代码添加一个 gem。这是一个基于对上述示例的小改动的工作示例。
validate :logo_content_type, if: -> { logo.attached? }
def logo_content_type
allowed = %w[image/jpeg image/jpeg image/webp image/png]
if allowed.exclude?(logo.content_type)
errors.add(:logo, message: 'Logo must be a JPG, JPEG, WEBP or PNG')
end
end