Class: Pikuri::Agent::History::Attachment
- Inherits:
-
Data
- Object
- Data
- Pikuri::Agent::History::Attachment
- Defined in:
- lib/pikuri/agent/history.rb
Overview
A file the model was shown — an image from read, a pasted document.
Holds the actual bytes; #stored_name is what #save calls it
on disk.
Instance Attribute Summary collapse
-
#bytes ⇒ Object
readonly
Returns the value of attribute bytes.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#mime ⇒ Object
readonly
Returns the value of attribute mime.
Instance Method Summary collapse
-
#digest ⇒ String
SHA-256 of #bytes, hex.
-
#initialize(mime:, filename:, bytes:) ⇒ Attachment
constructor
A new instance of Attachment.
-
#stored_name ⇒ String
Content-addressed filename: the digest prefix makes it unique and self-verifying, so the same image read twice is stored once and a tampered file is caught at load.
Constructor Details
#initialize(mime:, filename:, bytes:) ⇒ Attachment
Returns a new instance of Attachment.
112 113 114 |
# File 'lib/pikuri/agent/history.rb', line 112 def initialize(mime:, filename:, bytes:) super(mime: mime, filename: filename, bytes: bytes.b) end |
Instance Attribute Details
#bytes ⇒ Object (readonly)
Returns the value of attribute bytes
104 105 106 |
# File 'lib/pikuri/agent/history.rb', line 104 def bytes @bytes end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename
104 105 106 |
# File 'lib/pikuri/agent/history.rb', line 104 def filename @filename end |
#mime ⇒ Object (readonly)
Returns the value of attribute mime
104 105 106 |
# File 'lib/pikuri/agent/history.rb', line 104 def mime @mime end |
Instance Method Details
#digest ⇒ String
Returns SHA-256 of #bytes, hex.
132 133 134 |
# File 'lib/pikuri/agent/history.rb', line 132 def digest Digest::SHA256.hexdigest(bytes) end |
#stored_name ⇒ String
Content-addressed filename: the digest prefix makes it unique and self-verifying, so the same image read twice is stored once and a tampered file is caught at load.
Attachment.new(mime: 'image/png', filename: 'shot.png', bytes: png).stored_name
# => "3f9a1c8e2b-shot.png"
124 125 126 127 128 129 |
# File 'lib/pikuri/agent/history.rb', line 124 def stored_name ext = File.extname(filename.to_s) stem = File.basename(filename.to_s, ext).gsub(/[^A-Za-z0-9._-]+/, '-') stem = 'attachment' if stem.empty? "#{digest[0, 10]}-#{stem}#{ext}" end |