From f6440ca70dbbacd72509ee9970428d0a72caf81c Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Mon, 23 Jul 2018 16:24:21 -0700 Subject: [PATCH] fix upload preprocessing for pixiv posts fixes #3782 --- app/logical/downloads/file.rb | 5 +++++ app/logical/storage_manager/match.rb | 4 ++-- app/logical/upload_service/preprocessor.rb | 22 ++++++++++++++-------- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/logical/downloads/file.rb b/app/logical/downloads/file.rb index 62a2d5bc8..66e2d2798 100644 --- a/app/logical/downloads/file.rb +++ b/app/logical/downloads/file.rb @@ -22,6 +22,11 @@ module Downloads @data[:get_thumbnail] = options[:get_thumbnail] end + def rewrite_url + url, _, _ = before_download(@source, @data) + return url + end + def size url, headers, _ = before_download(@source, @data) options = { timeout: 3, headers: headers }.deep_merge(Danbooru.config.httparty_options) diff --git a/app/logical/storage_manager/match.rb b/app/logical/storage_manager/match.rb index 7fe97f9be..564b71d6e 100644 --- a/app/logical/storage_manager/match.rb +++ b/app/logical/storage_manager/match.rb @@ -103,9 +103,9 @@ class StorageManager::Match < StorageManager end end - def file_path(post, type, **options) + def file_path(post, file_ext, type, **options) find(id: post.id, type: type) do |manager| - manager.file_path(post, type, **options) + manager.file_path(post, file_ext, type, **options) end end end diff --git a/app/logical/upload_service/preprocessor.rb b/app/logical/upload_service/preprocessor.rb index f14afc4e9..75b27c42e 100644 --- a/app/logical/upload_service/preprocessor.rb +++ b/app/logical/upload_service/preprocessor.rb @@ -15,9 +15,15 @@ class UploadService params[:md5_confirmation] end + def normalized_source + @normalized_source ||= begin + Downloads::File.new(params[:source]).rewrite_url + end + end + def in_progress? if Utils.is_downloadable?(source) - Upload.where(status: "preprocessing", source: source).exists? + Upload.where(status: "preprocessing", source: normalized_source).exists? elsif md5.present? Upload.where(status: "preprocessing", md5: md5).exists? else @@ -27,7 +33,7 @@ class UploadService def predecessor if Utils.is_downloadable?(source) - Upload.where(status: ["preprocessed", "preprocessing"], source: source).first + Upload.where(status: ["preprocessed", "preprocessing"], source: normalized_source).first elsif md5.present? Upload.where(status: ["preprocessed", "preprocessing"], md5: md5).first end @@ -49,17 +55,17 @@ class UploadService def start! if Utils.is_downloadable?(source) CurrentUser.as_system do - if Post.tag_match("source:#{source}").where.not(id: original_post_id).exists? - raise ActiveRecord::RecordNotUnique.new("A post with source #{source} already exists") + if Post.tag_match("source:#{normalized_source}").where.not(id: original_post_id).exists? + raise ActiveRecord::RecordNotUnique.new("A post with source #{normalized_source} already exists") end end - if Upload.where(source: source, status: "completed").exists? - raise ActiveRecord::RecordNotUnique.new("A completed upload with source #{source} already exists") + if Upload.where(source: normalized_source, status: "completed").exists? + raise ActiveRecord::RecordNotUnique.new("A completed upload with source #{normalized_source} already exists") end - if Upload.where(source: source).where("status like ?", "error%").exists? - raise ActiveRecord::RecordNotUnique.new("An errored upload with source #{source} already exists") + if Upload.where(source: normalized_source).where("status like ?", "error%").exists? + raise ActiveRecord::RecordNotUnique.new("An errored upload with source #{normalized_source} already exists") end end