From dfb64f9b8420f31622a9b7fd7c9f09f3b4a37e4a Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Thu, 30 Jan 2025 17:54:50 -0700 Subject: [PATCH] FIX: Quoting videos can show a corrupted thumbnail (#31079) This change ensures we use the base62 sha1 for videos when quoting because this is what the composer is used to using. With a valid base62 sha1 the composer already knows how to fetch the placeholder image for it. Fallbacks have been created to continue to support the old way as well as a fix for the old way so that the thumbnail continues to display when quoting. These fallbacks are in place so that we don't have to rebake all posts that contain videos. If we ever do that we may remove these fallbacks. --- app/assets/javascripts/discourse/app/lib/utilities.js | 8 +++++++- lib/pretty_text.rb | 3 +++ lib/pretty_text/helpers.rb | 5 ++++- spec/lib/pretty_text_spec.rb | 4 +++- 4 files changed, 17 insertions(+), 3 deletions(-) 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 -

+

HTML expect(doc.to_html).to eq(html_with_thumbnail)