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.
111 lines
3.3 KiB
Elixir
111 lines
3.3 KiB
Elixir
7 years ago
|
defmodule Asciinema.Asciicasts.Asciicast do
|
||
|
use Ecto.Schema
|
||
|
import Ecto.Changeset
|
||
|
alias Asciinema.Users.User
|
||
|
alias Asciinema.Asciicasts.Asciicast
|
||
7 years ago
|
alias Asciinema.PngGenerator.PngParams
|
||
|
|
||
|
@default_png_scale 2
|
||
|
@default_theme "asciinema"
|
||
7 years ago
|
|
||
|
schema "asciicasts" do
|
||
|
field :version, :integer
|
||
|
field :file, :string
|
||
7 years ago
|
field :terminal_columns, :integer
|
||
|
field :terminal_lines, :integer
|
||
7 years ago
|
field :terminal_type, :string
|
||
7 years ago
|
field :stdout_data, :string
|
||
|
field :stdout_timing, :string
|
||
7 years ago
|
field :stdout_frames, :string
|
||
7 years ago
|
field :private, :boolean
|
||
|
field :secret_token, :string
|
||
7 years ago
|
field :duration, :float
|
||
7 years ago
|
field :title, :string
|
||
7 years ago
|
field :theme_name, :string
|
||
|
field :snapshot_at, :float
|
||
7 years ago
|
field :snapshot, Asciinema.Ecto.Type.JsonArray
|
||
7 years ago
|
field :command, :string
|
||
|
field :shell, :string
|
||
|
field :uname, :string
|
||
7 years ago
|
field :user_agent, :string
|
||
7 years ago
|
|
||
7 years ago
|
timestamps(inserted_at: :created_at)
|
||
|
|
||
7 years ago
|
belongs_to :user, User
|
||
7 years ago
|
end
|
||
|
|
||
7 years ago
|
defimpl Phoenix.Param, for: Asciicast do
|
||
|
def to_param(%Asciicast{private: false, id: id}) do
|
||
|
Integer.to_string(id)
|
||
|
end
|
||
|
def to_param(%Asciicast{private: true, secret_token: secret_token}) do
|
||
|
secret_token
|
||
|
end
|
||
7 years ago
|
end
|
||
|
|
||
7 years ago
|
def changeset(struct, attrs) do
|
||
|
struct
|
||
|
|> cast(attrs, [:title, :private, :snapshot_at])
|
||
|
|> validate_required([:private])
|
||
|
end
|
||
|
|
||
7 years ago
|
def create_changeset(struct, attrs) do
|
||
|
struct
|
||
7 years ago
|
|> changeset(attrs)
|
||
|
|> cast(attrs, [:version, :file, :duration, :terminal_columns, :terminal_lines, :terminal_type, :command, :shell, :uname, :user_agent])
|
||
|
|> validate_required([:user_id, :version, :duration, :terminal_columns, :terminal_lines])
|
||
7 years ago
|
|> generate_secret_token
|
||
7 years ago
|
end
|
||
|
|
||
7 years ago
|
def update_changeset(struct, attrs) do
|
||
|
struct
|
||
7 years ago
|
|> changeset(attrs)
|
||
|
|> cast(attrs, [:theme_name])
|
||
7 years ago
|
end
|
||
|
|
||
7 years ago
|
def snapshot_changeset(struct, snapshot) do
|
||
|
cast(struct, %{snapshot: snapshot}, [:snapshot])
|
||
|
end
|
||
|
|
||
7 years ago
|
defp generate_secret_token(changeset) do
|
||
|
put_change(changeset, :secret_token, Crypto.random_token(25))
|
||
7 years ago
|
end
|
||
7 years ago
|
|
||
7 years ago
|
def json_store_path(%Asciicast{file: v} = asciicast) when is_binary(v) do
|
||
|
file_store_path(asciicast, :file)
|
||
7 years ago
|
end
|
||
7 years ago
|
def json_store_path(%Asciicast{stdout_frames: v} = asciicast) when is_binary(v) do
|
||
|
file_store_path(asciicast, :stdout_frames)
|
||
|
end
|
||
|
|
||
|
def file_store_path(%Asciicast{id: id, file: fname}, :file) do
|
||
|
file_store_path(:file, id, fname)
|
||
|
end
|
||
|
def file_store_path(%Asciicast{id: id, stdout_frames: fname}, :stdout_frames) do
|
||
|
file_store_path(:stdout_frames, id, fname)
|
||
|
end
|
||
|
def file_store_path(%Asciicast{id: id, stdout_data: fname}, :stdout_data) do
|
||
7 years ago
|
file_store_path(:stdout, id, fname)
|
||
7 years ago
|
end
|
||
|
def file_store_path(%Asciicast{id: id, stdout_timing: fname}, :stdout_timing) do
|
||
|
file_store_path(:stdout_timing, id, fname)
|
||
|
end
|
||
|
def file_store_path(type, id, fname) when is_binary(fname) do
|
||
|
"asciicast/#{type}/#{id}/#{fname}"
|
||
7 years ago
|
end
|
||
7 years ago
|
|
||
|
def snapshot_at(%Asciicast{snapshot_at: snapshot_at, duration: duration}) do
|
||
|
snapshot_at || duration / 2
|
||
|
end
|
||
|
|
||
|
def theme_name(%Asciicast{theme_name: a_theme_name}, %User{theme_name: u_theme_name}) do
|
||
|
a_theme_name || u_theme_name || @default_theme
|
||
|
end
|
||
|
|
||
|
def png_params(%Asciicast{} = asciicast, %User{} = user) do
|
||
|
%PngParams{snapshot_at: snapshot_at(asciicast),
|
||
|
theme: theme_name(asciicast, user),
|
||
|
scale: @default_png_scale}
|
||
|
end
|
||
7 years ago
|
end
|