Skip to content

Allow Excluding or Including Specific file extensions in image uploader #202

@ishields

Description

@ishields

Allow customizing supported formats for the image uploader. For example, I'd like to be able to exclude .avif.

Maglev.configure do |config|
      config.reject_image_extensions = %w[avif]
end

The reason I want to do this is I use cloudflare to resize these images downstream and avif is not a supported input.

For now I patched it like this but this seems like something that could be customized.

# Patch Maglev::Asset to reject AVIF files
# This file is loaded by config/initializers/maglev_patch.rb
class Maglev::Asset
  # Validate that content_type is not AVIF
  validate :verify_content_types, on: :create

  private

  def verify_content_types
    # Check Active Storage attachment blob first (most reliable)
    # Maglev uses Active Storage, so check if there's an attachment
    if respond_to?(:file) && file.attached?
      blob_content_type = "#{file.blob&.content_type}#{content_type}"
      if blob_content_type.present? && (blob_content_type.include?('avif'))
        errors.add(:base, 'AVIF format is not supported. Please use JPEG, PNG, GIF, or WebP.')
        return
      end
    end
  end
end

I imagine the better way is to jusdt allow customizing the front end piece

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions