2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Jobs::PullHotlinkedImages do
|
2017-09-01 10:26:13 -04:00
|
|
|
let(:image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat1.png" }
|
|
|
|
let(:broken_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat2.png" }
|
|
|
|
let(:large_image_url) { "http://wiki.mozilla.org/images/2/2e/Longcat3.png" }
|
2020-05-29 12:47:05 -04:00
|
|
|
let(:encoded_image_url) { "https://example.com/אלחוט-.jpg" }
|
2017-09-01 10:26:13 -04:00
|
|
|
let(:png) do
|
|
|
|
Base64.decode64(
|
|
|
|
"R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==",
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2017-09-01 10:26:13 -04:00
|
|
|
let(:large_png) do
|
|
|
|
Base64.decode64(
|
|
|
|
"iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAK10lEQVR42r3aeVRTVx4H8Oc2atWO7Sw9OnM6HWvrOON0aFlcAZ3RopZWOyqgoCACKqPWBUVQi4gIqAVllciiKPu+JOyGnQQSNgkIIQgoKljAYVARCZnf4yXhkeXlJmDP+f4hOUF+n3fvffe++y5W0i4qJqWoDU8hKQUPxWFKcq9VnHxJ8gTi5EqS0yJOtiRZfHEyJWE0i0MnJaMJTzopaQ/wpJKS0ogneTQYABANTDlDvpxBCsiu72eUP0zPq8Fzr45e8TircRDFQAAy5ABpcgDCgJV2iCbRQM+rinU/E26ie9NgfrDO1GBtTBy96SH/WhBhaxwfGEjndmfKGeiaGsYAJXIANQyCkfR05u3dhuOKVhLamnmRzocyKp9mNo9QG9IRDDiAiMaG3Nqfo45aoJROzk3DDxNCbjGahBM0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uWKDnAme7bvPlI1a/Hfz4ljp+BfnqPPKD/DzQWIVWNoUiJAAAAAElFTkSuQmCC",
|
|
|
|
)
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2019-12-18 00:51:57 -05:00
|
|
|
let(:upload_path) { Discourse.store.upload_path }
|
2017-09-01 10:26:13 -04:00
|
|
|
|
|
|
|
before do
|
2020-08-05 20:06:34 -04:00
|
|
|
Jobs.run_immediately!
|
|
|
|
|
2017-09-01 10:26:13 -04:00
|
|
|
stub_request(:get, image_url).to_return(body: png, headers: { "Content-Type" => "image/png" })
|
2020-05-29 12:47:05 -04:00
|
|
|
stub_request(:get, encoded_image_url).to_return(
|
|
|
|
body: png,
|
|
|
|
headers: {
|
|
|
|
"Content-Type" => "image/png",
|
|
|
|
},
|
|
|
|
)
|
2018-02-24 06:35:57 -05:00
|
|
|
stub_request(:get, broken_image_url).to_return(status: 404)
|
2017-09-01 10:26:13 -04:00
|
|
|
stub_request(:get, large_image_url).to_return(
|
|
|
|
body: large_png,
|
|
|
|
headers: {
|
|
|
|
"Content-Type" => "image/png",
|
|
|
|
},
|
|
|
|
)
|
2019-06-06 02:55:46 -04:00
|
|
|
|
|
|
|
stub_request(
|
|
|
|
:get,
|
2019-12-18 00:51:57 -05:00
|
|
|
"#{Discourse.base_url}/#{upload_path}/original/1X/f59ea56fe8ebe42048491d43a19d9f34c5d0f8dc.gif",
|
2019-06-06 02:55:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
stub_request(
|
|
|
|
:get,
|
2019-12-18 00:51:57 -05:00
|
|
|
"#{Discourse.base_url}/#{upload_path}/original/1X/c530c06cf89c410c0355d7852644a73fc3ec8c04.png",
|
2019-06-06 02:55:46 -04:00
|
|
|
)
|
|
|
|
|
2017-09-01 10:26:13 -04:00
|
|
|
SiteSetting.download_remote_images_to_local = true
|
|
|
|
SiteSetting.max_image_size_kb = 2
|
2017-09-01 13:12:47 -04:00
|
|
|
SiteSetting.download_remote_images_threshold = 0
|
2017-09-01 10:26:13 -04:00
|
|
|
end
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2019-06-06 02:55:46 -04:00
|
|
|
describe "#execute" do
|
|
|
|
before { Jobs.run_immediately! }
|
|
|
|
|
2021-02-03 00:45:07 -05:00
|
|
|
it "does nothing if topic has been deleted" do
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{image_url}'>")
|
|
|
|
post.topic.destroy!
|
|
|
|
|
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.not_to change {
|
2022-07-19 10:03:03 -04:00
|
|
|
Upload.count
|
|
|
|
}
|
2021-02-03 00:45:07 -05:00
|
|
|
end
|
|
|
|
|
2019-06-06 02:55:46 -04:00
|
|
|
it "does nothing if there are no large images to pull" do
|
2017-11-15 23:13:15 -05:00
|
|
|
post = Fabricate(:post, raw: "bob bob")
|
|
|
|
orig = post.updated_at
|
|
|
|
|
|
|
|
freeze_time 1.week.from_now
|
|
|
|
Jobs::PullHotlinkedImages.new.execute(post_id: post.id)
|
2020-03-10 17:13:17 -04:00
|
|
|
expect(orig).to eq_time(post.reload.updated_at)
|
2017-11-15 23:13:15 -05:00
|
|
|
end
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
it "replaces images" do
|
2017-09-01 10:26:13 -04:00
|
|
|
post = Fabricate(:post, raw: "<img src='#{image_url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1).and not_change { UserHistory.count } # Should not add to the staff log
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2022-05-16 12:56:00 -04:00
|
|
|
it "enqueues raw replacement job with a delay" do
|
|
|
|
Jobs.run_later!
|
|
|
|
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{image_url}'>")
|
|
|
|
stub_image_size
|
|
|
|
|
|
|
|
freeze_time
|
|
|
|
Jobs.expects(:cancel_scheduled_job).with(:update_hotlinked_raw, post_id: post.id).once
|
|
|
|
delay = SiteSetting.editing_grace_period + 1
|
|
|
|
|
|
|
|
expect_enqueued_with(
|
|
|
|
job: :update_hotlinked_raw,
|
|
|
|
args: {
|
|
|
|
post_id: post.id,
|
|
|
|
},
|
|
|
|
at: Time.zone.now + delay.seconds,
|
|
|
|
) { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }
|
|
|
|
end
|
|
|
|
|
2020-08-05 20:06:34 -04:00
|
|
|
it "removes downloaded images when they are no longer needed" do
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{image_url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-08-05 20:06:34 -04:00
|
|
|
post.rebake!
|
|
|
|
post.reload
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.count).to eq(1)
|
2020-08-05 20:06:34 -04:00
|
|
|
|
|
|
|
post.update(raw: "Post with no images")
|
|
|
|
post.rebake!
|
|
|
|
post.reload
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.count).to eq(0)
|
2020-08-05 20:06:34 -04:00
|
|
|
end
|
|
|
|
|
2020-08-06 05:01:53 -04:00
|
|
|
it "replaces images again after edit" do
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{image_url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-08-06 05:01:53 -04:00
|
|
|
|
|
|
|
expect do post.rebake! end.to change { Upload.count }.by(1)
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2020-08-06 05:01:53 -04:00
|
|
|
|
|
|
|
# Post raw is updated back to the old value (e.g. by wordpress integration)
|
|
|
|
post.update(raw: "<img src='#{image_url}'>")
|
|
|
|
|
2022-07-19 10:03:03 -04:00
|
|
|
expect do post.rebake! end.not_to change { Upload.count } # We alread have the upload
|
2020-08-06 05:01:53 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2020-08-06 05:01:53 -04:00
|
|
|
end
|
|
|
|
|
2020-05-29 12:47:05 -04:00
|
|
|
it "replaces encoded image urls" do
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{encoded_image_url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-05-29 12:47:05 -04:00
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2020-05-29 12:47:05 -04:00
|
|
|
end
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
it "replaces images in an anchor tag with weird indentation" do
|
2020-08-05 20:06:34 -04:00
|
|
|
# Skipped pending https://meta.discourse.org/t/152801
|
|
|
|
# This spec was previously passing, even though the resulting markdown was invalid
|
|
|
|
# Now the spec has been improved, and shows the issue
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
stub_request(
|
|
|
|
:get,
|
|
|
|
"http://test.localhost/uploads/short-url/z2QSs1KJWoj51uYhDjb6ifCzxH6.gif",
|
|
|
|
).to_return(status: 200, body: "")
|
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
2019-06-21 00:32:02 -04:00
|
|
|
<h1></h1>
|
2019-06-20 21:55:39 -04:00
|
|
|
<a href="https://somelink.com">
|
2022-03-29 05:55:10 -04:00
|
|
|
<img alt="somelink" src="#{image_url}">
|
2019-06-20 21:55:39 -04:00
|
|
|
</a>
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
MD
|
2019-06-20 21:55:39 -04:00
|
|
|
|
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
2019-06-21 00:32:02 -04:00
|
|
|
upload = post.uploads.last
|
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
expect(post.reload.raw).to eq(<<~MD.chomp)
|
2019-06-21 00:32:02 -04:00
|
|
|
<h1></h1>
|
2019-06-20 21:55:39 -04:00
|
|
|
<a href="https://somelink.com">
|
2022-03-29 05:55:10 -04:00
|
|
|
<img alt="somelink" src="#{upload.short_url}">
|
2019-06-20 21:55:39 -04:00
|
|
|
</a>
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
MD
|
2019-06-20 21:55:39 -04:00
|
|
|
end
|
|
|
|
|
2020-02-26 23:52:55 -05:00
|
|
|
it "replaces correct image URL" do
|
|
|
|
url = image_url.sub("/2e/Longcat1.png", "")
|
|
|
|
post = Fabricate(:post, raw: "[Images](#{url})\n![](#{image_url})")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-02-26 23:52:55 -05:00
|
|
|
|
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
expect(post.reload.raw).to eq("[Images](#{url})\n![](#{Upload.last.short_url})")
|
|
|
|
end
|
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
it "replaces images without protocol" do
|
2017-09-01 10:26:13 -04:00
|
|
|
url = image_url.sub(/^https?\:/, "")
|
2019-06-06 03:50:35 -04:00
|
|
|
post = Fabricate(:post, raw: "<img alt='test' src='#{url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2017-01-16 05:50:07 -05:00
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
2017-06-13 07:27:05 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img alt=\"test\" src=\"#{Upload.last.short_url}\">")
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
2017-06-13 07:27:05 -04:00
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
it "replaces images without extension" do
|
2017-09-01 10:26:13 -04:00
|
|
|
url = image_url.sub(/\.[a-zA-Z0-9]+$/, "")
|
|
|
|
stub_request(:get, url).to_return(body: png, headers: { "Content-Type" => "image/png" })
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2017-06-13 07:27:05 -04:00
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
expect do Jobs::PullHotlinkedImages.new.execute(post_id: post.id) end.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
2017-06-02 05:39:06 -04:00
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.reload.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
2017-06-02 05:39:06 -04:00
|
|
|
|
2019-05-23 03:43:25 -04:00
|
|
|
it "replaces optimized images" do
|
|
|
|
optimized_image = Fabricate(:optimized_image)
|
|
|
|
url = "#{Discourse.base_url}#{optimized_image.url}"
|
|
|
|
|
|
|
|
stub_request(:get, url).to_return(status: 200, body: file_from_fixtures("smallest.png"))
|
|
|
|
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{url}'>")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2019-05-23 03:43:25 -04:00
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
upload = Upload.last
|
|
|
|
post.reload
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2019-05-23 03:43:25 -04:00
|
|
|
expect(post.uploads).to contain_exactly(upload)
|
|
|
|
end
|
|
|
|
|
2022-05-16 12:56:00 -04:00
|
|
|
it "skips editing raw for raw_html posts" do
|
2022-04-04 15:14:48 -04:00
|
|
|
raw = "<img src=\"#{image_url}\">"
|
|
|
|
post = Fabricate(:post, raw: raw, cook_method: Post.cook_methods[:raw_html])
|
|
|
|
stub_image_size
|
|
|
|
expect do
|
|
|
|
post.rebake!
|
|
|
|
post.reload
|
2022-05-16 12:56:00 -04:00
|
|
|
end.to change { Upload.count }.by(1)
|
2022-04-04 15:14:48 -04:00
|
|
|
|
|
|
|
expect(post.raw).to eq(raw)
|
|
|
|
end
|
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
context "when secure uploads enabled for an upload that has already been downloaded and exists" do
|
2020-01-23 20:59:30 -05:00
|
|
|
it "doesnt redownload the secure upload" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-09-14 07:32:25 -04:00
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
upload = Fabricate(:secure_upload_s3, secure: true)
|
2020-01-23 20:59:30 -05:00
|
|
|
stub_s3(upload)
|
2022-09-28 19:24:33 -04:00
|
|
|
url = Upload.secure_uploads_url_from_upload_url(upload.url)
|
2020-01-23 20:59:30 -05:00
|
|
|
url = Discourse.base_url + url
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{url}'>")
|
2020-01-28 19:11:38 -05:00
|
|
|
upload.update(access_control_post: post)
|
2020-01-23 20:59:30 -05:00
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.not_to change {
|
|
|
|
Upload.count
|
|
|
|
}
|
|
|
|
end
|
2020-01-28 19:11:38 -05:00
|
|
|
|
|
|
|
context "when the upload original_sha1 is missing" do
|
|
|
|
it "redownloads the upload" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-09-14 07:32:25 -04:00
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
upload = Fabricate(:upload_s3, secure: true)
|
|
|
|
stub_s3(upload)
|
2022-09-28 19:24:33 -04:00
|
|
|
Upload.stubs(:signed_url_from_secure_uploads_url).returns(upload.url)
|
|
|
|
url = Upload.secure_uploads_url_from_upload_url(upload.url)
|
2020-01-28 19:11:38 -05:00
|
|
|
url = Discourse.base_url + url
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{url}'>")
|
|
|
|
upload.update(access_control_post: post)
|
|
|
|
FileStore::S3Store.any_instance.stubs(:store_upload).returns(upload.url)
|
|
|
|
|
|
|
|
# without this we get an infinite hang...
|
|
|
|
Post.any_instance.stubs(:trigger_post_process)
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the upload access_control_post is different to the current post" do
|
|
|
|
it "redownloads the upload" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-09-14 07:32:25 -04:00
|
|
|
|
2020-01-28 19:11:38 -05:00
|
|
|
upload = Fabricate(:secure_upload_s3, secure: true)
|
|
|
|
stub_s3(upload)
|
2022-09-28 19:24:33 -04:00
|
|
|
Upload.stubs(:signed_url_from_secure_uploads_url).returns(upload.url)
|
|
|
|
url = Upload.secure_uploads_url_from_upload_url(upload.url)
|
2020-01-28 19:11:38 -05:00
|
|
|
url = Discourse.base_url + url
|
|
|
|
post = Fabricate(:post, raw: "<img src='#{url}'>")
|
|
|
|
upload.update(access_control_post: Fabricate(:post))
|
|
|
|
FileStore::S3Store.any_instance.stubs(:store_upload).returns(upload.url)
|
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
2020-08-05 07:14:59 -04:00
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.not_to change {
|
2022-07-19 10:03:03 -04:00
|
|
|
Upload.count
|
|
|
|
}
|
2020-01-28 19:11:38 -05:00
|
|
|
end
|
|
|
|
end
|
2020-01-23 20:59:30 -05:00
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
it "replaces markdown image" do
|
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
|
|
|
[![some test](#{image_url})](https://somelink.com)
|
|
|
|
![some test](#{image_url})
|
|
|
|
![](#{image_url})
|
|
|
|
![abcde](#{image_url} 'some test')
|
|
|
|
![](#{image_url} 'some test')
|
|
|
|
MD
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2019-06-06 03:50:35 -04:00
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.raw).to eq(<<~MD.chomp)
|
|
|
|
[![some test](#{Upload.last.short_url})](https://somelink.com)
|
|
|
|
![some test](#{Upload.last.short_url})
|
|
|
|
![](#{Upload.last.short_url})
|
|
|
|
![abcde](#{Upload.last.short_url} 'some test')
|
|
|
|
![](#{Upload.last.short_url} 'some test')
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2020-06-19 07:52:51 -04:00
|
|
|
it "works when invalid url in post" do
|
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
|
|
|
![some test](#{image_url})
|
|
|
|
![some test 2]("#{image_url})
|
|
|
|
MD
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-06-19 07:52:51 -04:00
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
end
|
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
it "replaces bbcode images" do
|
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
|
|
|
[img]
|
|
|
|
#{image_url}
|
|
|
|
[/img]
|
|
|
|
|
|
|
|
[img]
|
|
|
|
#{image_url}
|
|
|
|
[/img]
|
|
|
|
MD
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2019-06-06 03:50:35 -04:00
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.raw).to eq(<<~MD.chomp)
|
|
|
|
![](#{Upload.last.short_url})
|
|
|
|
|
|
|
|
![](#{Upload.last.short_url})
|
|
|
|
MD
|
|
|
|
end
|
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
describe "onebox" do
|
|
|
|
let(:media) { "File:Brisbane_May_2013201.jpg" }
|
|
|
|
let(:url) { "https://commons.wikimedia.org/wiki/#{media}" }
|
|
|
|
let(:api_url) do
|
|
|
|
"https://en.wikipedia.org/w/api.php?action=query&titles=#{media}&prop=imageinfo&iilimit=50&iiprop=timestamp|user|url&iiurlwidth=500&format=json"
|
2023-01-09 06:18:21 -05:00
|
|
|
end
|
2017-07-06 04:55:28 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
stub_request(:head, url)
|
2018-02-24 06:35:57 -05:00
|
|
|
stub_request(:get, url).to_return(body: "")
|
2022-07-04 22:47:10 -04:00
|
|
|
stub_request(:head, image_url)
|
2019-06-06 03:50:35 -04:00
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
stub_request(:get, api_url).to_return(
|
|
|
|
body:
|
|
|
|
"{
|
|
|
|
\"query\": {
|
|
|
|
\"pages\": {
|
|
|
|
\"-1\": {
|
|
|
|
\"title\": \"#{media}\",
|
|
|
|
\"imageinfo\": [{
|
|
|
|
\"thumburl\": \"#{image_url}\",
|
|
|
|
\"url\": \"#{image_url}\",
|
|
|
|
\"descriptionurl\": \"#{url}\"
|
|
|
|
}]
|
|
|
|
}
|
2017-06-02 05:39:06 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-06 04:55:28 -04:00
|
|
|
}",
|
|
|
|
)
|
|
|
|
end
|
2017-06-02 05:39:06 -04:00
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
it "replaces image src" do
|
|
|
|
post = Fabricate(:post, raw: "#{url}")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
|
|
|
|
2020-08-05 20:06:34 -04:00
|
|
|
post.rebake!
|
2017-07-06 04:55:28 -04:00
|
|
|
post.reload
|
|
|
|
|
2022-11-24 20:40:31 -05:00
|
|
|
expect(post.cooked).to match(%r{<img src=.*/uploads.*\ class="thumbnail})
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.count).to eq(1)
|
2020-08-05 20:06:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "associates uploads correctly" do
|
|
|
|
post = Fabricate(:post, raw: "#{url}")
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2020-08-05 20:06:34 -04:00
|
|
|
post.rebake!
|
|
|
|
post.reload
|
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.count).to eq(1)
|
2020-08-05 20:06:34 -04:00
|
|
|
|
|
|
|
post.update(raw: "no onebox")
|
|
|
|
post.rebake!
|
|
|
|
post.reload
|
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
expect(post.upload_references.count).to eq(0)
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
2017-09-01 10:26:13 -04:00
|
|
|
|
|
|
|
it "all combinations" do
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
2018-08-17 04:52:55 -04:00
|
|
|
<img src='#{image_url}'>
|
|
|
|
#{url}
|
|
|
|
<img src='#{broken_image_url}'>
|
|
|
|
<a href='#{url}'><img src='#{large_image_url}'></a>
|
2022-07-04 22:47:10 -04:00
|
|
|
#{image_url}
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
MD
|
2021-10-19 07:42:29 -04:00
|
|
|
stub_image_size
|
2017-09-01 10:26:13 -04:00
|
|
|
|
2020-08-05 20:06:34 -04:00
|
|
|
2.times { post.rebake! }
|
2019-06-06 03:50:35 -04:00
|
|
|
|
2017-09-01 10:26:13 -04:00
|
|
|
post.reload
|
|
|
|
|
2019-06-06 03:50:35 -04:00
|
|
|
expect(post.raw).to eq(<<~MD.chomp)
|
2022-03-29 05:55:10 -04:00
|
|
|
<img src="upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif">
|
2019-06-06 03:50:35 -04:00
|
|
|
https://commons.wikimedia.org/wiki/File:Brisbane_May_2013201.jpg
|
|
|
|
<img src='#{broken_image_url}'>
|
|
|
|
<a href='#{url}'><img src='#{large_image_url}'></a>
|
2022-07-04 22:47:10 -04:00
|
|
|
![](upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif)
|
2019-06-06 03:50:35 -04:00
|
|
|
MD
|
|
|
|
|
2017-09-01 10:26:13 -04:00
|
|
|
expect(post.cooked).to match(%r{<p><img src=.*/uploads})
|
2022-11-24 20:40:31 -05:00
|
|
|
expect(post.cooked).to match(%r{<img src=.*/uploads.*\ class="thumbnail})
|
2018-11-26 16:49:57 -05:00
|
|
|
expect(post.cooked).to match(/<span class="broken-image/)
|
2017-11-15 05:30:47 -05:00
|
|
|
expect(post.cooked).to match(/<div class="large-image-placeholder">/)
|
2017-09-01 10:26:13 -04:00
|
|
|
end
|
2022-07-06 06:46:33 -04:00
|
|
|
|
|
|
|
it "rewrites a lone onebox" do
|
|
|
|
post = Fabricate(:post, raw: <<~MD)
|
|
|
|
Onebox here:
|
|
|
|
#{image_url}
|
|
|
|
MD
|
|
|
|
stub_image_size
|
|
|
|
|
|
|
|
post.rebake!
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
|
|
|
expect(post.raw).to eq(<<~MD.chomp)
|
|
|
|
Onebox here:
|
|
|
|
![](upload://z2QSs1KJWoj51uYhDjb6ifCzxH6.gif)
|
|
|
|
MD
|
|
|
|
|
|
|
|
expect(post.cooked).to match(%r{<img src=.*/uploads})
|
|
|
|
end
|
2017-09-01 10:26:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 16:31:12 -04:00
|
|
|
describe "#should_download_image?" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:job) { described_class.new }
|
2017-06-02 05:39:06 -04:00
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
describe "when url is invalid" do
|
|
|
|
it "should return false" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?("null")).to eq(false)
|
|
|
|
expect(job.should_download_image?("meta.discourse.org")).to eq(false)
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
2017-06-02 05:39:06 -04:00
|
|
|
end
|
|
|
|
|
2017-07-06 04:55:28 -04:00
|
|
|
describe "when url is valid" do
|
|
|
|
it "should return true" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?("http://meta.discourse.org")).to eq(true)
|
|
|
|
expect(job.should_download_image?("//meta.discourse.org")).to eq(true)
|
2017-07-06 04:55:28 -04:00
|
|
|
end
|
|
|
|
end
|
2019-03-27 16:31:12 -04:00
|
|
|
|
|
|
|
describe "when url is an upload" do
|
|
|
|
it "should return false for original" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(Fabricate(:upload).url)).to eq(false)
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
|
2022-09-28 19:24:33 -04:00
|
|
|
context "when secure uploads enabled" do
|
|
|
|
it "should return false for secure-upload url" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2022-09-28 19:24:33 -04:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-09-14 07:32:25 -04:00
|
|
|
|
2020-01-23 20:59:30 -05:00
|
|
|
upload = Fabricate(:upload_s3, secure: true)
|
|
|
|
stub_s3(upload)
|
2022-09-28 19:24:33 -04:00
|
|
|
url = Upload.secure_uploads_url_from_upload_url(upload.url)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(url)).to eq(false)
|
2020-01-23 20:59:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-27 16:31:12 -04:00
|
|
|
it "should return true for optimized" do
|
|
|
|
src = Discourse.store.get_path_for_optimized_image(Fabricate(:optimized_image))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(true)
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-19 15:21:05 -04:00
|
|
|
it "returns false for emoji" do
|
|
|
|
src = Emoji.url_for("testemoji.png")
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2020-06-19 15:21:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for emoji when app and S3 CDNs configured" do
|
2020-09-14 07:32:25 -04:00
|
|
|
setup_s3
|
2020-06-19 15:21:05 -04:00
|
|
|
SiteSetting.s3_cdn_url = "https://s3.cdn.com"
|
2020-09-14 07:32:25 -04:00
|
|
|
set_cdn_url "https://mydomain.cdn/test"
|
2020-06-19 15:21:05 -04:00
|
|
|
|
|
|
|
src = UrlHelper.cook_url(Emoji.url_for("testemoji.png"))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2020-06-19 15:21:05 -04:00
|
|
|
end
|
|
|
|
|
2021-03-02 14:04:16 -05:00
|
|
|
it "returns false for emoji when emoji CDN configured" do
|
|
|
|
SiteSetting.external_emoji_url = "https://emoji.cdn.com"
|
|
|
|
|
|
|
|
src = UrlHelper.cook_url(Emoji.url_for("testemoji.png"))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2021-03-02 14:04:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for emoji when app, S3 *and* emoji CDNs configured" do
|
|
|
|
setup_s3
|
|
|
|
SiteSetting.s3_cdn_url = "https://s3.cdn.com"
|
|
|
|
SiteSetting.external_emoji_url = "https://emoji.cdn.com"
|
|
|
|
set_cdn_url "https://mydomain.cdn/test"
|
|
|
|
|
|
|
|
src = UrlHelper.cook_url(Emoji.url_for("testemoji.png"))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2021-03-02 14:04:16 -05:00
|
|
|
end
|
|
|
|
|
2020-06-19 15:21:05 -04:00
|
|
|
it "returns false for plugin assets" do
|
|
|
|
src = UrlHelper.cook_url("/plugins/discourse-amazing-plugin/myasset.png")
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2020-06-19 15:21:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for local non-uploaded files" do
|
|
|
|
src = UrlHelper.cook_url("/mycustomroute.png")
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(false)
|
2020-06-19 15:21:05 -04:00
|
|
|
end
|
|
|
|
|
2019-03-27 16:31:12 -04:00
|
|
|
context "when download_remote_images_to_local? is false" do
|
|
|
|
before { SiteSetting.download_remote_images_to_local = false }
|
|
|
|
|
|
|
|
it "still returns true for optimized" do
|
|
|
|
src = Discourse.store.get_path_for_optimized_image(Fabricate(:optimized_image))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?(src)).to eq(true)
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for valid remote URLs" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(job.should_download_image?("http://meta.discourse.org")).to eq(false)
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a lightboxed image" do
|
2022-11-24 20:40:31 -05:00
|
|
|
fab!(:upload) { Fabricate(:large_image_upload) }
|
2019-05-23 03:43:25 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2019-03-27 16:31:12 -04:00
|
|
|
|
2019-05-23 03:43:25 -04:00
|
|
|
before { Jobs.run_immediately! }
|
|
|
|
|
|
|
|
it "replaces missing local uploads in lightbox link" do
|
|
|
|
post =
|
|
|
|
PostCreator.create!(
|
|
|
|
user,
|
|
|
|
raw: "<img src='#{Discourse.base_url}#{upload.url}'>",
|
|
|
|
title: "Some title that is long enough",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(post.reload.cooked).to have_tag(:a, with: { class: "lightbox" })
|
|
|
|
|
|
|
|
stub_request(:get, "#{Discourse.base_url}#{upload.url}").to_return(
|
|
|
|
status: 200,
|
|
|
|
body: file_from_fixtures("smallest.png"),
|
|
|
|
)
|
|
|
|
|
|
|
|
upload.delete
|
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.to change {
|
|
|
|
Upload.count
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
post.reload
|
|
|
|
|
2022-03-29 05:55:10 -04:00
|
|
|
expect(post.raw).to eq("<img src=\"#{Upload.last.short_url}\">")
|
2019-05-23 03:43:25 -04:00
|
|
|
expect(post.uploads.count).to eq(1)
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't remove optimized images from lightboxes" do
|
2019-05-23 03:43:25 -04:00
|
|
|
post =
|
|
|
|
PostCreator.create!(
|
|
|
|
user,
|
|
|
|
raw: "![alt](#{upload.short_url})",
|
|
|
|
title: "Some title that is long enough",
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(post.reload.cooked).to have_tag(:a, with: { class: "lightbox" })
|
|
|
|
|
|
|
|
expect { Jobs::PullHotlinkedImages.new.execute(post_id: post.id) }.not_to change {
|
|
|
|
Upload.count
|
|
|
|
}
|
|
|
|
|
|
|
|
post.reload
|
2019-03-27 16:31:12 -04:00
|
|
|
|
2019-05-23 03:43:25 -04:00
|
|
|
expect(post.raw).to eq("![alt](#{upload.short_url})")
|
2019-03-27 16:31:12 -04:00
|
|
|
end
|
2017-06-02 05:39:06 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#disable_if_low_on_disk_space" do
|
2022-05-16 12:56:00 -04:00
|
|
|
fab!(:post) { Fabricate(:post, created_at: 20.days.ago) }
|
|
|
|
let(:job) { Jobs::PullHotlinkedImages.new }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.download_remote_images_to_local = true
|
|
|
|
SiteSetting.download_remote_images_threshold = 20
|
|
|
|
job.stubs(:available_disk_space).returns(50)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing when there's enough disk space" do
|
|
|
|
SiteSetting.expects(:download_remote_images_to_local=).never
|
|
|
|
job.execute({ post_id: post.id })
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there's not enough disk space" do
|
|
|
|
before { SiteSetting.download_remote_images_threshold = 75 }
|
|
|
|
|
|
|
|
it "disables download_remote_images_threshold and send a notification to the admin" do
|
|
|
|
StaffActionLogger.any_instance.expects(:log_site_setting_change).once
|
|
|
|
SystemMessage
|
|
|
|
.expects(:create_from_system_user)
|
|
|
|
.with(Discourse.site_contact_user, :download_remote_images_disabled)
|
|
|
|
.once
|
|
|
|
job.execute({ post_id: post.id })
|
|
|
|
|
|
|
|
expect(SiteSetting.download_remote_images_to_local).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't disable download_remote_images_to_local if site uses S3" do
|
|
|
|
setup_s3
|
|
|
|
job.execute({ post_id: post.id })
|
|
|
|
|
|
|
|
expect(SiteSetting.download_remote_images_to_local).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-23 20:59:30 -05:00
|
|
|
def stub_s3(upload)
|
2020-09-14 07:32:25 -04:00
|
|
|
stub_upload(upload)
|
2020-01-28 19:11:38 -05:00
|
|
|
stub_request(:get, "https:" + upload.url).to_return(
|
|
|
|
status: 200,
|
|
|
|
body: file_from_fixtures("smallest.png"),
|
|
|
|
)
|
2020-01-23 20:59:30 -05:00
|
|
|
end
|
2017-01-16 05:50:07 -05:00
|
|
|
end
|