adbanner-test/app/models/ad_image.rb

133 lines
4.2 KiB
Ruby
Raw Normal View History

2014-04-11 08:55:33 +00:00
require 'uri'
2014-04-11 07:47:33 +00:00
class AdImage
include Mongoid::Document
include Mongoid::Timestamps
2022-01-03 14:23:32 +00:00
mount_uploader :file, BannerImageUploader
2022-01-05 07:52:40 +00:00
mount_uploader :video_snapshot, BannerImageUploader
2022-01-03 09:33:54 +00:00
mount_uploader :video_file, BannerUploader
mount_uploader :video_file_webm, BannerUploader
2022-08-21 08:43:10 +00:00
field :default_video_snapshot, type: Boolean, default: false
2021-10-22 15:31:10 +00:00
field :auto_convert_video, type: Boolean, default: false
2014-04-11 07:47:33 +00:00
field :title, type: String, localize: true
field :context, type: String, localize: true
field :weight, type: Integer, default: 1
field :out_link, type: String
field :link_open, type: String
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
2021-04-10 09:12:40 +00:00
field :youtube , :type => String
2016-02-16 12:21:14 +00:00
field :sort_number, :type => Integer
2016-05-18 08:29:39 +00:00
field :language_enabled, :type => Array, :default => ["en","zh_tw"]
2021-04-10 09:12:40 +00:00
field :exchange_item, :default => "1"
2014-04-11 07:47:33 +00:00
LINK_OPEN_TYPES = ["local", "new_window"]
2014-04-11 08:26:34 +00:00
belongs_to :banner
2014-04-11 07:47:33 +00:00
2022-11-19 02:57:29 +00:00
before_save :add_http
2021-04-10 09:12:40 +00:00
# validates :file, presence: true
2022-11-19 02:57:29 +00:00
# validates :out_link, format: {:with=> /\A(http:\/\/|https:\/\/|\/)/i}, allow_blank: true
2014-08-14 11:54:21 +00:00
# validates :title, presence: true
2022-08-04 08:43:30 +00:00
scope :can_display, ->{self.and(AdImage.unscoped.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
scope :is_expired, ->{self.and(AdImage.unscoped.or({:deadline.lte=>Time.now}).selector)}
scope :not_expired, ->{self.and(AdImage.unscoped.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
2022-08-21 08:43:10 +00:00
before_save do
unless @skip_callback
if self.video_snapshot_changed?
self.default_video_snapshot = false
end
end
end
2021-10-22 15:31:10 +00:00
after_save do
2022-08-21 08:43:10 +00:00
unless @skip_callback
if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video
Thread.new do
self.generate_webm
end
end
if self.video_file? && self.video_file_changed? && (!(self.video_snapshot?) || self.default_video_snapshot)
Thread.new do
self.generate_video_snapshot
end
2021-10-22 15:31:10 +00:00
end
end
end
2022-08-21 08:43:10 +00:00
def generate_video_snapshot
if self.video_file?
self.default_video_snapshot = true
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
self.save
@skip_callback = false
true
end
end
2021-10-22 15:31:10 +00:00
def generate_webm
video_path = self.video_file.path rescue nil
if !video_path.blank?
video_webm = video_path.split('.')[0...-1].join('.')+".webm"
core_num = [`cat /proc/cpuinfo | grep processor | wc -l`.to_i/2,1].max
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)
2022-08-21 08:43:10 +00:00
@skip_callback = true
2021-10-22 15:31:10 +00:00
self.save
2022-08-21 08:43:10 +00:00
@skip_callback = false
true
2021-10-22 15:31:10 +00:00
else
puts "generate webm failed"
end
end
end
2014-08-14 11:54:21 +00:00
def expired?
self.deadline<Time.now rescue false
end
2014-04-11 08:55:33 +00:00
2021-04-10 09:12:40 +00:00
def embed_url
uri = URI.parse(self.youtube) rescue nil
if !uri.nil? && uri.host == "www.youtube.com"
2022-09-12 14:41:44 +00:00
if uri.path.start_with?('/embed/')
return self.youtube.sub(/http(s|):\/\//,'//')
else
params = CGI.parse(uri.query.to_s)
v = params.blank? ? nil : params['v'].first
if v.blank?
return ""
else
return "//www.youtube.com/embed/#{v}"
end
end
2021-04-10 09:12:40 +00:00
else
return ""
end
end
def youtube_thumb
uri = URI.parse(self.youtube) rescue nil
if !uri.nil? && uri.host == "www.youtube.com"
2021-10-25 03:03:51 +00:00
params = CGI.parse(uri.query) rescue {}
if params.has_key?('v')
v = params['v'].first
return "http://img.youtube.com/vi/#{v}/0.jpg"
else
return ""
end
2021-04-10 09:12:40 +00:00
else
return ""
end
end
2014-04-11 08:55:33 +00:00
protected
2014-04-11 07:47:33 +00:00
2014-04-11 08:55:33 +00:00
def add_http
2022-01-24 01:42:55 +00:00
unless self.out_link.blank? || self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//] || self.out_link[/^\//]
2022-11-19 02:57:29 +00:00
self.out_link = 'https://' + self.out_link
2014-04-11 08:55:33 +00:00
end
end
2014-04-11 07:47:33 +00:00
end