Method: Deliver::AppScreenshotValidator.validate_file_extension_and_format

Defined in:
deliver/lib/deliver/app_screenshot_validator.rb

.validate_file_extension_and_format(screenshot, errors_found) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'deliver/lib/deliver/app_screenshot_validator.rb', line 77

def self.validate_file_extension_and_format(screenshot, errors_found)
  extension = File.extname(screenshot.path).delete('.')
  valid_file_extensions = ALLOWED_SCREENSHOT_FILE_EXTENSION.values.flatten
  is_valid_extension = valid_file_extensions.include?(extension)

  unless is_valid_extension
    errors_found << ValidationError.new(type: ValidationError::INVALID_FILE_EXTENSION,
                                        path: screenshot.path,
                                        debug_info: "Only #{valid_file_extensions.join(', ')} are allowed")
  end

  format = FastImage.type(screenshot.path)
  is_extension_matched = ALLOWED_SCREENSHOT_FILE_EXTENSION[format] &&
                         ALLOWED_SCREENSHOT_FILE_EXTENSION[format].include?(extension)

  # This error only appears when file extension is valid
  if is_valid_extension && !is_extension_matched
    expected_extension = ALLOWED_SCREENSHOT_FILE_EXTENSION[format].first
    expected_filename = File.basename(screenshot.path, File.extname(screenshot.path)) + ".#{expected_extension}"
    errors_found << ValidationError.new(type: ValidationError::FILE_EXTENSION_MISMATCH,
                                        path: screenshot.path,
                                        debug_info: %(Actual format is "#{format}". Rename the filename to "#{expected_filename}".))
  end
end