From e4d745318031d25e8d2004cc0b77f6cf233778d6 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 15 Feb 2022 18:04:53 -0600 Subject: [PATCH] uploads: improve error messages. Improve upload error messages when downloading an URL fails, or it isn't an image or video file. --- app/logical/danbooru/http.rb | 2 +- app/logical/sources/strategies/base.rb | 2 -- app/models/media_asset.rb | 2 +- test/functional/uploads_controller_test.rb | 2 +- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/app/logical/danbooru/http.rb b/app/logical/danbooru/http.rb index a0b8944e2..c327c9c74 100644 --- a/app/logical/danbooru/http.rb +++ b/app/logical/danbooru/http.rb @@ -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 diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index 84a4c628b..52b8630f5 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -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 diff --git a/app/models/media_asset.rb b/app/models/media_asset.rb index 79af6f49d..07ab58878 100644 --- a/app/models/media_asset.rb +++ b/app/models/media_asset.rb @@ -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 diff --git a/test/functional/uploads_controller_test.rb b/test/functional/uploads_controller_test.rb index ea0ece1e9..f06b3d90f 100644 --- a/test/functional/uploads_controller_test.rb +++ b/test/functional/uploads_controller_test.rb @@ -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