Convert MOV and WEBM to MP4, raise maximum limit to 40MB (#8101)
Separate size limits for images and video. Images remain at 8MB, while videos can be up to 40MB.
This commit is contained in:
		
							parent
							
								
									18831acc10
								
							
						
					
					
						commit
						1656663598
					
				@ -25,10 +25,11 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
  enum type: [:image, :gifv, :video, :unknown]
 | 
			
		||||
 | 
			
		||||
  IMAGE_FILE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif'].freeze
 | 
			
		||||
  VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v'].freeze
 | 
			
		||||
  VIDEO_FILE_EXTENSIONS = ['.webm', '.mp4', '.m4v', '.mov'].freeze
 | 
			
		||||
 | 
			
		||||
  IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif'].freeze
 | 
			
		||||
  VIDEO_MIME_TYPES = ['video/webm', 'video/mp4'].freeze
 | 
			
		||||
  IMAGE_MIME_TYPES             = ['image/jpeg', 'image/png', 'image/gif'].freeze
 | 
			
		||||
  VIDEO_MIME_TYPES             = ['video/webm', 'video/mp4', 'video/quicktime'].freeze
 | 
			
		||||
  VIDEO_CONVERTIBLE_MIME_TYPES = ['video/webm', 'video/quicktime'].freeze
 | 
			
		||||
 | 
			
		||||
  IMAGE_STYLES = {
 | 
			
		||||
    original: {
 | 
			
		||||
@ -54,7 +55,25 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
    },
 | 
			
		||||
  }.freeze
 | 
			
		||||
 | 
			
		||||
  LIMIT = 8.megabytes
 | 
			
		||||
  VIDEO_FORMAT = {
 | 
			
		||||
    format: 'mp4',
 | 
			
		||||
    convert_options: {
 | 
			
		||||
      output: {
 | 
			
		||||
        'movflags' => 'faststart',
 | 
			
		||||
        'pix_fmt'  => 'yuv420p',
 | 
			
		||||
        'vf'       => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
 | 
			
		||||
        'vsync'    => 'cfr',
 | 
			
		||||
        'c:v'      => 'h264',
 | 
			
		||||
        'b:v'      => '500K',
 | 
			
		||||
        'maxrate'  => '1300K',
 | 
			
		||||
        'bufsize'  => '1300K',
 | 
			
		||||
        'crf'      => 18,
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
  }.freeze
 | 
			
		||||
 | 
			
		||||
  IMAGE_LIMIT = 8.megabytes
 | 
			
		||||
  VIDEO_LIMIT = 40.megabytes
 | 
			
		||||
 | 
			
		||||
  belongs_to :account, inverse_of: :media_attachments, optional: true
 | 
			
		||||
  belongs_to :status,  inverse_of: :media_attachments, optional: true
 | 
			
		||||
@ -65,8 +84,9 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
                    convert_options: { all: '-quality 90 -strip' }
 | 
			
		||||
 | 
			
		||||
  validates_attachment_content_type :file, content_type: IMAGE_MIME_TYPES + VIDEO_MIME_TYPES
 | 
			
		||||
  validates_attachment_size :file, less_than: LIMIT
 | 
			
		||||
  remotable_attachment :file, LIMIT
 | 
			
		||||
  validates_attachment_size :file, less_than: IMAGE_LIMIT, unless: :video?
 | 
			
		||||
  validates_attachment_size :file, less_than: VIDEO_LIMIT, if: :video?
 | 
			
		||||
  remotable_attachment :file, VIDEO_LIMIT
 | 
			
		||||
 | 
			
		||||
  include Attachmentable
 | 
			
		||||
 | 
			
		||||
@ -122,25 +142,15 @@ class MediaAttachment < ApplicationRecord
 | 
			
		||||
      if f.instance.file_content_type == 'image/gif'
 | 
			
		||||
        {
 | 
			
		||||
          small: IMAGE_STYLES[:small],
 | 
			
		||||
          original: {
 | 
			
		||||
            format: 'mp4',
 | 
			
		||||
            convert_options: {
 | 
			
		||||
              output: {
 | 
			
		||||
                'movflags' => 'faststart',
 | 
			
		||||
                'pix_fmt'  => 'yuv420p',
 | 
			
		||||
                'vf'       => 'scale=\'trunc(iw/2)*2:trunc(ih/2)*2\'',
 | 
			
		||||
                'vsync'    => 'cfr',
 | 
			
		||||
                'c:v'      => 'h264',
 | 
			
		||||
                'b:v'      => '500K',
 | 
			
		||||
                'maxrate'  => '1300K',
 | 
			
		||||
                'bufsize'  => '1300K',
 | 
			
		||||
                'crf'      => 18,
 | 
			
		||||
              },
 | 
			
		||||
            },
 | 
			
		||||
          },
 | 
			
		||||
          original: VIDEO_FORMAT,
 | 
			
		||||
        }
 | 
			
		||||
      elsif IMAGE_MIME_TYPES.include? f.instance.file_content_type
 | 
			
		||||
        IMAGE_STYLES
 | 
			
		||||
      elsif VIDEO_CONVERTIBLE_MIME_TYPES.include?(f.instance.file_content_type)
 | 
			
		||||
        {
 | 
			
		||||
          small: VIDEO_STYLES[:small],
 | 
			
		||||
          original: VIDEO_FORMAT,
 | 
			
		||||
        }
 | 
			
		||||
      else
 | 
			
		||||
        VIDEO_STYLES
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user