philomena/lib/philomena_web/views/image_view.ex

201 lines
6.3 KiB
Elixir
Raw Normal View History

2019-08-18 18:17:05 +02:00
defmodule PhilomenaWeb.ImageView do
use PhilomenaWeb, :view
alias Philomena.Tags.Tag
2019-11-30 23:40:53 +01:00
def show_vote_counts?(%{hide_vote_counts: true}), do: false
def show_vote_counts?(_user), do: true
# this is a bit ridculous
2019-12-01 18:11:00 +01:00
def render_intent(_conn, %{thumbnails_generated: false}, _size), do: :not_rendered
def render_intent(conn, image, size) do
2019-11-30 23:40:53 +01:00
uris = thumb_urls(image, false)
vid? = image.image_mime_type == "video/webm"
gif? = image.image_mime_type == "image/gif"
tags = Tag.display_order(image.tags) |> Enum.map_join(", ", & &1.name)
alt = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{tags}"
2019-12-01 18:11:00 +01:00
hidpi? = conn.cookies["hidpi"] == "true"
webm? = conn.cookies["webm"] == "true"
2019-11-30 23:40:53 +01:00
use_gif? = vid? and not webm? and size in ~W(thumb thumb_small thumb_tiny)a
2019-12-01 18:11:00 +01:00
filtered? = filter_or_spoiler_hits?(conn, image)
2019-11-30 23:40:53 +01:00
cond do
2019-12-01 18:11:00 +01:00
filtered? and vid? ->
{:filtered_video, alt}
filtered? and not vid? ->
{:filtered_image, alt}
2019-11-30 23:40:53 +01:00
hidpi? and not (gif? or vid?) ->
{:hidpi, uris[size], uris[:medium], alt}
not vid? or use_gif? ->
{:image, String.replace(uris[size], ".webm", ".gif"), alt}
true ->
{:video, uris[size], String.replace(uris[size], ".webm", ".mp4"), alt}
end
end
2019-10-05 02:27:26 +02: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-18 03:23:45 +01:00
tall: thumb_url(image, show_hidden, :tall),
2019-11-27 06:51:20 +01:00
full: pretty_url(image, true, false)
2019-10-05 02:27:26 +02:00
}
2019-11-30 23:40:53 +01:00
|> append_gif_urls(image, show_hidden)
end
defp append_gif_urls(urls, %{image_mime_type: "image/gif"} = image, show_hidden) do
full_url = thumb_url(image, show_hidden, :full)
Map.merge(
urls,
%{
webm: String.replace(full_url, ".gif", ".webm"),
mp4: String.replace(full_url, ".gif", ".mp4")
}
)
2019-10-05 02:27:26 +02:00
end
2019-11-30 23:40:53 +01:00
defp append_gif_urls(urls, _image, _show_hidden), do: urls
2019-10-05 02:27:26 +02:00
2019-08-18 18:17:05 +02:00
def thumb_url(image, show_hidden, name) do
2019-08-18 20:14:36 +02:00
%{year: year, month: month, day: day} = image.created_at
deleted = image.hidden_from_users
root = image_url_root()
2019-11-17 04:19:20 +01:00
format =
image.image_format
|> to_string()
2019-11-17 04:19:20 +01:00
|> String.downcase()
2019-12-04 02:56:39 +01:00
|> thumb_format(name)
2019-08-18 20:14:36 +02:00
id_fragment =
if deleted and show_hidden do
2019-11-27 06:51:20 +01:00
"#{image.id}-#{image.hidden_image_key}"
2019-08-18 20:14:36 +02:00
else
"#{image.id}"
end
"#{root}/#{year}/#{month}/#{day}/#{id_fragment}/#{name}.#{format}"
end
2019-11-27 06:51:20 +01:00
def pretty_url(image, short, download) do
2019-08-18 20:14:36 +02:00
%{year: year, month: month, day: day} = image.created_at
root = image_url_root()
view = if download, do: "download", else: "view"
2019-11-27 06:51:20 +01:00
filename = if short, do: image.id, else: image.file_name_cache
2019-11-17 04:19:20 +01:00
format =
image.image_format
|> to_string()
2019-11-17 04:19:20 +01:00
|> String.downcase()
2019-12-04 02:56:39 +01:00
|> thumb_format(nil)
2019-08-18 20:14:36 +02:00
"#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}"
end
def image_url_root do
Application.get_env(:philomena, :image_url_root)
2019-08-18 18:17:05 +02:00
end
2019-10-11 03:47:13 +02:00
2019-11-12 01:31:22 +01:00
def image_container_data(image, size) do
[
2019-10-11 03:47:13 +02:00
image_id: image.id,
image_tags: Jason.encode!(Enum.map(image.tags, & &1.id)),
2019-11-16 06:09:02 +01:00
image_tag_aliases: image.tag_list_plus_alias_cache,
2019-10-11 03:47:13 +02: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-12 01:31:22 +01:00
end
2019-10-11 03:47:13 +02:00
2019-11-12 01:31:22 +01:00
def image_container(image, size, block) do
content_tag(:div, block.(), class: "image-container #{size}", data: image_container_data(image, size))
2019-10-11 03:47:13 +02:00
end
2019-11-12 00:54:20 +01:00
def display_order(tags) do
Tag.display_order(tags)
2019-11-12 00:54:20 +01:00
end
2019-11-17 04:19:20 +01:00
2019-11-30 03:33:15 +01:00
def scope(conn), do: Philomena.ImageScope.scope(conn)
def anonymous_by_default?(conn) do
conn.assigns.current_user.anonymous_by_default
end
2019-12-05 22:57:59 +01:00
def info_row(_conn, []), do: []
2019-12-05 23:01:09 +01:00
def info_row(conn, [{tag, description, dnp_entries}]) do
render PhilomenaWeb.TagView, "_tag_info_row.html", conn: conn, tag: tag, body: description, dnp_entries: dnp_entries
2019-12-05 22:57:59 +01:00
end
def info_row(conn, tags) do
render PhilomenaWeb.TagView, "_tags_row.html", conn: conn, tags: tags
end
2019-12-07 17:48:39 +01:00
def deleter(%{deleter: %{name: name}}), do: name
def deleter(_image), do: "System"
2019-12-04 02:56:39 +01:00
defp thumb_format("svg", _name), do: "png"
defp thumb_format(_, :rendered), do: "png"
defp thumb_format(format, _name), do: format
2019-12-01 18:11:00 +01:00
defp image_filter_data(image) do
%{
id: image.id,
"namespaced_tags.name": String.split(image.tag_list_plus_alias_cache || "", ", "),
2019-12-01 18:11:00 +01:00
score: image.score,
faves: image.faves_count,
upvotes: image.upvotes_count,
downvotes: image.downvotes_count,
comment_count: image.comments_count,
created_at: image.created_at,
first_seen_at: image.first_seen_at,
source_url: image.source_url,
width: image.image_width,
height: image.image_height,
aspect_ratio: image.image_aspect_ratio,
sha512_hash: image.image_sha512_hash,
orig_sha512_hash: image.image_orig_sha512_hash
}
end
def filter_or_spoiler_hits?(conn, image) do
tag_filter_or_spoiler_hits?(conn, image) or complex_filter_or_spoiler_hits?(conn, image)
end
defp tag_filter_or_spoiler_hits?(conn, image) do
filter = conn.assigns.current_filter
filtered_tag_ids = MapSet.new(filter.spoilered_tag_ids ++ filter.hidden_tag_ids)
image_tag_ids = MapSet.new(image.tags, & &1.id)
MapSet.size(MapSet.intersection(filtered_tag_ids, image_tag_ids)) > 0
end
defp complex_filter_or_spoiler_hits?(conn, image) do
doc = image_filter_data(image)
complex_filter = conn.assigns.compiled_complex_filter
complex_spoiler = conn.assigns.compiled_complex_spoiler
query = %{
bool: %{
should: [complex_filter, complex_spoiler]
}
}
Search.Evaluator.hits?(doc, query)
end
2019-08-18 18:17:05 +02:00
end