From 30048ab97f8c30a3334e13f882fd5b621ba4b67d Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Wed, 31 Jul 2024 10:54:52 -0600 Subject: [PATCH] FIX: Video thumbnails for missing videos (#28152) Skip trying to find a thumbnail if the video src cannot be found. Bug report: https://meta.discourse.org/t/317423 --- lib/pretty_text.rb | 1 + spec/lib/pretty_text_spec.rb | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index 57968978a46..da00e5888c7 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -452,6 +452,7 @@ module PrettyText .css(".video-placeholder-container") .each do |video| video_src = video["data-video-src"] + next if video_src == "/404" || video_src.nil? video_sha1 = File.basename(video_src, File.extname(video_src)) thumbnail = Upload.where("original_filename LIKE ?", "#{video_sha1}.%").last if thumbnail diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb index 123c5c3bfe7..96b505becc4 100644 --- a/spec/lib/pretty_text_spec.rb +++ b/spec/lib/pretty_text_spec.rb @@ -2794,4 +2794,42 @@ HTML require("discourse-common/lib/deprecated").default("Some deprecation message"); JS end + + describe "video thumbnails" do + before do + SiteSetting.authorized_extensions = "mp4|png" + @video_upload = Fabricate(:upload, original_filename: "video.mp4", extension: "mp4") + end + + after { Upload.where(original_filename: ["404.png", "#{@video_upload.sha1}.png"]).destroy_all } + + it "does not link to a thumbnail image if the video source is missing" do + Fabricate(:upload, original_filename: "404.png", extension: "png") + + html = <<~HTML +

+ HTML + doc = Nokogiri::HTML5.fragment(html) + described_class.add_video_placeholder_image(doc) + + expect(doc.to_html).to eq(html) + end + + it "links to a thumbnail image if the video source is valid" do + thumbnail = + Fabricate(:upload, original_filename: "#{@video_upload.sha1}.png", extension: "png") + + html = <<~HTML +

+ HTML + doc = Nokogiri::HTML5.fragment(html) + described_class.add_video_placeholder_image(doc) + + html_with_thumbnail = <<~HTML +

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