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.

63 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class MediaAttachment < ApplicationRecord
  3. IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
  4. VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
  5. belongs_to :account, inverse_of: :media_attachments
  6. belongs_to :status, inverse_of: :media_attachments
  7. has_attached_file :file,
  8. styles: -> (f) { file_styles f },
  9. processors: -> (f) { f.video? ? [:transcoder] : [:thumbnail] },
  10. convert_options: { all: '-strip' }
  11. validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
  12. validates_attachment_size :file, less_than: 4.megabytes
  13. validates :account, presence: true
  14. def local?
  15. remote_url.blank?
  16. end
  17. def file_remote_url=(url)
  18. self.file = URI.parse(url)
  19. end
  20. def image?
  21. IMAGE_MIME_TYPES.include? file_content_type
  22. end
  23. def video?
  24. VIDEO_MIME_TYPES.include? file_content_type
  25. end
  26. def type
  27. image? ? 'image' : 'video'
  28. end
  29. class << self
  30. private
  31. def file_styles(f)
  32. if f.instance.image?
  33. {
  34. original: '100%',
  35. small: '510x680>',
  36. }
  37. else
  38. {
  39. small: {
  40. convert_options: {
  41. output: {
  42. vf: 'scale=\'min(510\, iw):min(680\, ih)\':force_original_aspect_ratio=decrease',
  43. },
  44. },
  45. format: 'png',
  46. time: 1,
  47. },
  48. }
  49. end
  50. end
  51. end
  52. end