Wraith.Uploads behaviour (Wraith v0.1.0)

View Source

Images (and other files) authors upload: feature images, inline post images, avatars. One adapter per environment (config :wraith, Wraith.Uploads, adapter: …):

  • Wraith.Uploads.Local — files under priv/uploads (dev, tests, a single container with a volume), served at /uploads/<key>
  • Wraith.Uploads.S3 — a bucket through ex_aws; the URL is the bucket's public URL or a CDN in front of it (:public_url)

Keys are <yyyy>/<mm>/<uuid>.<ext>; store/3 takes the bytes (or a path), checks the type and size (10 MB), and returns {:ok, url}.

Summary

Functions

The image library: uploaded originals (never the resized variants), newest first. limit: (default 60).

Store content (binary) under a fresh key for content_type. opts[:name] keeps a readable stem.

Store a file on disk (a LiveView upload entry's path).

Like store/3 but also returns the image's dimensions (nil for non-raster types), so a feature image's width/height can be saved with the post.

Store a file on disk and return its dimensions too.

Is this key a generated variant (not an original)?

Callbacks

delete(t)

@callback delete(String.t()) :: :ok | {:error, term()}

list(keyword)

@callback list(keyword()) :: [
  %{
    key: String.t(),
    url: String.t(),
    size: non_neg_integer(),
    modified: String.t() | nil
  }
]

put(t, binary, t)

@callback put(String.t(), binary(), String.t()) :: {:ok, String.t()} | {:error, term()}

Functions

accepted_extensions()

accepted_types()

delete(key)

impl()

list(opts \\ [])

The image library: uploaded originals (never the resized variants), newest first. limit: (default 60).

max_bytes()

store(content, content_type, opts \\ [])

@spec store(binary(), String.t(), keyword()) ::
  {:ok, String.t()} | {:error, String.t()}

Store content (binary) under a fresh key for content_type. opts[:name] keeps a readable stem.

The declared type is only trusted after the bytes agree with it: a browser, an agent or a remote server can say image/png about anything, and an SVG (or an HTML file dressed as one) served from our origin would run script with our cookies. sniff/2 checks magic bytes for every raster type and the root element for SVG; PDF must start with %PDF-.

store_file(path, content_type, opts \\ [])

Store a file on disk (a LiveView upload entry's path).

store_image(content, content_type, opts \\ [])

@spec store_image(binary(), String.t(), keyword()) ::
  {:ok, String.t(), map() | nil} | {:error, String.t()}

Like store/3 but also returns the image's dimensions (nil for non-raster types), so a feature image's width/height can be saved with the post.

store_image_file(path, content_type, opts \\ [])

Store a file on disk and return its dimensions too.

variant_key?(key)

Is this key a generated variant (not an original)?