jam-cloud/ruby/lib/jam_ruby/models/icecast_mount.rb

121 lines
5.3 KiB
Ruby

module JamRuby
class IcecastMount < ActiveRecord::Base
attr_accessible :authentication_id, :name, :source_username, :source_pass, :max_listeners, :max_listener_duration,
:dump_file, :intro, :fallback_mount, :fallback_override, :fallback_when_full, :charset, :is_public,
:stream_name, :stream_description, :stream_url, :genre, :bitrate, :mime_type, :subtype, :burst_size,
:mp3_metadata_interval, :hidden, :on_connect, :on_disconnect, as: :admin
belongs_to :authentication, class_name: "JamRuby::IcecastUserAuthentication", inverse_of: :mount, :foreign_key => 'authentication_id'
has_many :server_mounts, :class_name => "JamRuby::IcecastServerMount", :inverse_of => :mount, :foreign_key => 'icecast_mount_id'
has_many :servers, :class_name => "JamRuby::IcecastServer", :through => :server_mounts, :source => :server
validates :name, presence: true
validates :source_username, length: {minimum: 5}, if: lambda {|m| m.source_username.present?}
validates :source_pass, length: {minimum: 5}, if: lambda {|m| m.source_pass.present?}
validates :max_listeners, length: {in: 1..15000}, if: lambda {|m| m.max_listeners.present?}
validates :max_listener_duration, length: {in: 1..3600 * 48}, if: lambda {|m| m.max_listener_duration.present?}
validates :fallback_override, :inclusion => {:in => [0, 1]} , if: lambda {|m| m.fallback_mount.present?}
validates :fallback_when_full, :inclusion => {:in => [0, 1]} , if: lambda {|m| m.fallback_mount.present?}
validates :is_public, presence: true, :inclusion => {:in => [-1, 0, 1]}
validates :stream_name, presence: true
validates :stream_description, presence: true
validates :stream_url, presence: true
validates :genre, presence: true
validates :bitrate, numericality: {only_integer: true}, if: lambda {|m| m.bitrate.present?}
validates :mime_type, presence: true
validates :subtype, presence: true
validates :burst_size, numericality: {only_integer: true}, if: lambda {|m| m.burst_size.present?}
validates :mp3_metadata_interval, numericality: {only_integer: true}, if: lambda {|m| m.mp3_metadata_interval.present?}
validates :hidden, :inclusion => {:in => [0, 1]}
validate :name_has_correct_format
before_save :sanitize_active_admin
after_save :after_save
after_commit :after_commit
def name_has_correct_format
errors.add(:name, "must start with /") unless name && name.start_with?('/')
end
def after_save
IcecastServer.update(servers, config_changed: 1)
# transiting to sourced from not sourced
if !sourced_was && sourced
end
end
def sanitize_active_admin
self.authentication_id = nil if self.authentication_id == ''
end
def source_up
with_lock do
self.sourced = true
self.save(:validate => false)
end
end
def source_down
with_lock do
sourced = false
save(:validate => false)
end
end
def listener_add
with_lock do
increment!(:listeners)
end
end
def listener_remove
with_lock do
decrement!(:listeners)
end
end
def dumpXml(builder)
builder.tag! 'mount' do |mount|
mount.tag! 'mount-name', name
mount.tag! 'username', source_username if !source_username.nil? && !source_username.empty?
mount.tag! 'password', source_pass if !source_pass.nil? && !source_pass.empty?
mount.tag! 'max-listeners', max_listeners unless max_listeners.nil?
mount.tag! 'max-listener-duration', max_listener_duration unless max_listener_duration.nil?
mount.tag! 'dump-file', dump_file if !dump_file.nil? && !dump_file.empty?
mount.tag! 'intro', intro if !intro.nil? && !intro.empty?
mount.tag! 'fallback-mount', fallback_mount if !fallback_mount.nil? && !fallback_mount.empty?
mount.tag! 'fallback-override', fallback_override if fallback_override
mount.tag! 'fallback-when-full', fallback_when_full if fallback_when_full
mount.tag! 'charset', charset if charset
mount.tag! 'public', is_public
mount.tag! 'stream-name', stream_name if !stream_name.nil? && !stream_name.empty?
mount.tag! 'stream-description', stream_description if !stream_description.nil? && !stream_description.empty?
mount.tag! 'stream-url', stream_url if !stream_url.nil? && !stream_url.empty?
mount.tag! 'genre', genre unless genre.empty?
mount.tag! 'bitrate', bitrate if bitrate
mount.tag! 'type', mime_type
mount.tag! 'subtype', subtype
mount.tag! 'burst-size', burst_size if burst_size
mount.tag! 'mp3-metadata-interval', mp3_metadata_interval unless mp3_metadata_interval.nil?
mount.tag! 'hidden', hidden
mount.tag! 'on-connect', on_connect if on_connect
mount.tag! 'on-disconnect', on_disconnect if on_disconnect
authentication.dumpXml(builder) if authentication
end
end
def get_media_url
raise "Unassociated server to mount" if self.server_mount.nil?
"http://" + server_mount.server.hostname + self.name
end
end
end