You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
Ruby
63 lines
1.6 KiB
Ruby
class Asciicast < ActiveRecord::Base
|
|
mount_uploader :stdin, BasicUploader
|
|
mount_uploader :stdin_timing, BasicUploader
|
|
mount_uploader :stdout, BasicUploader
|
|
mount_uploader :stdout_timing, BasicUploader
|
|
|
|
validates :stdout, :stdout_timing, :presence => true
|
|
validates :terminal_columns, :terminal_lines, :duration, :presence => true
|
|
|
|
belongs_to :user
|
|
|
|
before_create :assign_user, :unless => :user
|
|
|
|
attr_reader :description
|
|
|
|
def meta=(file)
|
|
data = JSON.parse(file.tempfile.read)
|
|
|
|
self.user_token = data['user_token']
|
|
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
|
|
|
|
def as_json(opts = {})
|
|
super :methods => [:escaped_stdout_data, :stdout_timing_data]
|
|
end
|
|
|
|
def escaped_stdout_data
|
|
if data = stdout.read
|
|
data.bytes.map { |b| '\x' + format('%02x', b) }.join
|
|
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
|
|
|
|
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
|
|
end
|