2011-11-23 21:30:09 +00:00
|
|
|
class Asciicast < ActiveRecord::Base
|
2011-11-23 21:44:00 +00:00
|
|
|
mount_uploader :stdin, BasicUploader
|
|
|
|
mount_uploader :stdin_timing, BasicUploader
|
|
|
|
mount_uploader :stdout, BasicUploader
|
|
|
|
mount_uploader :stdout_timing, BasicUploader
|
2011-11-23 22:22:14 +00:00
|
|
|
|
|
|
|
validates :stdout, :stdout_timing, :presence => true
|
|
|
|
validates :terminal_columns, :terminal_lines, :duration, :presence => true
|
2012-01-14 20:26:07 +00:00
|
|
|
|
2012-03-04 17:14:58 +00:00
|
|
|
belongs_to :user
|
2012-03-04 20:55:45 +00:00
|
|
|
has_many :comments, :order => :created_at, :dependent => :destroy
|
2012-03-03 16:38:29 +00:00
|
|
|
|
2012-03-11 14:29:31 +00:00
|
|
|
scope :featured, where(:featured => true)
|
|
|
|
|
2012-03-04 17:14:58 +00:00
|
|
|
before_create :assign_user, :unless => :user
|
|
|
|
|
2012-04-03 18:47:59 +00:00
|
|
|
attr_accessible :meta, :stdout, :stdout_timing, :stdin, :stdin_timing,
|
|
|
|
:title, :description
|
2012-03-04 14:18:09 +00:00
|
|
|
|
2012-03-04 19:28:30 +00:00
|
|
|
def self.assign_user(user_token, user)
|
|
|
|
where(:user_id => nil, :user_token => user_token).
|
|
|
|
update_all(:user_id => user.id, :user_token => nil)
|
|
|
|
end
|
|
|
|
|
2012-01-14 20:26:07 +00:00
|
|
|
def meta=(file)
|
|
|
|
data = JSON.parse(file.tempfile.read)
|
|
|
|
|
2012-04-03 17:06:05 +00:00
|
|
|
self.username = data['username']
|
2012-03-04 17:14:58 +00:00
|
|
|
self.user_token = data['user_token']
|
2012-01-14 20:26:07 +00:00
|
|
|
self.duration = data['duration']
|
|
|
|
self.recorded_at = data['recorded_at']
|
|
|
|
self.title = data['title']
|
|
|
|
self.command = data['command']
|
|
|
|
self.shell = data['shell']
|
|
|
|
self.uname = data['uname']
|
|
|
|
self.terminal_lines = data['term']['lines']
|
|
|
|
self.terminal_columns = data['term']['columns']
|
|
|
|
self.terminal_type = data['term']['type']
|
|
|
|
end
|
2012-03-03 17:38:11 +00:00
|
|
|
|
2012-04-04 16:41:17 +00:00
|
|
|
def os
|
|
|
|
if uname =~ /Linux/
|
|
|
|
'Linux'
|
|
|
|
elsif uname =~ /Darwin/
|
|
|
|
'OSX'
|
|
|
|
else
|
|
|
|
uname.split(' ', 2)[0]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def shell_name
|
|
|
|
File.basename(shell.to_s)
|
|
|
|
end
|
|
|
|
|
2012-03-03 17:38:11 +00:00
|
|
|
def as_json(opts = {})
|
|
|
|
super :methods => [:escaped_stdout_data, :stdout_timing_data]
|
|
|
|
end
|
|
|
|
|
|
|
|
def escaped_stdout_data
|
|
|
|
if data = stdout.read
|
2012-03-03 18:43:38 +00:00
|
|
|
data.bytes.map { |b| '\x' + format('%02x', b) }.join
|
2012-03-03 17:38:11 +00:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def stdout_timing_data
|
|
|
|
if data = stdout_timing.read
|
|
|
|
Bzip2.uncompress(data).lines.map do |line|
|
|
|
|
delay, n = line.split
|
|
|
|
[delay.to_f, n.to_i]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2012-03-04 17:14:58 +00:00
|
|
|
|
|
|
|
def assign_user
|
|
|
|
if user_token.present?
|
|
|
|
if ut = UserToken.find_by_token(user_token)
|
|
|
|
self.user = ut.user
|
|
|
|
self.user_token = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-11-23 21:30:09 +00:00
|
|
|
end
|