ash_hq/lib/ash_hq_web/components/forum/attachment.ex

41 lines
860 B
Elixir
Raw Normal View History

defmodule AshHqWeb.Components.Forum.Attachment do
2023-01-22 20:51:02 +13:00
@moduledoc "Renders an attachment"
use Surface.Component
2023-01-22 20:51:02 +13:00
prop(attachment, :any, required: true)
def render(assigns) do
~F"""
<div>
{#case video_type(@attachment.filename)}
{#match {:video, mime}}
<video controls width={@attachment.width} height={@attachment.height}>
<source src={@attachment.url} type={mime}>
</video>
2023-01-22 20:51:02 +13:00
2023-01-22 21:40:07 +13:00
{#match {:image, _mime}}
2023-01-22 20:51:02 +13:00
<img src={@attachment.url} width={@attachment.width} height={@attachment.height} />
{#match _}
other
{/case}
</div>
"""
end
defp video_type(path) do
mime = MIME.from_path(path)
case mime do
"image/" <> _ ->
{:image, mime}
"video/" <> _ ->
{:video, mime}
_ ->
{:other, mime}
end
end
end