闭社主体 forked from https://github.com/tootsuite/mastodon
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.

65 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: '-quality 90 -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. default_scope { order('id asc') }
  15. def local?
  16. remote_url.blank?
  17. end
  18. def file_remote_url=(url)
  19. self.file = URI.parse(url)
  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: '1280x1280>',
  36. small: '400x400>',
  37. }
  38. else
  39. {
  40. small: {
  41. convert_options: {
  42. output: {
  43. vf: 'scale=\'min(400\, iw):min(400\, ih)\':force_original_aspect_ratio=decrease',
  44. },
  45. },
  46. format: 'png',
  47. time: 1,
  48. },
  49. }
  50. end
  51. end
  52. end
  53. end