logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

account_avatar.rb (1072B)


  1. # frozen_string_literal: true
  2. module AccountAvatar
  3. extend ActiveSupport::Concern
  4. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].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: '-strip' }, 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
  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