Cyclium.Exports (Cyclium v0.1.14)

Copy Markdown View Source

Durable, downloadable artifacts an actor produces for a principal (e.g. a CSV). An export is a blob persisted to cyclium_exports, fetched on demand via a signed, expiring link — scoped to the principal that requested it.

This is the framework half; a web app mounts a small controller that verifies the token (valid_token?/2), loads the export (fetch_valid/1), checks the caller owns it, and streams the content. Any actor in any app can create exports (see Cyclium.Tools.CsvExport).

Configuration

config :cyclium, :export_signing_secret, "<a long random secret>"
config :cyclium, :export_ttl_seconds, 604_800  # optional, default 7 days

The token is an HMAC of the export id (no external signing dep); the TTL is enforced server-side via expires_at, so the token itself carries no expiry.

Summary

Functions

Create an export. Required: :principal_type, :principal_id, :filename, :content. Optional: :type (default "csv"), :content_type, :episode_id, :conversation_id, :ttl_seconds. Stamps created_at, expires_at, and byte_size.

Fetch an export by id only if it exists and has not expired.

Delete all expired exports. Returns the number deleted. Schedule periodically.

An opaque HMAC token binding a download link to one export id.

Constant-time check that token was issued by sign/1 for id.

Functions

create(attrs)

@spec create(map()) ::
  {:ok, Cyclium.Schemas.Export.t()} | {:error, :too_large | Ecto.Changeset.t()}

Create an export. Required: :principal_type, :principal_id, :filename, :content. Optional: :type (default "csv"), :content_type, :episode_id, :conversation_id, :ttl_seconds. Stamps created_at, expires_at, and byte_size.

Content larger than :export_max_bytes (config; default 5 MB) is rejected with {:error, :too_large} — the content lives inline in the DB, so this guards against a runaway export bloating the table. Raise the limit or add GCS offload when genuinely-large exports are needed.

fetch_valid(id)

@spec fetch_valid(binary()) :: Cyclium.Schemas.Export.t() | nil

Fetch an export by id only if it exists and has not expired.

purge_expired()

@spec purge_expired() :: non_neg_integer()

Delete all expired exports. Returns the number deleted. Schedule periodically.

sign(id)

@spec sign(binary()) :: binary()

An opaque HMAC token binding a download link to one export id.

valid_token?(id, token)

@spec valid_token?(binary(), binary()) :: boolean()

Constant-time check that token was issued by sign/1 for id.