# `ElementorexCms.Media`
[🔗](https://github.com/rocket4ce/elementorex_cms/blob/v0.1.0/lib/elementorex_cms/media.ex#L1)

Behaviour to connect the editor's image fields to your file storage.

Without a media module, image fields take a URL. With one, they also offer
*Upload* (LiveView uploads, stored by `store/2`) and *Library* (a grid of
existing files from `list/2`).

    defmodule MyApp.EditorMedia do
      @behaviour ElementorexCms.Media

      @impl true
      def store(%{path: path, filename: filename, content_type: type}, %{user_id: user_id}) do
        with {:ok, file} <- MyApp.Files.upload(path, filename, type, user_id) do
          {:ok, %{url: file.url, alt: file.alt}}
        end
      end

      @impl true
      def list(opts, _context) do
        MyApp.Files.list_images(search: opts[:search], limit: 40)
        |> Enum.map(&%{url: &1.url, thumb: &1.url, alt: &1.alt, name: &1.filename})
      end
    end

    <.live_component module={ElementorexCms.Editor} id="editor" media={MyApp.EditorMedia}
      media_context={%{user_id: @current_scope.user.id}} ... />

# `item`

```elixir
@type item() :: %{
  :url =&gt; String.t(),
  optional(:thumb) =&gt; String.t(),
  optional(:alt) =&gt; String.t(),
  optional(:name) =&gt; String.t()
}
```

A file of the library.

# `upload`

```elixir
@type upload() :: %{
  path: String.t(),
  filename: String.t(),
  content_type: String.t() | nil,
  size: non_neg_integer()
}
```

An uploaded file: its temporary path, the client's file name and the content type.

# `list`
*optional* 

```elixir
@callback list(opts :: keyword(), context :: map()) :: [item()]
```

Lists library images. `opts` has `:search` (may be `nil`).

# `store`

```elixir
@callback store(upload(), context :: map()) ::
  {:ok, %{:url =&gt; String.t(), optional(:alt) =&gt; String.t()}} | {:error, term()}
```

Stores an uploaded image and returns its public URL.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
