From 31d05e312c3eea0154b682663e81b79c8bf05a5e Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 27 Nov 2019 00:51:20 -0500 Subject: [PATCH] miscellaneous fixes --- lib/philomena/images.ex | 5 +++ lib/philomena/images/image.ex | 52 ++++++++++++++++++++++++++- lib/philomena/images/tag_differ.ex | 40 --------------------- lib/philomena_web/views/app_view.ex | 31 ++++++++++++++-- lib/philomena_web/views/image_view.ex | 8 ++--- vagrant/philomena-nginx.conf | 2 +- 6 files changed, 90 insertions(+), 48 deletions(-) diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index bdddc6c9..358f0772 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -56,6 +56,11 @@ defmodule Philomena.Images do Multi.new |> Multi.insert(:image, image) + |> Multi.run(:name_caches, fn repo, %{image: image} -> + image + |> Image.cache_changeset() + |> repo.update() + end) |> Multi.run(:added_tag_count, fn repo, %{image: image} -> tag_ids = image.added_tags |> Enum.map(& &1.id) tags = Tag |> where([t], t.id in ^tag_ids) diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index eecc694f..4c5a4a55 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -7,6 +7,7 @@ defmodule Philomena.Images.Image do doc_type: "image" import Ecto.Changeset + import Ecto.Query alias Philomena.ImageIntensities.ImageIntensity alias Philomena.ImageVotes.ImageVote @@ -22,6 +23,7 @@ defmodule Philomena.Images.Image do alias Philomena.Images.TagDiffer alias Philomena.Images.TagValidator + alias Philomena.Repo schema "images" do belongs_to :user, User @@ -138,7 +140,7 @@ defmodule Philomena.Images.Image do |> validate_number(:image_height, greater_than: 0, less_than_or_equal_to: 32767) |> validate_length(:image_name, max: 255, count: :bytes) |> validate_inclusion(:image_mime_type, ~W(image/gif image/jpeg image/png image/svg+xml video/webm)) - |> unsafe_validate_unique([:image_sha512_hash], Philomena.Repo) + |> unsafe_validate_unique([:image_sha512_hash], Repo) end def source_changeset(image, attrs) do @@ -153,6 +155,7 @@ defmodule Philomena.Images.Image do |> cast(attrs, []) |> TagDiffer.diff_input(old_tags, new_tags) |> TagValidator.validate_tags() + |> cache_changeset() end def thumbnail_changeset(image, attrs) do @@ -166,4 +169,51 @@ defmodule Philomena.Images.Image do |> cast(attrs, [:image_sha512_hash]) |> change(processed: true) end + + def cache_changeset(image) do + changeset = change(image) + image = apply_changes(changeset) + + {tag_list_cache, tag_list_plus_alias_cache, file_name_cache} = + create_caches(image.id, image.tags) + + changeset + |> put_change(:tag_list_cache, tag_list_cache) + |> put_change(:tag_list_plus_alias_cache, tag_list_plus_alias_cache) + |> put_change(:file_name_cache, file_name_cache) + end + + defp create_caches(image_id, tags) do + tags = Tag.display_order(tags) + + tag_list_cache = + tags + |> Enum.map_join(", ", & &1.name) + + tag_ids = + tags |> Enum.map(& &1.id) + + aliases = + Tag + |> where([t], t.aliased_tag_id in ^tag_ids) + |> Repo.all() + + tag_list_plus_alias_cache = + (tags ++ aliases) + |> Tag.display_order() + |> Enum.map_join(", ", & &1.name) + + # Truncate filename to 150 characters, making room for the path + filename on Windows + # https://stackoverflow.com/questions/265769/maximum-filename-length-in-ntfs-windows-xp-and-windows-vista + file_name_slug_fragment = + tags + |> Enum.map_join("_", & &1.slug) + |> String.replace("%2F", "") + |> String.replace("/", "") + |> String.slice(0..150) + + file_name_cache = "#{image_id}__#{file_name_slug_fragment}" + + {tag_list_cache, tag_list_plus_alias_cache, file_name_cache} + end end diff --git a/lib/philomena/images/tag_differ.ex b/lib/philomena/images/tag_differ.ex index 3c665816..849fbdca 100644 --- a/lib/philomena/images/tag_differ.ex +++ b/lib/philomena/images/tag_differ.ex @@ -17,15 +17,9 @@ defmodule Philomena.Images.TagDiffer do {tags, actually_added, actually_removed} = apply_changes(tags, added_tags, removed_tags) - {tag_list_cache, tag_list_plus_alias_cache, file_name_cache} = - create_caches(image_id, tags) - changeset |> put_change(:added_tags, actually_added) |> put_change(:removed_tags, actually_removed) - |> put_change(:tag_list_cache, tag_list_cache) - |> put_change(:tag_list_plus_alias_cache, tag_list_plus_alias_cache) - |> put_change(:file_name_cache, file_name_cache) |> put_assoc(:tags, tags) end @@ -95,38 +89,4 @@ defmodule Philomena.Images.TagDiffer do {tags, actually_added, actually_removed} end - - defp create_caches(image_id, tags) do - tags = Tag.display_order(tags) - - tag_list_cache = - tags - |> Enum.map_join(", ", & &1.name) - - tag_ids = - tags |> Enum.map(& &1.id) - - aliases = - Tag - |> where([t], t.aliased_tag_id in ^tag_ids) - |> Repo.all() - - tag_list_plus_alias_cache = - (tags ++ aliases) - |> Tag.display_order() - |> Enum.map_join(", ", & &1.name) - - # Trunate filename to 150 characters, making room for the path + filename on Windows - # https://stackoverflow.com/questions/265769/maximum-filename-length-in-ntfs-windows-xp-and-windows-vista - file_name_slug_fragment = - tags - |> Enum.map_join("_", & &1.slug) - |> String.replace("%2F", "") - |> String.replace("/", "") - |> String.slice(0..150) - - file_name_cache = "#{image_id}__#{file_name_slug_fragment}" - - {tag_list_cache, tag_list_plus_alias_cache, file_name_cache} - end end \ No newline at end of file diff --git a/lib/philomena_web/views/app_view.ex b/lib/philomena_web/views/app_view.ex index 7aeb94dc..d3dda133 100644 --- a/lib/philomena_web/views/app_view.ex +++ b/lib/philomena_web/views/app_view.ex @@ -15,10 +15,25 @@ defmodule PhilomenaWeb.AppView do years: "%d years" } + @months %{ + 1 => "January", + 2 => "February", + 3 => "March", + 4 => "April", + 5 => "May", + 6 => "June", + 7 => "July", + 8 => "August", + 9 => "September", + 10 => "October", + 11 => "November", + 12 => "December" + } + def pretty_time(time) do seconds = NaiveDateTime.diff(NaiveDateTime.utc_now(), time, :second) relation = if seconds < 0, do: "from now", else: "ago" - time = time |> DateTime.from_naive!("Etc/UTC") |> DateTime.to_iso8601() + time = time |> DateTime.from_naive!("Etc/UTC") seconds = abs(seconds) minutes = abs(div(seconds, 60)) @@ -42,7 +57,7 @@ defmodule PhilomenaWeb.AppView do true -> String.replace(@time_strings[:years], "%d", to_string(years)) end - content_tag(:time, "#{words} #{relation}", datetime: time, title: time) + content_tag(:time, "#{words} #{relation}", datetime: DateTime.to_iso8601(time), title: datetime_string(time)) end def can?(conn, action, model) do @@ -76,4 +91,16 @@ defmodule PhilomenaWeb.AppView do submit text, class: class, data: data end) end + + defp datetime_string(time) do + :io_lib.format("~2..0B:~2..0B:~2..0B, ~s ~B, ~B", [ + time.hour, + time.minute, + time.second, + @months[time.month], + time.day, + time.year + ]) + |> to_string() + end end diff --git a/lib/philomena_web/views/image_view.ex b/lib/philomena_web/views/image_view.ex index 3e65388c..3ddc5d04 100644 --- a/lib/philomena_web/views/image_view.ex +++ b/lib/philomena_web/views/image_view.ex @@ -12,7 +12,7 @@ defmodule PhilomenaWeb.ImageView do medium: thumb_url(image, show_hidden, :medium), large: thumb_url(image, show_hidden, :large), tall: thumb_url(image, show_hidden, :tall), - full: thumb_url(image, show_hidden, :full) + full: pretty_url(image, true, false) } end @@ -27,7 +27,7 @@ defmodule PhilomenaWeb.ImageView do id_fragment = if deleted and show_hidden do - "#{image.id}-#{image.hidden_image_Key}" + "#{image.id}-#{image.hidden_image_key}" else "#{image.id}" end @@ -35,12 +35,12 @@ defmodule PhilomenaWeb.ImageView do "#{root}/#{year}/#{month}/#{day}/#{id_fragment}/#{name}.#{format}" end - def pretty_url(image, _short, download) do + def pretty_url(image, short, download) do %{year: year, month: month, day: day} = image.created_at root = image_url_root() view = if download, do: "download", else: "view" - filename = "#{image.id}" + filename = if short, do: image.id, else: image.file_name_cache format = image.image_format |> String.downcase() diff --git a/vagrant/philomena-nginx.conf b/vagrant/philomena-nginx.conf index 782981f0..54f9ffcb 100644 --- a/vagrant/philomena-nginx.conf +++ b/vagrant/philomena-nginx.conf @@ -11,7 +11,7 @@ server { location ~ ^/img/view/(.+)/([0-9]+).*\.([A-Za-z]+)$ { expires max; add_header Cache-Control public; - alias "APP_DIR/priv/static/images/thumbs/$1/$2/full.$3"; + alias "APP_DIR/priv/static/system/images/thumbs/$1/$2/full.$3"; } location ~ ^/img/download/(.+)/([0-9]+).*\.([A-Za-z]+)$ {