asciinema.org/app/models/asciicast.rb

167 lines
3.9 KiB
Ruby
Raw Normal View History

2011-11-23 21:30:09 +00:00
class Asciicast < ActiveRecord::Base
2015-07-20 12:05:38 +00:00
ORDER_MODES = { date: 'created_at', popularity: 'views_count' }
mount_uploader :stdin_data, StdinDataUploader
mount_uploader :stdin_timing, StdinTimingUploader
mount_uploader :stdout_data, StdoutDataUploader
mount_uploader :stdout_timing, StdoutTimingUploader
mount_uploader :stdout_frames, StdoutFramesUploader
2015-02-27 13:58:04 +00:00
mount_uploader :file, AsciicastUploader
mount_uploader :image, ImageUploader
2014-08-30 17:38:47 +00:00
serialize :snapshot, ActiveSupportJsonProxy
2013-07-08 22:55:31 +00:00
belongs_to :user
has_many :comments, -> { order(:created_at) }, dependent: :destroy
has_many :likes, dependent: :destroy
validates :user, :duration, presence: true
2015-02-27 13:58:04 +00:00
validates :stdout_data, :stdout_timing, presence: true, unless: :file
validates :file, presence: true, unless: :stdout_data
validates :snapshot_at, numericality: { greater_than: 0, allow_blank: true }
validates :terminal_columns, presence: true, numericality: { greater_than: 0, less_than_or_equal_to: 1000 }
validates :terminal_lines, presence: true, numericality: { greater_than: 0, less_than_or_equal_to: 500 }
2012-01-14 20:26:07 +00:00
scope :featured, -> { where(featured: true) }
2015-07-20 12:05:38 +00:00
scope :by_date, -> { order("created_at DESC") }
scope :by_random, -> { order("RANDOM()") }
scope :non_private, -> { where(private: false) }
2015-07-20 12:05:38 +00:00
scope :homepage_latest, -> { non_private.by_date.limit(6).includes(:user) }
scope :homepage_featured, -> { non_private.featured.where("created_at > ?", 1.year.ago).by_random.limit(6).includes(:user) }
2012-12-09 20:22:12 +00:00
2014-11-15 17:46:46 +00:00
before_create :generate_secret_token
def self.find_by_id_or_secret_token!(thing)
if thing.size == 25
find_by_secret_token!(thing)
else
non_private.find(thing)
end
end
2012-08-22 18:00:31 +00:00
def self.cache_key
timestamps = scoped.select(:updated_at).map { |o| o.updated_at.to_i }
Digest::MD5.hexdigest timestamps.join('/')
end
def self.paginate(page, per_page)
page(page).per(per_page)
end
2014-01-17 16:21:22 +00:00
def self.for_category_ordered(category, order, page = nil, per_page = nil)
collection = self
if category == :featured
collection = collection.featured
end
2015-04-24 11:40:28 +00:00
collection = collection.order("#{ORDER_MODES.fetch(order)} DESC")
2014-01-17 16:21:22 +00:00
if page
collection = collection.paginate(page, per_page)
end
collection
end
def width
terminal_columns
end
def height
terminal_lines
end
2015-04-20 10:43:37 +00:00
def title=(value)
value ? super(value.strip[0...255]) : super
end
def command=(value)
value ? super(value.strip[0...255]) : super
end
def theme_name=(value)
if value == ""
value = nil
end
super(value)
end
2014-11-15 17:46:46 +00:00
def self.generate_secret_token
SecureRandom.hex.to_i(16).to_s(36).rjust(25, '0')
2014-11-15 17:46:46 +00:00
end
def to_param
if private?
secret_token
else
id.to_s
end
end
def data
if file.present?
file
else
stdout_frames
end
end
def data_url(options = {})
data.url(options)
end
def ready?
data_url && snapshot
end
def download_filename
"asciicast-#{id}.json"
2015-12-22 17:40:20 +00:00
end
def stdout
return @stdout if @stdout
@stdout = Stdout::Buffered.new(get_stdout)
end
def with_terminal
terminal = Terminal.new(terminal_columns, terminal_lines)
yield(terminal)
ensure
2013-09-14 11:13:58 +00:00
terminal.release if terminal
end
def theme
theme_name && Theme.for_name(theme_name)
end
def owner?(user)
user && self.user == user
end
private
def get_stdout
if version == 0
Stdout::MultiFile.new(stdout_data.decompressed_path,
stdout_timing.decompressed_path)
else
Stdout::SingleFile.new(file.absolute_url)
end
end
def image_hash
version = 2 # version of screenshot, increment to force regeneration
input = "#{version}/#{id}/#{snapshot_at}"
Digest::SHA1.hexdigest(input)
end
2014-11-15 17:46:46 +00:00
def generate_secret_token
begin
self.secret_token = self.class.generate_secret_token
end while self.class.exists?(secret_token: secret_token)
end
2011-11-23 21:30:09 +00:00
end