From 570283fba7f8fefe7205031bad751d88633bb363 Mon Sep 17 00:00:00 2001 From: rulingcom Date: Fri, 18 Jul 2025 18:40:16 +0800 Subject: [PATCH] fix looping logic --- app/models/video_image.rb | 97 ++++++++++++++------------------------- 1 file changed, 35 insertions(+), 62 deletions(-) diff --git a/app/models/video_image.rb b/app/models/video_image.rb index 2125c6d..50eea34 100644 --- a/app/models/video_image.rb +++ b/app/models/video_image.rb @@ -44,6 +44,7 @@ class VideoImage # validates :file, presence: true # validates :out_link, format: {:with=> /\A(http:\/\/|https:\/\/|\/)/i}, allow_blank: true # validates :title, presence: true + before_validation :parse_youtube_url scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)} scope :can_display, ->{where(:is_hidden.ne=>true).valid_time_range} scope :can_display_and_sorted, ->{can_display.sorted} @@ -53,56 +54,8 @@ class VideoImage scope :is_expired, ->{self.and(VideoImage.unscoped.or({:deadline.lte=>Time.now}).selector)} scope :not_expired, ->{self.and(VideoImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)} before_save do - unless @skip_callback - uri = URI.parse(self.youtube) rescue nil - self.is_youtube = (uri && uri.host == "www.youtube.com") - if self.is_youtube - if uri.path.start_with?('/embed/') - self.youtube_id = uri.path.split('/embed/').last.split(/[\/#?]/).first - if self.youtube_id.blank? - self.is_youtube = false - self.youtube_id = nil - end - else - params = CGI.parse(uri.query.to_s) - v = params.blank? ? nil : params['v'].first - if v.blank? - self.is_youtube = false - self.youtube_id = nil - else - self.youtube_id = v - end - end - # if self.is_youtube - # begin - # youtube_img_url = "https:"+self.youtube_thumb - # self.remote_file_url = youtube_img_url - # uri = URI.parse(youtube_img_url) - # req = Net::HTTP::Get.new(uri.path) - # res = Net::HTTP.start( uri.host, uri.port, - # :use_ssl => (uri.scheme == 'https'), - # :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https| - # https.request(req) - # end - # snapshot_content = res.body rescue nil #Net::HTTP.get_response(URI.parse(youtube_img_url)).body rescue nil - # if snapshot_content - # self[:video_snapshot] = '0.jpg' - # snapshot_file_path = self.video_snapshot.file.path - # FileUtils.mkdir_p(File.dirname(snapshot_file_path)) - # File.open(snapshot_file_path, 'wb'){|f| f.write(snapshot_content)} - # image = MiniMagick::Image.open(snapshot_file_path) - # self.scale = (1.0 * image[:height] / image[:width] * 100).round(2) - # else - # puts "cannot read #{youtube_img_url}" - # end - # rescue => e - # puts e.to_s - # self.scale = nil - # end - # else - # self.scale = nil - # end - else + unless Thread.current[:skip_video_callbacks] + if self.is_youtube == false self.youtube_id = nil image = MiniMagick::Image.open(self.video_snapshot.file.path) rescue nil if image @@ -119,7 +72,7 @@ class VideoImage VideoProSetting.first.remove_user_id(self) end after_save do - unless @skip_callback + unless Thread.current[:skip_video_callbacks] if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video Thread.new do self.generate_webm @@ -130,13 +83,6 @@ class VideoImage self.generate_video_snapshot end end - if self.is_youtube - Thread.new do - youtube_img_url = "https:"+self.youtube_thumb - self.remote_file_url = youtube_img_url - self.save! - end - end end end def inc_count @@ -181,9 +127,9 @@ class VideoImage self[:video_snapshot] = File.basename(self.video_file.file.path).split(/\.[^.]+$/)[0] + ".jpg" FileUtils.mkdir_p(File.dirname(self.video_snapshot.file.path)) system("tmp/ffmpeg/ffmpeg -i #{self.video_file.file.path} -vframes 1 #{self.video_snapshot.file.path}") - @skip_callback = true + Thread.current[:skip_video_callbacks] = true self.save - @skip_callback = false + Thread.current[:skip_video_callbacks] = false true end end @@ -195,9 +141,9 @@ class VideoImage flag = system("tmp/ffmpeg/ffmpeg -i #{video_path} -c:v libvpx-vp9 -crf 35 -b:v 0 -b:a 96k -c:a libopus -filter:v fps=20 #{video_webm} -cpu-used #{core_num}") if flag self.video_file_webm = File.open(video_webm) - @skip_callback = true + Thread.current[:skip_video_callbacks] = true self.save - @skip_callback = false + Thread.current[:skip_video_callbacks] = false true else puts "generate webm failed" @@ -266,4 +212,31 @@ class VideoImage # self.out_link = 'https://' + self.out_link # end end + + private + def parse_youtube_url + return if youtube.blank? + return if self.youtube_changed? == false + uri = URI.parse(youtube) rescue nil + if uri&.host == "www.youtube.com" + self.is_youtube = true + if uri.path.start_with?('/embed/') + self.youtube_id = uri.path.split('/embed/').last.split(/[\/#?]/).first + else + params = CGI.parse(uri.query.to_s) + self.youtube_id = params['v']&.first + end + self.is_youtube = false if youtube_id.blank? + else + self.is_youtube = false + self.youtube_id = nil + end + if self.is_youtube + youtube_img_url = "https:"+self.youtube_thumb + self.remote_file_url = youtube_img_url + end + rescue URI::InvalidURIError => e + self.is_youtube = false + self.youtube_id = nil + end end