Fix #3197 - Post Replacement comment: use Replacement URL instead of Final Source
Record the URL of the image that was actually downloaded as the replacement URL.
This commit is contained in:
@@ -3,7 +3,7 @@ module Downloads
|
|||||||
class Error < Exception ; end
|
class Error < Exception ; end
|
||||||
|
|
||||||
attr_reader :data, :options
|
attr_reader :data, :options
|
||||||
attr_accessor :source, :original_source, :content_type, :file_path
|
attr_accessor :source, :original_source, :downloaded_source, :content_type, :file_path
|
||||||
|
|
||||||
def initialize(source, file_path, options = {})
|
def initialize(source, file_path, options = {})
|
||||||
# source can potentially get rewritten in the course
|
# source can potentially get rewritten in the course
|
||||||
@@ -11,6 +11,9 @@ module Downloads
|
|||||||
@source = source
|
@source = source
|
||||||
@original_source = source
|
@original_source = source
|
||||||
|
|
||||||
|
# the URL actually downloaded after rewriting the original source.
|
||||||
|
@downloaded_source = nil
|
||||||
|
|
||||||
# where to save the download
|
# where to save the download
|
||||||
@file_path = file_path
|
@file_path = file_path
|
||||||
|
|
||||||
@@ -43,6 +46,7 @@ module Downloads
|
|||||||
response.read_body(out)
|
response.read_body(out)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@downloaded_source = @source
|
||||||
@source = after_download(@source)
|
@source = after_download(@source)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class PostReplacement < ApplicationRecord
|
|||||||
if replacement_file.present?
|
if replacement_file.present?
|
||||||
update(replacement_url: "file://#{replacement_file.original_filename}")
|
update(replacement_url: "file://#{replacement_file.original_filename}")
|
||||||
else
|
else
|
||||||
update(replacement_url: upload.source)
|
update(replacement_url: upload.downloaded_source)
|
||||||
end
|
end
|
||||||
|
|
||||||
# queue the deletion *before* updating the post so that we use the old
|
# queue the deletion *before* updating the post so that we use the old
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class Upload < ApplicationRecord
|
|||||||
attr_accessor :file, :image_width, :image_height, :file_ext, :md5,
|
attr_accessor :file, :image_width, :image_height, :file_ext, :md5,
|
||||||
:file_size, :as_pending, :artist_commentary_title,
|
:file_size, :as_pending, :artist_commentary_title,
|
||||||
:artist_commentary_desc, :include_artist_commentary,
|
:artist_commentary_desc, :include_artist_commentary,
|
||||||
:referer_url
|
:referer_url, :downloaded_source
|
||||||
belongs_to :uploader, :class_name => "User"
|
belongs_to :uploader, :class_name => "User"
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
before_validation :initialize_uploader, :on => :create
|
before_validation :initialize_uploader, :on => :create
|
||||||
@@ -110,7 +110,7 @@ class Upload < ApplicationRecord
|
|||||||
update_attribute(:status, "processing")
|
update_attribute(:status, "processing")
|
||||||
self.source = strip_source
|
self.source = strip_source
|
||||||
if is_downloadable?
|
if is_downloadable?
|
||||||
self.source = download_from_source(temp_file_path)
|
self.downloaded_source, self.source = download_from_source(temp_file_path)
|
||||||
end
|
end
|
||||||
validate_file_exists
|
validate_file_exists
|
||||||
self.content_type = file_header_to_content_type(file_path)
|
self.content_type = file_header_to_content_type(file_path)
|
||||||
@@ -407,7 +407,7 @@ class Upload < ApplicationRecord
|
|||||||
download = Downloads::File.new(source, destination_path, :referer_url => referer_url)
|
download = Downloads::File.new(source, destination_path, :referer_url => referer_url)
|
||||||
download.download!
|
download.download!
|
||||||
ugoira_service.load(download.data)
|
ugoira_service.load(download.data)
|
||||||
download.source
|
[download.downloaded_source, download.source]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -216,5 +216,15 @@ class PostReplacementTest < ActiveSupport::TestCase
|
|||||||
assert_equal(final_source, @post.source)
|
assert_equal(final_source, @post.source)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "a post when replaced with a HTML source" do
|
||||||
|
should "record the image URL as the replacement URL, not the HTML source" do
|
||||||
|
replacement_url = "https://twitter.com/nounproject/status/540944400767922176"
|
||||||
|
image_url = "http://pbs.twimg.com/media/B4HSEP5CUAA4xyu.png:orig"
|
||||||
|
@post.replace!(replacement_url: replacement_url)
|
||||||
|
|
||||||
|
assert_equal(image_url, @post.replacements.last.replacement_url)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user