Class: ActionAgent::RecordingSnapshot

Inherits:
ApplicationRecord show all
Defined in:
app/models/action_agent/recording_snapshot.rb

Constant Summary collapse

SNAPSHOT_TYPES =
%w[screenshot dom full_page].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

for_owner, owner_association

Class Method Details

.attachments_available?Boolean

Whether file attachment is available in this host app.

Returns:

  • (Boolean)


15
16
17
# File 'app/models/action_agent/recording_snapshot.rb', line 15

def self.attachments_available?
  defined?(ActiveStorage) && method_defined?(:file)
end

Instance Method Details

#as_json_for_apiObject

For API response



57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/action_agent/recording_snapshot.rb', line 57

def as_json_for_api
  {
    id: id,
    storage_key: storage_key,
    snapshot_type: snapshot_type,
    width: width,
    height: height,
    file_size_bytes: file_size_bytes,
    url: signed_url,
    created_at: created_at.iso8601
  }
end

#signed_url(expires_in: 15.minutes) ⇒ Object

Get a signed URL for the file



29
30
31
32
33
34
35
36
37
# File 'app/models/action_agent/recording_snapshot.rb', line 29

def signed_url(expires_in: 15.minutes)
  return nil unless self.class.attachments_available?
  return nil unless file.attached?

  file.url(expires_in: expires_in)
rescue StandardError
  # Fallback if storage not configured
  nil
end

#store!(data, filename: nil, content_type: nil) ⇒ Object

Store file data. Without Active Storage the row still records the snapshot's metadata; only the blob is skipped.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/action_agent/recording_snapshot.rb', line 41

def store!(data, filename: nil, content_type: nil)
  content_type ||= infer_content_type
  filename ||= generate_filename

  if self.class.attachments_available?
    file.attach(
      io: StringIO.new(data),
      filename: filename,
      content_type: content_type
    )
  end

  update!(file_size_bytes: data.bytesize)
end