From 8d22ab9de80e39248c86f782791417ac24cade11 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 26 Jun 2017 11:38:38 -0500 Subject: [PATCH] post replacements: record upload source/filename in replacement_url. * Record "file://#{filename}" as the replacement url when the replacement comes from an uploaded file. * Record the actual url downloaded by the upload process otherwise. This may be different from the url given by the user, since the upload process may rewrite the url. --- app/models/post_replacement.rb | 8 +++++++- test/unit/post_replacement_test.rb | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index b41c314de..d9d210253 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -27,6 +27,12 @@ class PostReplacement < ApplicationRecord upload.process_upload upload.update(status: "completed", post_id: post.id) + if replacement_file.present? + update(replacement_url: "file://#{replacement_file.original_filename}") + else + update(replacement_url: upload.source) + end + # queue the deletion *before* updating the post so that we use the old # md5/file_ext to delete the old files. if saving the post fails, # this is rolled back so the job won't run. @@ -112,7 +118,7 @@ class PostReplacement < ApplicationRecord end def replacement_message - linked_source = linked_source(post.source) + linked_source = linked_source(replacement_url) linked_source_was = linked_source(post.source_was) <<-EOS.strip_heredoc diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index cfddbcfd0..164dd7e3f 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -134,6 +134,7 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal("4ceadc314938bc27f3574053a3e1459a", @post.md5) assert_equal("4ceadc314938bc27f3574053a3e1459a", Digest::MD5.file(@post.file_path).hexdigest) assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.source) + assert_equal("https://i.pximg.net/img-original/img/2017/04/04/08/54/15/62247350_p0.png", @post.replacements.last.replacement_url) end should "delete the old files after three days" do @@ -196,9 +197,11 @@ class PostReplacementTest < ActiveSupport::TestCase Tempfile.open do |file| file.write(File.read("#{Rails.root}/test/files/test.png")) file.seek(0) + uploaded_file = ActionDispatch::Http::UploadedFile.new(tempfile: file, filename: "test.png") - @post.replace!(replacement_file: file, replacement_url: "") + @post.replace!(replacement_file: uploaded_file, replacement_url: "") assert_equal(@post.md5, Digest::MD5.file(file).hexdigest) + assert_equal("file://test.png", @post.replacements.last.replacement_url) end end end