97 lines
3.0 KiB
Ruby
97 lines
3.0 KiB
Ruby
require 'uri'
|
|
|
|
class AdImage
|
|
include Mongoid::Document
|
|
include Mongoid::Timestamps
|
|
|
|
mount_uploader :file, BannerImageUploader
|
|
mount_uploader :video_snapshot, BannerImageUploader
|
|
mount_uploader :video_file, BannerUploader
|
|
mount_uploader :video_file_webm, BannerUploader
|
|
|
|
field :auto_convert_video, type: Boolean, default: false
|
|
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
|
|
field :youtube , :type => String
|
|
field :sort_number, :type => Integer
|
|
field :language_enabled, :type => Array, :default => ["en","zh_tw"]
|
|
field :exchange_item, :default => "1"
|
|
LINK_OPEN_TYPES = ["local", "new_window"]
|
|
|
|
belongs_to :banner
|
|
|
|
before_validation :add_http
|
|
# validates :file, presence: true
|
|
validates :out_link, format: {:with=> /\A(http:\/\/|https:\/\/|\/)/i}, allow_blank: true
|
|
# validates :title, presence: true
|
|
|
|
scope :can_display, ->{self.and(AdImage.or({:postdate.lte=>Time.now},{:postdate=>nil}).selector,AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
|
|
scope :is_expired, ->{self.and(AdImage.or({:deadline.lte=>Time.now}).selector)}
|
|
scope :not_expired, ->{self.and(AdImage.or({:deadline.gte=>Time.now},{:deadline=>nil}).selector)}
|
|
|
|
after_save do
|
|
if (self.video_file_changed? || self.auto_convert_video_changed?) && self.auto_convert_video
|
|
Thread.new do
|
|
self.generate_webm
|
|
end
|
|
end
|
|
end
|
|
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)
|
|
self.save
|
|
else
|
|
puts "generate webm failed"
|
|
end
|
|
end
|
|
end
|
|
|
|
def expired?
|
|
self.deadline<Time.now rescue false
|
|
end
|
|
|
|
def embed_url
|
|
uri = URI.parse(self.youtube) rescue nil
|
|
if !uri.nil? && uri.host == "www.youtube.com"
|
|
params = CGI.parse(uri.query)
|
|
v = params['v'].first
|
|
return "//www.youtube.com/embed/#{v}"
|
|
else
|
|
return ""
|
|
end
|
|
end
|
|
|
|
def youtube_thumb
|
|
uri = URI.parse(self.youtube) rescue nil
|
|
if !uri.nil? && uri.host == "www.youtube.com"
|
|
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
|
|
else
|
|
return ""
|
|
end
|
|
end
|
|
|
|
protected
|
|
|
|
def add_http
|
|
unless self.out_link.blank? || self.out_link[/^http:\/\//] || self.out_link[/^https:\/\//] || self.out_link[/^\//]
|
|
self.out_link = 'http://' + self.out_link
|
|
end
|
|
end
|
|
end
|