Sha256: f3ba0002626d0b5b36c8bf53ec53c39a0e5849af9e66a40fb2ebbc39b50847f3

Contents?: true

Size: 1.67 KB

Versions: 8

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

# This validator ensures the files to be uploaded match the attached uploader's
# content types. This prevents CarrierWave from uploading the records before
# they pass the content type validations.
class UploaderContentTypeValidator < ActiveModel::Validations::FileContentTypeValidator
  def validate_each(record, attribute, value)
    begin
      values = parse_values(value)
    rescue JSON::ParserError
      record.errors.add attribute, :invalid
      return
    end

    return if values.empty?

    uploader = record.send(attribute)
    return unless uploader
    return unless uploader.is_a?(Decidim::ApplicationUploader)

    mode = option_value(record, :mode)
    allowed_types = uploader.content_type_whitelist || []
    forbidden_types = uploader.content_type_blacklist || []

    values.each do |val|
      val_mode = mode

      # The :strict mode would be more robust for the content type detection if
      # the value does not know its own content type. However, this would
      # require the command line utility named `file` which is only available in
      # *nix. This would also require adding a new gem dependency for running
      # the CLI utility, Terrapin or Cocaine in older versions of the
      # file_validators gem. The :relaxed mode detects the content type based on
      # the file extension through the mime-types gem.
      val_mode = :relaxed if val_mode.blank? && !val.respond_to?(:content_type)

      content_type = get_content_type(val, val_mode)
      validate_whitelist(record, attribute, content_type, allowed_types)
      validate_blacklist(record, attribute, content_type, forbidden_types)
    end
  end

  def check_validity!; end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.5 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.4 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.3 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.2 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.1 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.1.rc1 app/validators/uploader_content_type_validator.rb
decidim-core-0.23.0 app/validators/uploader_content_type_validator.rb