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.4 KiB

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