diff --git a/app/models/concerns/tipline_content_multimedia.rb b/app/models/concerns/tipline_content_multimedia.rb index 6a7c9f18a6..210ef27ea3 100644 --- a/app/models/concerns/tipline_content_multimedia.rb +++ b/app/models/concerns/tipline_content_multimedia.rb @@ -3,6 +3,7 @@ # Attached file: image, audio or video module TiplineContentMultimedia + class ConvertedFileTooLarge < StandardError; end extend ActiveSupport::Concern included do @@ -50,11 +51,12 @@ def convert_header_file_audio_or_video(type) output_path = File.join(Rails.root, 'tmp', "#{content_name}-#{type}-output-#{self.id}-#{now}.mp4") video = FFMPEG::Movie.new(input.path) video.transcode(output_path, options) + raise TiplineContentMultimedia::ConvertedFileTooLarge.new('Converted file for tipline content is too large') if (File.size(output_path).to_f / 1024000.0) > self.header_file_video_max_size path = "#{content_name}/video/#{content_name}-#{type}-#{self.id}-#{now}" CheckS3.write(path, 'video/mp4', File.read(output_path)) url = CheckS3.public_url(path) rescue StandardError => e - CheckSentry.notify(e) + CheckSentry.notify(e, class: self.class, id: self.id) ensure FileUtils.rm_f input.path FileUtils.rm_f output_path diff --git a/app/models/concerns/tipline_content_video.rb b/app/models/concerns/tipline_content_video.rb index 161d1c71ab..668247151c 100644 --- a/app/models/concerns/tipline_content_video.rb +++ b/app/models/concerns/tipline_content_video.rb @@ -4,8 +4,12 @@ module TiplineContentVideo extend ActiveSupport::Concern # MP4 less than 10 MB (WhatsApp supports 16 MB, let's be safe) + def header_file_video_max_size + 10 + end + def validate_header_file_video - self.validate_header_file(10, ['mp4'], 'errors.messages.video_too_large') + self.validate_header_file(self.header_file_video_max_size, ['mp4'], 'errors.messages.video_too_large') end def should_convert_header_video? diff --git a/test/data/h265-video.mp4 b/test/data/h265-video.mp4 new file mode 100644 index 0000000000..c799f564a1 Binary files /dev/null and b/test/data/h265-video.mp4 differ diff --git a/test/models/tipline_newsletter_test.rb b/test/models/tipline_newsletter_test.rb index b857e51275..4f9dfcd9cb 100644 --- a/test/models/tipline_newsletter_test.rb +++ b/test/models/tipline_newsletter_test.rb @@ -479,4 +479,15 @@ def teardown create_tipline_newsletter send_every: nil end end + + test 'should validate video size after converting' do + # This test file is 300 KB, but 1.6 MB after converting + WebMock.stub_request(:get, /:9000/).to_return(body: File.read(File.join(Rails.root, 'test', 'data', 'h265-video.mp4'))) + TiplineNewsletter.any_instance.stubs(:new_file_uploaded?).returns(true) + TiplineNewsletter.any_instance.stubs(:header_file_video_max_size).returns(1) # Maximum 1 MB + CheckSentry.stubs(:notify).once + Sidekiq::Testing.inline! do + create_tipline_newsletter header_type: 'video', header_file: 'h265-video.mp4' + end + end end