View Source Bonfire.Common.Media (Bonfire v0.9.10-classic-beta.156)

Helpers for handling images and other media

Summary

Functions

Takes a Media map (or an object containing one) and returns the avatar's URL.

Takes a Media map (or an object containing one) and returns the avatar's URL.

Takes a Media map (or an object containing one) and returns the banner's URL.

Returns a map containing all files and their contents from a tar or compressed tar.gz archive.

Takes a Media map (or an object containing one) and returns the image's URL.

Determines the dominant color for a given user’s avatar or banner.

Takes a Media map (or an object containing one) and returns a URL for the media.

Reads specific files from a tar archive and returns their contents.

Takes a Media map (or an object containing one) and returns the thumbnail's URL.

Functions

Link to this function

avatar_fallback(_ \\ nil)

View Source
Link to this function

avatar_media(maybe_media)

View Source

Takes a Media map (or an object containing one) and returns the avatar's URL.

Examples

iex> avatar_media(%{profile: %{icon: "http://example.com/avatar.png"}})
%Media{...}

iex> avatar_media(%{icon: "http://example.com/icon.png"})
%Media{...}

iex> avatar_media(%{profile: %{icon: %{path: "http://example.com/path.png"}}})
%Media{...}

iex> avatar_media(%{nonexistent_key: "value"})
nil

Takes a Media map (or an object containing one) and returns the avatar's URL.

Examples

iex> avatar_url(%{profile: %{icon: %{url: "http://example.com/avatar.png"}}})
"http://example.com/avatar.png"

iex> avatar_url(%{icon: %{path: "http://example.com/path.png"}})
"http://example.com/path.png"

iex> avatar_url(%{icon_id: "icon123"})
# Assume Bonfire.Files.IconUploader.remote_url/1 returns "http://example.com/icon123.png"
"http://example.com/icon123.png"

iex> avatar_url(%{path: "image.jpg"})
# Assume Bonfire.Files.IconUploader.remote_url/1 returns "http://example.com/image.jpg"
"http://example.com/image.jpg"

iex> avatar_url(%{icon: "http://example.com/icon.png"})
"http://example.com/icon.png"

iex> avatar_url(%{image: "http://example.com/image.png"})
"http://example.com/image.png"

iex> avatar_url(%{id: "user123", shared_user: nil})
# Assume avatar_fallback/1 returns "/images/avatar.png"
"/images/avatar.png"

iex> avatar_url(%{id: "user456", shared_user: %{id: "shared123"}})
"https://picsum.photos/seed/user456/128/128?blur"

iex> avatar_url(%{id: "user789"})
# Assume avatar_fallback/1 returns "/images/avatar.png"
"/images/avatar.png"
Link to this function

extract_tar(archive, opts \\ [:compressed, :memory])

View Source

Returns a map containing all files and their contents from a tar or compressed tar.gz archive.

Examples

iex> extract_tar("path/to/archive.tar.gz")
%{"file1.txt" => <<...>> , "file2.txt" => <<...>>}

iex> extract_tar("path/to/archive.tar", [:memory])
%{"file1.txt" => <<...>> , "file2.txt" => <<...>>}

iex> extract_tar("path/to/archive.tar", [:compressed, :memory])
%{"file1.txt" => <<...>> , "file2.txt" => <<...>>}

Takes a Media map (or an object containing one) and returns the image's URL.

Examples

iex> image_url("http://example.com/image.png")
"http://example.com/image.png"

iex> image_url(%{media_type: "text/plain"})
nil

iex> image_url(%{profile: %{image: %{url: "http://example.com/image.png"}}})
"http://example.com/image.png"

iex> image_url(%{image: %{url: "http://example.com/image.png"}})
"http://example.com/image.png"

iex> image_url(%{icon: %{path: "http://example.com/image.png"}})
"http://example.com/image.png"

iex> image_url(%{path: "http://example.com/image.png"})
"http://example.com/image.png"

