Sha256: e7011eeea05f05bd8cbcc393117e870468df2ace87040fd02aa5e44aef53b5b9

Contents?: true

Size: 1.68 KB

Versions: 22

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

# This validator ensures the files to be uploaded match the attached uploader's
# content types.
class UploaderContentTypeValidator < ActiveModel::Validations::FileContentTypeValidator
  # rubocop: disable Metrics/CyclomaticComplexity
  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.attached_uploader(attribute) || record.send(attribute)
    return unless uploader.is_a?(Decidim::ApplicationUploader)

    mode = option_value(record, :mode)
    allowed_types = uploader.content_type_allowlist || []
    forbidden_types = uploader.content_type_denylist || []

    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
  # rubocop: enable Metrics/CyclomaticComplexity

  def check_validity!; end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
decidim-core-0.26.10 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.9 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.8 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.7 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.5 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.4 app/validators/uploader_content_type_validator.rb
decidim-core-0.27.0 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.3 app/validators/uploader_content_type_validator.rb
decidim-core-0.27.0.rc2 app/validators/uploader_content_type_validator.rb
decidim-core-0.27.0.rc1 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.2 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.1 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.0 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.0.rc2 app/validators/uploader_content_type_validator.rb
decidim-core-0.26.0.rc1 app/validators/uploader_content_type_validator.rb
decidim-core-0.25.2 app/validators/uploader_content_type_validator.rb
decidim-core-0.25.1 app/validators/uploader_content_type_validator.rb
decidim-core-0.25.0 app/validators/uploader_content_type_validator.rb
decidim-core-0.25.0.rc4 app/validators/uploader_content_type_validator.rb
decidim-core-0.25.0.rc3 app/validators/uploader_content_type_validator.rb