Metadata of one uploaded file, addressed by key.
Coelho records what the editor and the renderer need to show an
attachment — key, filename, content type, size — and nothing about where
the bytes are. Storage stays the application's: this row is what a
resolver looks up to build a URL. See Coelho.Attachments.
Create the table with mix coelho.gen.migration.
{:ok, attachment} =
%Coelho.Attachment{}
|> Coelho.Attachment.changeset(%{
key: Coelho.Attachment.generate_key(),
filename: "plan.pdf",
content_type: "application/pdf",
byte_size: 91_233
})
|> Repo.insert()
Coelho.Attachment.to_node(attachment)
#=> %{"type" => "attachment", "attrs" => %{"key" => "...", ...}}Nothing here is tied to an owner. A document references attachments by
key, and Coelho.Attachments.keys/2 answers which keys a document still
uses — which is what a cleanup job needs, and is why the row carries no
polymorphic association.
Summary
Functions
Casts and validates attachment metadata.
A fresh key.
The document node referencing this attachment.
Types
Functions
@spec changeset(t(), map()) :: Ecto.Changeset.t()
Casts and validates attachment metadata.
@spec generate_key() :: String.t()
A fresh key.
Keys are opaque and URL safe, and deliberately not derived from the filename: the key is what ends up inside stored documents, so it must survive a rename and must not leak what it points at.
The document node referencing this attachment.
Insert the result into the editor once an upload has been consumed.