iex> image_url(%{image_id: "image123"})
# Assume Bonfire.Files.ImageUploader.remote_url/1 returns "http://example.com/image123.png"
"http://example.com/image123.png"

iex> image_url(%{image: "http://example.com/image.png"})
"http://example.com/image.png"

iex> image_url(%{profile: %{image: "http://example.com/profile_image.png"}})
"http://example.com/profile_image.png"

iex> image_url(%{nonexistent_key: "value"})
nil
Link to this function

maybe_dominant_color(user, avatar_url \\ nil, banner_url \\ nil, banner_fallback \\ nil)

View Source

Determines the dominant color for a given user’s avatar or banner.

Examples

iex> maybe_dominant_color(%{profile: %{icon: "http://example.com/avatar.png"}})
"#AA4203" # Example dominant color

iex> maybe_dominant_color(%{profile: %{icon: "http://example.com/avatar.png"}}, nil, "http://example.com/banner.png")
"#AA4203" # Example dominant color

iex> maybe_dominant_color(%{profile: %{icon: "http://example.com/avatar.png"}}, nil, nil, "/images/bonfires.png")
"#AA4203" # Example dominant color

iex> maybe_dominant_color(%{profile: %{icon: nil}}, "http://example.com/banner.png")
nil

Takes a Media map (or an object containing one) and returns a URL for the media.

Examples

iex> media_url(%{path: "http://example.com/image.jpg"})
"http://example.com/image.jpg"

iex> media_url(%{path: "remote.jpg", metadata: %{"module" => "MyModule"}})
# Assume MyModule.remote_url/1 is defined and returns "http://example.com/remote.jpg"
"http://example.com/remote.jpg"

iex> media_url(%{media_type: "image/jpeg", path: "image.jpg"})
"http://image.jpg"

iex> media_url(%{media_type: "text/plain", path: "document.txt"})
"http://document.txt"

iex> media_url(%{changes: %{path: "http://changed.example.com/image.jpg"}})
"http://changed.example.com/image.jpg"

iex> media_url(%{path: "image.jpg"})
"http://image.jpg"

iex> media_url(%{media: %{path: "http://nested.example.com/image.jpg"}})
"http://nested.example.com/image.jpg"

iex> media_url(%{nonexistent_key: "value"})
nil
Link to this function

read_tar_files(archive, file_or_files, opts \\ [:compressed, :verbose])

View Source

Reads specific files from a tar archive and returns their contents.

Examples

iex> read_tar_files("path/to/archive.tar", "file1.txt")
{:ok, "file1 contents"}

iex> read_tar_files("path/to/archive.tar", ["file1.txt", "file2.txt"])
{:ok, ["file1 contents", "file2 contents"]}

iex> read_tar_files("path/to/nonexistent.tar", "file1.txt")
{:error, "File not found"}

iex> read_tar_files("path/to/archive.tar", "nonexistent_file.txt")
{:error, "File not found"}

Takes a Media map (or an object containing one) and returns the thumbnail's URL.

Examples

iex> thumbnail_url(%{path: "thumbnail.jpg", metadata: %{"module" => "MyModule"}})
# Assume MyModule.remote_url/2 with :thumbnail returns "http://example.com/thumbnail.jpg"
"http://example.com/thumbnail.jpg"

iex> thumbnail_url(%{media_type: "image/jpeg", path: "thumbnail.jpg"})
"http://thumbnail.jpg"

iex> thumbnail_url(%{media_type: "video/mp4", path: "video.mpeg"})
# Assume Bonfire.Files.VideoUploader.remote_url/2 with :thumbnail returns "http://video-thumbnail.jpg"
"http://video-thumbnail.jpg"

iex> thumbnail_url(%{path: "document.pdf", media_type: "document"})
# Assume Bonfire.Files.DocumentUploader.remote_url/2 with :thumbnail returns "http://document-thumbnail.jpg"
"http://document-thumbnail.jpg"

iex> thumbnail_url(%{nonexistent_key: "value"})
nil