2013-09-25 17:29:57 +00:00
|
|
|
class AsciicastParams
|
2015-03-23 16:40:11 +00:00
|
|
|
FormatError = Class.new(StandardError)
|
2013-09-25 17:29:57 +00:00
|
|
|
|
2015-02-27 14:38:18 +00:00
|
|
|
def self.build(asciicast_params, username, token, user_agent)
|
|
|
|
if asciicast_params.try(:respond_to?, :read)
|
|
|
|
from_format_1_request(asciicast_params, username, token, user_agent)
|
|
|
|
else
|
|
|
|
from_format_0_request(asciicast_params, username, token, user_agent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.from_format_0_request(params, username, token, user_agent)
|
|
|
|
meta = JSON.parse(params.delete(:meta).read)
|
|
|
|
token ||= meta.delete('user_token')
|
|
|
|
username ||= meta.delete('username')
|
2013-09-25 17:29:57 +00:00
|
|
|
|
|
|
|
attributes = {
|
2014-02-09 13:58:17 +00:00
|
|
|
command: meta['command'],
|
2014-02-11 16:49:22 +00:00
|
|
|
duration: meta['duration'],
|
2014-02-09 13:58:17 +00:00
|
|
|
shell: meta['shell'],
|
2014-02-11 16:49:22 +00:00
|
|
|
stdin_data: params[:stdin],
|
|
|
|
stdin_timing: params[:stdin_timing],
|
|
|
|
stdout_data: params[:stdout],
|
|
|
|
stdout_timing: params[:stdout_timing],
|
2014-02-09 13:58:17 +00:00
|
|
|
terminal_columns: meta['term']['columns'],
|
2014-02-11 16:49:22 +00:00
|
|
|
terminal_lines: meta['term']['lines'],
|
2014-02-09 13:58:17 +00:00
|
|
|
terminal_type: meta['term']['type'],
|
2014-02-11 16:49:22 +00:00
|
|
|
title: meta['title'],
|
2015-02-27 14:38:18 +00:00
|
|
|
user: User.for_api_token!(token, username),
|
|
|
|
version: 0,
|
2013-09-25 17:29:57 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 13:58:04 +00:00
|
|
|
if meta['uname'] # old client, with useless user_agent
|
2013-10-13 10:29:30 +00:00
|
|
|
attributes[:uname] = meta['uname']
|
|
|
|
else
|
2014-02-10 16:39:37 +00:00
|
|
|
attributes[:user_agent] = user_agent
|
2013-10-13 10:29:30 +00:00
|
|
|
end
|
|
|
|
|
2014-02-10 16:39:37 +00:00
|
|
|
attributes
|
2013-09-25 17:29:57 +00:00
|
|
|
end
|
|
|
|
|
2015-02-27 14:38:18 +00:00
|
|
|
def self.from_format_1_request(asciicast_file, username, token, user_agent)
|
2015-03-23 16:40:11 +00:00
|
|
|
begin
|
|
|
|
asciicast = Oj.sc_parse(AsciicastHandler.new, asciicast_file)
|
|
|
|
rescue Oj::ParseError
|
|
|
|
raise FormatError, "This doesn't look like a valid asciicast file"
|
|
|
|
end
|
|
|
|
|
2015-02-27 15:44:15 +00:00
|
|
|
version = asciicast['version']
|
|
|
|
|
|
|
|
if version != 1
|
2015-03-23 16:40:11 +00:00
|
|
|
raise FormatError, "Unsupported asciicast format version: #{version}"
|
2015-02-27 15:44:15 +00:00
|
|
|
end
|
|
|
|
|
2015-02-27 13:58:04 +00:00
|
|
|
env = asciicast['env']
|
|
|
|
|
|
|
|
{
|
|
|
|
command: asciicast['command'],
|
2015-02-27 14:38:18 +00:00
|
|
|
duration: asciicast['duration'],
|
|
|
|
file: asciicast_file,
|
2015-02-27 13:58:04 +00:00
|
|
|
shell: env && env['SHELL'],
|
2015-02-27 14:38:18 +00:00
|
|
|
terminal_columns: asciicast['width'],
|
|
|
|
terminal_lines: asciicast['height'],
|
2015-02-27 13:58:04 +00:00
|
|
|
terminal_type: env && env['TERM'],
|
2015-02-27 14:38:18 +00:00
|
|
|
title: asciicast['title'],
|
|
|
|
user: User.for_api_token!(token, username),
|
2015-02-27 13:58:04 +00:00
|
|
|
user_agent: user_agent,
|
2015-02-27 15:44:15 +00:00
|
|
|
version: version,
|
2015-02-27 13:58:04 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
class AsciicastHandler < ::Oj::ScHandler
|
2015-02-27 15:44:15 +00:00
|
|
|
META_ATTRIBUTES = %w[version width height duration command title env SHELL TERM]
|
2015-02-27 13:58:04 +00:00
|
|
|
|
|
|
|
def hash_start
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash_set(h, k, v)
|
|
|
|
if META_ATTRIBUTES.include?(k)
|
|
|
|
h[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-25 17:29:57 +00:00
|
|
|
end
|