diff --git a/app/logical/upload_service.rb b/app/logical/upload_service.rb index 1db0597db..897fd8d27 100644 --- a/app/logical/upload_service.rb +++ b/app/logical/upload_service.rb @@ -213,6 +213,39 @@ class UploadService tags << "animated_png" if is_animated_png?(upload, file) tags.join(" ") end + + def self.download_from_source(source, referer_url: nil) + download = Downloads::File.new(source, referer_url: referer_url) + file = download.download! + context = { + downloaded_source: download.downloaded_source, + source: download.source + } + + if download.data[:is_ugoira] + context[:ugoira] = { + frame_data: download.data[:ugoira_frame_data], + content_type: download.data[:ugoira_content_type] + } + end + + yield(context) + + return file + end + + def self.download_for_upload(source, upload) + file = download_from_source(source, referer_url: upload.referer_url) do |context| + upload.downloaded_source = context[:downloaded_source] + upload.source = context[:source] + + if context[:ugoira] + upload.context = { ugoira: context[:ugoira] } + end + end + + return file + end end class Preprocessor @@ -281,14 +314,7 @@ class UploadService begin if source.present? - file = download_from_source(source, referer_url: upload.referer_url) do |context| - upload.downloaded_source = context[:downloaded_source] - upload.source = context[:source] - - if context[:ugoira] - upload.context = { ugoira: context[:ugoira] } - end - end + file = Utils.download_for_upload(source, upload) elsif params[:file].present? file = params[:file] end @@ -314,26 +340,6 @@ class UploadService pred.save return pred end - - def download_from_source(source, referer_url: nil) - download = Downloads::File.new(source, referer_url: referer_url) - file = download.download! - context = { - downloaded_source: download.downloaded_source, - source: download.source - } - - if download.data[:is_ugoira] - context[:ugoira] = { - frame_data: download.data[:ugoira_frame_data], - content_type: download.data[:ugoira_content_type] - } - end - - yield(context) - - return file - end end class Replacer @@ -506,10 +512,12 @@ class UploadService @upload.update(status: "processing") + if @upload.file.nil? && source.present? + @upload.file = Utils.download_for_upload(source, @upload) + end + if @upload.file.present? Utils.process_file(upload, @upload.file) - else - # sources will be handled in preprocessing now end @upload.save! diff --git a/app/models/upload.rb b/app/models/upload.rb index ee6a1a3d6..f4f917470 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -59,7 +59,7 @@ class Upload < ApplicationRecord validates :image_height, numericality: { less_than_or_equal_to: Danbooru.config.max_image_height }, allow_nil: true validates :image_width, numericality: { less_than_or_equal_to: Danbooru.config.max_image_width }, allow_nil: true validates :rating, inclusion: { in: %w(q e s) }, allow_nil: true - validates :md5, confirmation: true + validates :md5, confirmation: true, if: -> (rec) { rec.md5_confirmation.present? } validates :file_ext, format: { with: /jpg|gif|png|swf|webm|mp4|zip/ }, allow_nil: true validates_with Validator serialize :context, JSON diff --git a/test/models/upload_service_test.rb b/test/models/upload_service_test.rb index 12fd458dc..f16986631 100644 --- a/test/models/upload_service_test.rb +++ b/test/models/upload_service_test.rb @@ -275,19 +275,16 @@ class UploadServiceTest < ActiveSupport::TestCase end end end - end - context "::Preprocessor" do - subject { UploadService::Preprocessor } - - context "#download_from_source" do + context ".download_from_source" do setup do - @jpeg = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg" - @ugoira = "https://i.pximg.net/img-zip-ugoira/img/2017/04/04/08/57/38/62247364_ugoira1920x1080.zip" + @ugoira_source = "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=62247364" + @jpeg_source = "https://upload.wikimedia.org/wikipedia/commons/c/c5/Moraine_Lake_17092005.jpg" + @upload = Upload.new end should "work on a jpeg" do - file = subject.new({}).download_from_source(@jpeg) do |context| + file = subject.download_from_source(@jpeg_source) do |context| assert_not_nil(context[:downloaded_source]) assert_not_nil(context[:source]) end @@ -297,7 +294,7 @@ class UploadServiceTest < ActiveSupport::TestCase end should "work on an ugoira url" do - file = subject.new({}).download_from_source(@ugoira, referer_url: "https://www.pixiv.net") do |context| + file = subject.download_from_source(@ugoira_source, referer_url: "https://www.pixiv.net") do |context| assert_not_nil(context[:downloaded_source]) assert_not_nil(context[:source]) assert_not_nil(context[:ugoira]) @@ -306,7 +303,17 @@ class UploadServiceTest < ActiveSupport::TestCase assert_operator(File.size(file.path), :>, 0) file.close end + + should "initialize fields on the upload" do + subject.download_for_upload(@ugoira_source, @upload) + assert_not_nil(@upload.source) + assert_not_nil(@upload.context) + end end + end + + context "::Preprocessor" do + subject { UploadService::Preprocessor } context "#start!" do setup do