PhxMediaLibrary.Collection (PhxMediaLibrary v0.8.0)

Copy Markdown View Source

Represents a media collection configuration.

Collections group related media and can have specific settings like allowed file types, storage disk, file limits, and size constraints.

Fields

  • :name — the collection name atom (e.g. :images, :documents)
  • :disk — storage disk override for this collection
  • :accepts — list of accepted MIME types (e.g. ["image/jpeg", "image/png"])
  • :single_file — when true, only one file is kept in this collection (default: false)
  • :max_files — maximum number of files allowed in the collection
  • :max_size — maximum file size in bytes (e.g. 10_000_000 for 10 MB)
  • :fallback_url — URL to use when the collection is empty
  • :fallback_path — filesystem path to use when the collection is empty
  • :verify_content_type — when true, verify file content matches its declared MIME type (default: true)
  • :webp — WebP conversion override for this collection: true/false or a keyword of knobs (quality:, keep_original:) deep-merged over the global config :phx_media_library, :webp. nil (default) inherits the global.
  • :responsive — responsive-images override, same shape (true/false/keyword with widths:), merged over the global config :phx_media_library, :responsive.

Summary

Functions

Create a new collection configuration.

Types

override()

@type override() :: boolean() | keyword() | nil

t()

@type t() :: %PhxMediaLibrary.Collection{
  accepts: [String.t()] | nil,
  disk: atom() | nil,
  fallback_path: String.t() | nil,
  fallback_url: String.t() | nil,
  max_files: pos_integer() | nil,
  max_size: pos_integer() | nil,
  name: atom(),
  responsive: override(),
  single_file: boolean(),
  verify_content_type: boolean(),
  webp: override()
}

Functions

new(name, opts \\ [])

@spec new(
  atom(),
  keyword()
) :: t()

Create a new collection configuration.

Options

  • :disk - Storage disk to use
  • :accepts - List of accepted MIME types
  • :single_file - Only keep one file (default: false)
  • :max_files - Maximum number of files
  • :max_size - Maximum file size in bytes (e.g. 10_000_000 for 10 MB)
  • :fallback_url - URL when collection is empty
  • :fallback_path - Path when collection is empty
  • :verify_content_type - Verify file content matches declared MIME type (default: true)
  • :webp - WebP override: true/false/keyword, merged over global config (default: inherit)
  • :responsive - Responsive-images override: true/false/keyword, merged over global (default: inherit)

Examples

Collection.new(:images, accepts: ~w(image/jpeg image/png), max_size: 5_000_000)

Collection.new(:photos, webp: true)

Collection.new(:hero, webp: [quality: 90, keep_original: false], responsive: [widths: [640, 1280]])

Collection.new(:avatar, single_file: true, max_size: 2_000_000)

Collection.new(:documents,
  accepts: ~w(application/pdf),
  max_files: 10,
  max_size: 20_000_000,
  verify_content_type: true
)