avatar.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # frozen_string_literal: true
  2. module Account::Avatar
  3. extend ActiveSupport::Concern
  4. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
  5. LIMIT = 2.megabytes
  6. class_methods do
  7. def avatar_styles(file)
  8. styles = { original: { geometry: '400x400#', file_geometry_parser: FastGeometryParser } }
  9. styles[:static] = { geometry: '400x400#', format: 'png', convert_options: '-coalesce', file_geometry_parser: FastGeometryParser } if file.content_type == 'image/gif'
  10. styles
  11. end
  12. private :avatar_styles
  13. end
  14. included do
  15. # Avatar upload
  16. has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '+profile "!icc,*" +set date:modify +set date:create +set date:timestamp' }, processors: [:lazy_thumbnail]
  17. validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
  18. validates_attachment_size :avatar, less_than: LIMIT
  19. remotable_attachment :avatar, LIMIT, suppress_errors: false
  20. end
  21. def avatar_original_url
  22. avatar.url(:original)
  23. end
  24. def avatar_static_url
  25. avatar_content_type == 'image/gif' ? avatar.url(:static) : avatar_original_url
  26. end
  27. end