uploads: improve error messages.

Improve upload error messages when downloading an URL fails, or it isn't
an image or video file.
This commit is contained in:
evazion
2022-02-15 18:04:53 -06:00
parent 87a00a1182
commit e4d7453180
4 changed files with 3 additions and 5 deletions

View File

@@ -147,7 +147,7 @@ module Danbooru
def download_media(url, file: Tempfile.new("danbooru-download-", binmode: true))
response = get(url)
raise DownloadError, "Downloading #{response.uri} failed with code #{response.status}" if response.status != 200
raise DownloadError, "#{url} failed with code #{response.status}" if response.status != 200
raise FileTooLargeError, "File size too large (size: #{response.content_length.to_i.to_formatted_s(:human_size)}; max size: #{@max_size.to_formatted_s(:human_size)})" if @max_size && response.content_length.to_i > @max_size
size = 0

View File

@@ -219,9 +219,7 @@ module Sources
# Download the file at the given url, or at the main image url by default.
def download_file!(download_url = image_url)
raise DownloadError, "Download failed: couldn't find download url for #{url}" if download_url.blank?
response, file = http_downloader.download_media(download_url)
raise DownloadError, "Download failed: #{download_url} returned error #{response.status}" if response.status != 200
file
end

View File

@@ -38,7 +38,7 @@ class MediaAsset < ApplicationRecord
}
validates :md5, uniqueness: { conditions: -> { where(status: [:processing, :active]) } }
validates :file_ext, inclusion: { in: %w[jpg png gif mp4 webm swf zip], message: "Not an image or video" }
validates :file_ext, inclusion: { in: %w[jpg png gif mp4 webm swf zip], message: "File is not an image or video" }
validates :file_size, numericality: { less_than_or_equal_to: Danbooru.config.max_file_size, message: ->(asset, _) { "too large (size: #{asset.file_size.to_formatted_s(:human_size)}; max size: #{Danbooru.config.max_file_size.to_formatted_s(:human_size)})" } }
validates :duration, numericality: { less_than_or_equal_to: MAX_VIDEO_DURATION, message: "must be less than #{MAX_VIDEO_DURATION} seconds", allow_nil: true }, on: :create # XXX should allow admins to bypass
validate :validate_resolution, on: :create

View File

@@ -174,7 +174,7 @@ class UploadsControllerTest < ActionDispatch::IntegrationTest
post_auth uploads_path(format: :json), @user, params: { upload: { file: file }}
assert_response 201
assert_match("Not an image or video", Upload.last.error)
assert_match("File is not an image or video", Upload.last.error)
end
context "for a file larger than the file size limit" do