philomena/lib/philomena_web/views/image_view.ex

87 lines
2.5 KiB
Elixir
Raw Normal View History

2019-08-18 12:17:05 -04:00
defmodule PhilomenaWeb.ImageView do
use PhilomenaWeb, :view
alias Philomena.Tags.Tag
2019-10-04 20:27:26 -04:00
def thumb_urls(image, show_hidden) do
%{
thumb_tiny: thumb_url(image, show_hidden, :thumb_tiny),
thumb_small: thumb_url(image, show_hidden, :thumb_small),
thumb: thumb_url(image, show_hidden, :thumb),
small: thumb_url(image, show_hidden, :small),
medium: thumb_url(image, show_hidden, :medium),
large: thumb_url(image, show_hidden, :large),
2019-11-17 21:23:45 -05:00
tall: thumb_url(image, show_hidden, :tall),
2019-11-27 00:51:20 -05:00
full: pretty_url(image, true, false)
2019-10-04 20:27:26 -04:00
}
end
2019-08-18 12:17:05 -04:00
def thumb_url(image, show_hidden, name) do
2019-08-18 14:14:36 -04:00
%{year: year, month: month, day: day} = image.created_at
deleted = image.hidden_from_users
root = image_url_root()
2019-11-16 22:19:20 -05:00
format =
image.image_format
|> String.downcase()
|> thumb_format()
2019-08-18 14:14:36 -04:00
id_fragment =
if deleted and show_hidden do
2019-11-27 00:51:20 -05:00
"#{image.id}-#{image.hidden_image_key}"
2019-08-18 14:14:36 -04:00
else
"#{image.id}"
end
"#{root}/#{year}/#{month}/#{day}/#{id_fragment}/#{name}.#{format}"
end
2019-11-27 00:51:20 -05:00
def pretty_url(image, short, download) do
2019-08-18 14:14:36 -04:00
%{year: year, month: month, day: day} = image.created_at
root = image_url_root()
view = if download, do: "download", else: "view"
2019-11-27 00:51:20 -05:00
filename = if short, do: image.id, else: image.file_name_cache
2019-11-16 22:19:20 -05:00
format =
image.image_format
|> String.downcase()
|> thumb_format()
2019-08-18 14:14:36 -04:00
"#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}"
end
def image_url_root do
Application.get_env(:philomena, :image_url_root)
2019-08-18 12:17:05 -04:00
end
2019-10-10 21:47:13 -04:00
2019-11-11 19:31:22 -05:00
def image_container_data(image, size) do
[
2019-10-10 21:47:13 -04:00
image_id: image.id,
image_tags: Jason.encode!(Enum.map(image.tags, & &1.id)),
2019-11-16 00:09:02 -05:00
image_tag_aliases: image.tag_list_plus_alias_cache,
2019-10-10 21:47:13 -04:00
score: image.score,
faves: image.faves_count,
upvotes: image.upvotes_count,
downvotes: image.downvotes_count,
comment_count: image.comments_count,
created_at: NaiveDateTime.to_iso8601(image.created_at),
source_url: image.source_url,
uris: Jason.encode!(thumb_urls(image, false)),
width: image.image_width,
height: image.image_height,
aspect_ratio: image.image_aspect_ratio,
size: size
]
2019-11-11 19:31:22 -05:00
end
2019-10-10 21:47:13 -04:00
2019-11-11 19:31:22 -05:00
def image_container(image, size, block) do
content_tag(:div, block.(), class: "image-container #{size}", data: image_container_data(image, size))
2019-10-10 21:47:13 -04:00
end
2019-11-11 18:54:20 -05:00
def display_order(tags) do
Tag.display_order(tags)
2019-11-11 18:54:20 -05:00
end
2019-11-16 22:19:20 -05:00
defp thumb_format("svg"), do: "png"
defp thumb_format(format), do: format
2019-08-18 12:17:05 -04:00
end