fixes #3855
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
class UploadService
|
||||
class Replacer
|
||||
extend Memoist
|
||||
|
||||
attr_reader :post, :replacement
|
||||
|
||||
def initialize(post:, replacement:)
|
||||
@@ -58,6 +60,26 @@ class UploadService
|
||||
undoer.process!
|
||||
end
|
||||
|
||||
def source_strategy(upload)
|
||||
return Sources::Strategies.find(upload.source, upload.referer_url)
|
||||
end
|
||||
|
||||
def find_replacement_url(repl, upload)
|
||||
if repl.replacement_file.present?
|
||||
return "file://#{repl.replacement_file.original_filename}"
|
||||
end
|
||||
|
||||
if !upload.source.present?
|
||||
raise "No source found in upload for replacement"
|
||||
end
|
||||
|
||||
if source_strategy(upload).canonical_url.present?
|
||||
return source_strategy(upload).canonical_url
|
||||
end
|
||||
|
||||
return upload.source
|
||||
end
|
||||
|
||||
def process!
|
||||
preprocessor = Preprocessor.new(
|
||||
rating: post.rating,
|
||||
@@ -72,11 +94,7 @@ class UploadService
|
||||
return if upload.is_errored?
|
||||
md5_changed = upload.md5 != post.md5
|
||||
|
||||
if replacement.replacement_file.present?
|
||||
replacement.replacement_url = "file://#{replacement.replacement_file.original_filename}"
|
||||
elsif upload.source.present?
|
||||
replacement.replacement_url = Sources::Strategies.canonical(upload.source, upload.referer_url)
|
||||
end
|
||||
replacement.replacement_url = find_replacement_url(replacement, upload)
|
||||
|
||||
if md5_changed
|
||||
post.queue_delete_files(PostReplacement::DELETION_GRACE_PERIOD)
|
||||
@@ -93,7 +111,7 @@ class UploadService
|
||||
post.image_width = upload.image_width
|
||||
post.image_height = upload.image_height
|
||||
post.file_size = upload.file_size
|
||||
post.source = Sources::Strategies.canonical(upload.source, upload.referer_url)
|
||||
post.source = replacement.replacement_url
|
||||
post.tag_string = upload.tag_string
|
||||
|
||||
update_ugoira_frame_data(post, upload)
|
||||
|
||||
@@ -592,6 +592,32 @@ class UploadServiceTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
context "for a twitter source replacement" do
|
||||
setup do
|
||||
@new_url = "https://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:orig"
|
||||
|
||||
travel_to(1.month.ago) do
|
||||
@user = FactoryBot.create(:user)
|
||||
end
|
||||
|
||||
as_user do
|
||||
@post = FactoryBot.create(:post, source: "http://blah", file_ext: "jpg", md5: "something", uploader_ip_addr: "127.0.0.2")
|
||||
@post.stubs(:queue_delete_files)
|
||||
@replacement = FactoryBot.create(:post_replacement, post: @post, replacement_url: @new_url)
|
||||
end
|
||||
end
|
||||
|
||||
subject { UploadService::Replacer.new(post: @post, replacement: @replacement) }
|
||||
|
||||
should "replace the post" do
|
||||
as_user { subject.process! }
|
||||
|
||||
@post.reload
|
||||
|
||||
assert_equal(@new_url, @post.replacements.last.replacement_url)
|
||||
end
|
||||
end
|
||||
|
||||
context "for a source replacement" do
|
||||
setup do
|
||||
@new_url = "https://raikou1.donmai.us/d3/4e/d34e4cf0a437a5d65f8e82b7bcd02606.jpg"
|
||||
|
||||
Reference in New Issue
Block a user