Fix #4906: Trying to replace a sourceless post that matches md5 returns error and doesn't fix source.

Allow replacing a post with itself. Skip uploading the file and just
update the post's source with the given source.
This commit is contained in:
evazion
2022-01-11 16:09:14 -06:00
parent 17fb34922b
commit df09bb239b
2 changed files with 18 additions and 14 deletions

View File

@@ -28,13 +28,15 @@ class UploadService
def process!
media_file = Utils::get_file_for_upload(replacement.replacement_url, nil, replacement.replacement_file&.tempfile)
if media_file.md5 == post.md5
raise Error, "Can't replace a post with itself; regenerate the post instead"
elsif Post.exists?(md5: media_file.md5)
if Post.where.not(id: post.id).exists?(md5: media_file.md5)
raise Error, "Duplicate: post with md5 #{media_file.md5} already exists"
end
media_asset = MediaAsset.upload!(media_file)
if media_file.md5 == post.md5
media_asset = post.media_asset
else
media_asset = MediaAsset.upload!(media_file)
end
replacement.replacement_url = replacement_url
replacement.file_ext = media_asset.file_ext

View File

@@ -272,11 +272,11 @@ class UploadServiceTest < ActiveSupport::TestCase
end
context "a post with the same file" do
should "raise an error" do
should "update the source" do
upload_file("test/files/test.png") do |file|
assert_raises(UploadService::Replacer::Error) do
as(@user) { @post.reload.replace!(replacement_file: file, replacement_url: "") }
end
as(@user) { @post.reload.replace!(replacement_file: file, replacement_url: "", final_source: "blah") }
assert_equal("blah", @post.reload.source)
end
end
end
@@ -314,16 +314,18 @@ class UploadServiceTest < ActiveSupport::TestCase
end
as(@user) do
@post_md5 = "710fd9cba4ef37260f9152ffa9d154d8"
@post = FactoryBot.create(:post, source: "https://cdn.donmai.us/original/71/0f/#{@post_md5}.png", file_ext: "png", md5: @post_md5, uploader_ip_addr: "127.0.0.2")
@post_source = "https://cdn.donmai.us/original/71/0f/#{@post_md5}.png"
@post = FactoryBot.create(:post, source: @post_source, file_ext: "png", md5: @post_md5, uploader_ip_addr: "127.0.0.2")
@replacement = FactoryBot.create(:post_replacement, post: @post, replacement_url: @new_url)
end
end
context "when replacing with its own source" do
should "raise an error" do
assert_raises(UploadService::Replacer::Error) do
as(@user) { @post.reload.replace!(replacement_url: @post.source) }
end
context "when replacing a post with the same file as itself" do
should "update the source" do
@post.update!(source: "blah")
as(@user) { @post.reload.replace!(replacement_url: @post_source) }
assert_equal(@post_source, @post.reload.source)
end
end