Reputation: 2319
I am working on a messaging system for our internal use and each message can have attachments. Said attachments can be pretty much any type of file but there are some types of files that I don't want to allow, like for instance EXEs.
Instead of listing a huge amount of accepted content_types in the model's validation which is far from practical and will for sure block files that should be accepted, I would rather simply specify the content types that should not be accepted.
Googling around has yielded no results, but I'm sure someone else has come across this situation before, any idea of how to best tackle this?
Upvotes: 2
Views: 617
Reputation: 2319
So this is what I ended up doing:
has_mongoid_attached_file :file
do_not_validate_attachment_file_type :file
validate :excluded_content_type
def excluded_content_type
forbidden = [
"application/x-msdownload",
"application/x-ms-installer",
"application/x-elf",
"application/x-sh",
"application/x-shellscript",
"text/x-perl",
"text/x-python",
"application/javascript"
]
errors.add(:base, 'Executable and script files are not allowed.') if forbidden.include? file_content_type
end
I don't know if this is the best way to do it but it works. If you have a better way of doing it post your answer!
Upvotes: 3
Reputation: 427
If content types that should not be accepted.
Write in model.rb
has_attached_file :record_attachment
validates_attachment_content_type :record_attachment, presence: false, :content_type => {:not => "application/zip"}
Upvotes: 2