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.
This commit is contained in:
evazion
2017-06-26 11:38:38 -05:00
parent 4fd1c6ec21
commit 8d22ab9de8
2 changed files with 11 additions and 2 deletions

View File

@@ -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

View File

@@ -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