You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
884 B

  1. class MediaAttachment < ApplicationRecord
  2. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif']
  3. VIDEO_MIME_TYPES = ['video/webm']
  4. belongs_to :account, inverse_of: :media_attachments
  5. belongs_to :status, inverse_of: :media_attachments
  6. has_attached_file :file, styles: lambda { |f| f.instance.image? ? { small: '510x680>' } : { small: { format: 'webm' } } }, processors: lambda { |f| f.video? ? [:transcoder] : [:thumbnail] }
  7. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  8. validates_attachment_size :file, less_than: 4.megabytes
  9. validates :account, presence: true
  10. def local?
  11. self.remote_url.blank?
  12. end
  13. def file_remote_url=(url)
  14. self.file = URI.parse(url)
  15. end
  16. def image?
  17. IMAGE_MIME_TYPES.include? file_content_type
  18. end
  19. def video?
  20. VIDEO_MIME_TYPES.include? file_content_type
  21. end
  22. end