Coelho.Attachment (coelho v0.2.0)

Copy Markdown View Source

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

t()

@type t() :: %Coelho.Attachment{
  __meta__: term(),
  byte_size: term(),
  checksum: term(),
  content_type: term(),
  filename: term(),
  id: term(),
  inserted_at: term(),
  key: term(),
  metadata: term(),
  updated_at: term()
}

Functions

changeset(attachment, attrs)

@spec changeset(t(), map()) :: Ecto.Changeset.t()

Casts and validates attachment metadata.

generate_key()

@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.

to_node(attachment)

@spec to_node(t()) :: map()

The document node referencing this attachment.

Insert the result into the editor once an upload has been consumed.