Skip to content

A platform for community discussion. Free, open, simple.

FIX: Quoting videos can show a corrupted thumbnail (#31079)

Blake Ericksondfb64f9b8420
diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js
index b6cbf19ef2a..aae661e00ec 100644
--- a/app/assets/javascripts/discourse/app/lib/utilities.js
+++ b/app/assets/javascripts/discourse/app/lib/utilities.js
@@ -169,7 +169,13 @@ export function selectedText() {
div
.querySelectorAll("div.video-placeholder-container[data-video-src]")
.forEach((element) => {
- element.replaceWith(`![|video](${element.dataset.videoSrc})`);
+ const videoBase62Sha1 = element.dataset.videoBase62Sha1;
+ if (videoBase62Sha1) {
+ element.replaceWith(`![|video](upload://${videoBase62Sha1})`);
+ } else {
+ // Fallback for old posts that don't contain data-video-base62-sha1
+ element.replaceWith(`![|video](${element.dataset.videoSrc})`);
+ }
});
return toMarkdown(div.outerHTML);
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index 03b7ee3cd4a..e73e0af4499 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -460,6 +460,9 @@ module PrettyText
video["data-thumbnail-src"] = UrlHelper.absolute(
GlobalPath.upload_cdn_path(thumbnail.url),
)
+ video[
+ "data-video-base62-sha1"
+ ] = "#{Upload.base62_sha1(video_sha1)}#{File.extname(video_src)}"
end
end
end
diff --git a/lib/pretty_text/helpers.rb b/lib/pretty_text/helpers.rb
index a20d77decee..7185ec27622 100644
--- a/lib/pretty_text/helpers.rb
+++ b/lib/pretty_text/helpers.rb
@@ -45,7 +45,10 @@ module PrettyText
urls.each do |url|
sha1 = Upload.sha1_from_short_url(url)
if (url.split(".")[1].nil?) # video sha1 without extension for thumbnail
- thumbnail = Upload.where("original_filename LIKE ?", "#{sha1}.%").last
+ thumbnail = Upload.where("original_filename LIKE ?", "#{sha1}.%").last if sha1
+ # Fallback for old posts that don't contain data-video-base62-sha1
+ thumbnail = Upload.where("original_filename LIKE ?", "#{url}.%").last if thumbnail.nil? &&
+ sha1.nil?
sha1 = thumbnail.sha1 if thumbnail
end
map[url] = sha1 if sha1
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
index 2dc974a3e09..819c1832188 100644
--- a/spec/lib/pretty_text_spec.rb
+++ b/spec/lib/pretty_text_spec.rb
@@ -2819,8 +2819,10 @@ HTML
doc = Nokogiri::HTML5.fragment(html)
described_class.add_video_placeholder_image(doc)
+ video_base62_sha1 = "#{Upload.base62_sha1(@video_upload.sha1)}.#{@video_upload.extension}"
+
html_with_thumbnail = <<~HTML
- <p></p><div class="video-placeholder-container" data-video-src="#{@video_upload.url}" data-thumbnail-src="http://test.localhost#{thumbnail.url}"></div><p></p>
+ <p></p><div class="video-placeholder-container" data-video-src="#{@video_upload.url}" data-thumbnail-src="http://test.localhost#{thumbnail.url}" data-video-base62-sha1="#{video_base62_sha1}"></div><p></p>
HTML
expect(doc.to_html).to eq(html_with_thumbnail)