From 7540e235a2b387ca08cf57f8942d1b190d242808 Mon Sep 17 00:00:00 2001
From: Takeshi Umeda <noel.yoshiba@gmail.com>
Date: Mon, 20 Jul 2020 05:28:27 +0900
Subject: [PATCH] Fix movie width and frame_rate returning nil (#14357)

* Fix movie width and frame_rate returning nil

* Add StreamValidationError and raise

* Fix code style
---
 app/lib/exceptions.rb                  | 1 +
 app/models/concerns/remotable.rb       | 2 +-
 app/models/media_attachment.rb         | 1 +
 app/services/update_account_service.rb | 2 +-
 4 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/app/lib/exceptions.rb b/app/lib/exceptions.rb
index 3362576b0b..7c8e778713 100644
--- a/app/lib/exceptions.rb
+++ b/app/lib/exceptions.rb
@@ -7,6 +7,7 @@ module Mastodon
   class HostValidationError < ValidationError; end
   class LengthValidationError < ValidationError; end
   class DimensionsValidationError < ValidationError; end
+  class StreamValidationError < ValidationError; end
   class RaceConditionError < Error; end
   class RateLimitExceededError < Error; end
 
diff --git a/app/models/concerns/remotable.rb b/app/models/concerns/remotable.rb
index c6d0c7f1f4..56b9c01642 100644
--- a/app/models/concerns/remotable.rb
+++ b/app/models/concerns/remotable.rb
@@ -29,7 +29,7 @@ module Remotable
         rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError => e
           Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
           raise e unless suppress_errors
-        rescue Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError, Paperclip::Error, Mastodon::DimensionsValidationError => e
+        rescue Paperclip::Errors::NotIdentifiedByImageMagickError, Addressable::URI::InvalidURIError, Mastodon::HostValidationError, Mastodon::LengthValidationError, Paperclip::Error, Mastodon::DimensionsValidationError, Mastodon::StreamValidationError => e
           Rails.logger.debug "Error fetching remote #{attachment_name}: #{e}"
         end
 
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index 3f2e0ceb1b..3d93ec75ba 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -336,6 +336,7 @@ class MediaAttachment < ApplicationRecord
 
     return unless movie.valid?
 
+    raise Mastodon::StreamValidationError, 'Video has no video stream' if movie.width.nil? || movie.frame_rate.nil?
     raise Mastodon::DimensionsValidationError, "#{movie.width}x#{movie.height} videos are not supported" if movie.width * movie.height > MAX_VIDEO_MATRIX_LIMIT
     raise Mastodon::DimensionsValidationError, "#{movie.frame_rate.to_i}fps videos are not supported" if movie.frame_rate > MAX_VIDEO_FRAME_RATE
   end
diff --git a/app/services/update_account_service.rb b/app/services/update_account_service.rb
index 4172d57740..666db5c711 100644
--- a/app/services/update_account_service.rb
+++ b/app/services/update_account_service.rb
@@ -12,7 +12,7 @@ class UpdateAccountService < BaseService
       check_links(account)
       process_hashtags(account)
     end
-  rescue Mastodon::DimensionsValidationError => de
+  rescue Mastodon::DimensionsValidationError, Mastodon::StreamValidationError => de
     account.errors.add(:avatar, de.message)
     false
   end
-- 
GitLab