From eede2f075233fe8b292ebf70514242d84c467480 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 18 Dec 2017 17:10:55 -0600 Subject: [PATCH] Fix #3324: Incorporate replacement comment info in the replacement history. --- app/models/post_replacement.rb | 20 ++++++++++++++++--- ...13037_add_metadata_to_post_replacements.rb | 17 ++++++++++++++++ db/structure.sql | 14 ++++++++++++- test/unit/post_replacement_test.rb | 19 +++++++++++++++++- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20171218213037_add_metadata_to_post_replacements.rb diff --git a/app/models/post_replacement.rb b/app/models/post_replacement.rb index e92cac981..19df9392f 100644 --- a/app/models/post_replacement.rb +++ b/app/models/post_replacement.rb @@ -3,13 +3,19 @@ class PostReplacement < ApplicationRecord belongs_to :post belongs_to :creator, class_name: "User" - before_validation :initialize_fields + before_validation :initialize_fields, on: :create attr_accessor :replacement_file, :final_source, :tags def initialize_fields self.creator = CurrentUser.user self.original_url = post.source self.tags = post.tag_string + " " + self.tags.to_s + + self.file_ext_was = post.file_ext + self.file_size_was = post.file_size + self.image_width_was = post.image_width + self.image_height_was = post.image_height + self.md5_was = post.md5 end def undo! @@ -32,9 +38,9 @@ class PostReplacement < ApplicationRecord md5_changed = (upload.md5 != post.md5) if replacement_file.present? - update(replacement_url: "file://#{replacement_file.original_filename}") + self.replacement_url = "file://#{replacement_file.original_filename}" else - update(replacement_url: upload.downloaded_source) + self.replacement_url = upload.downloaded_source end # queue the deletion *before* updating the post so that we use the old @@ -44,6 +50,12 @@ class PostReplacement < ApplicationRecord Post.delay(queue: "default", run_at: Time.now + DELETION_GRACE_PERIOD).delete_files(post.id, post.file_path, post.large_file_path, post.preview_file_path) end + self.file_ext = upload.file_ext + self.file_size = upload.file_size + self.image_height = upload.image_height + self.image_width = upload.image_width + self.md5 = upload.md5 + post.md5 = upload.md5 post.file_ext = upload.file_ext post.image_width = upload.image_width @@ -51,6 +63,7 @@ class PostReplacement < ApplicationRecord post.file_size = upload.file_size post.source = final_source.presence || upload.source post.tag_string = upload.tag_string + rescale_notes update_ugoira_frame_data(upload) @@ -60,6 +73,7 @@ class PostReplacement < ApplicationRecord post.queue_backup end + save! post.save! end diff --git a/db/migrate/20171218213037_add_metadata_to_post_replacements.rb b/db/migrate/20171218213037_add_metadata_to_post_replacements.rb new file mode 100644 index 000000000..1a2e9becd --- /dev/null +++ b/db/migrate/20171218213037_add_metadata_to_post_replacements.rb @@ -0,0 +1,17 @@ +class AddMetadataToPostReplacements < ActiveRecord::Migration + def change + PostReplacement.without_timeout do + add_column :post_replacements, :file_ext_was, :string + add_column :post_replacements, :file_size_was, :integer + add_column :post_replacements, :image_width_was, :integer + add_column :post_replacements, :image_height_was, :integer + add_column :post_replacements, :md5_was, :string + + add_column :post_replacements, :file_ext, :string + add_column :post_replacements, :file_size, :integer + add_column :post_replacements, :image_width, :integer + add_column :post_replacements, :image_height, :integer + add_column :post_replacements, :md5, :string + end + end +end diff --git a/db/structure.sql b/db/structure.sql index bd655ffd4..4a7dba3ad 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -2666,7 +2666,17 @@ CREATE TABLE post_replacements ( original_url text NOT NULL, replacement_url text NOT NULL, created_at timestamp without time zone NOT NULL, - updated_at timestamp without time zone NOT NULL + updated_at timestamp without time zone NOT NULL, + file_ext_was character varying, + file_size_was integer, + image_width_was integer, + image_height_was integer, + md5_was character varying, + file_ext character varying, + file_size integer, + image_width integer, + image_height integer, + md5 character varying ); @@ -7530,3 +7540,5 @@ INSERT INTO schema_migrations (version) VALUES ('20171106075030'); INSERT INTO schema_migrations (version) VALUES ('20171127195124'); +INSERT INTO schema_migrations (version) VALUES ('20171218213037'); + diff --git a/test/unit/post_replacement_test.rb b/test/unit/post_replacement_test.rb index 3785bafea..d71327795 100644 --- a/test/unit/post_replacement_test.rb +++ b/test/unit/post_replacement_test.rb @@ -30,7 +30,7 @@ class PostReplacementTest < ActiveSupport::TestCase def teardown super - + CurrentUser.user = nil CurrentUser.ip_addr = nil Delayed::Worker.delay_jobs = false @@ -49,6 +49,7 @@ class PostReplacementTest < ActiveSupport::TestCase setup do @post.update(source: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png") @post.replace!(replacement_url: "https://www.google.com/intl/en_ALL/images/logo.gif", tags: "-tag1 tag2") + @replacement = @post.replacements.last @upload = Upload.last end @@ -79,6 +80,22 @@ class PostReplacementTest < ActiveSupport::TestCase assert_equal(@post.id, PostReplacement.last.post_id) end + should "record the old file metadata" do + assert_equal(500, @replacement.image_width_was) + assert_equal(335, @replacement.image_height_was) + assert_equal(28086, @replacement.file_size_was) + assert_equal("jpg", @replacement.file_ext_was) + assert_equal("ecef68c44edb8a0d6a3070b5f8e8ee76", @replacement.md5_was) + end + + should "record the new file metadata" do + assert_equal(276, @replacement.image_width) + assert_equal(110, @replacement.image_height) + assert_equal(8558, @replacement.file_size) + assert_equal("gif", @replacement.file_ext) + assert_equal("e80d1c59a673f560785784fb1ac10959", @replacement.md5) + end + should "correctly update the attributes" do assert_equal(@post.id, @upload.post.id) assert_equal("completed", @upload.status)