generate pretty file name on demand (fixes philomena-dev/philomena#97)

This commit is contained in:
byte[] 2021-02-10 19:30:31 -05:00
parent f6c8d57bdf
commit ec7ed8d780
2 changed files with 14 additions and 3 deletions

View file

@ -449,6 +449,7 @@ defmodule PhilomenaWeb.Router do
scope "/autocomplete", Autocomplete, as: :autocomplete do scope "/autocomplete", Autocomplete, as: :autocomplete do
resources "/tags", TagController, only: [:show], singleton: true resources "/tags", TagController, only: [:show], singleton: true
end end
scope "/fetch", Fetch, as: :fetch do scope "/fetch", Fetch, as: :fetch do
resources "/tags", TagController, only: [:index] resources "/tags", TagController, only: [:index]
end end

View file

@ -108,7 +108,7 @@ defmodule PhilomenaWeb.ImageView do
root = image_url_root() root = image_url_root()
view = if download, do: "download", else: "view" view = if download, do: "download", else: "view"
filename = if short, do: image.id, else: escaped_file_name(image) filename = if short, do: image.id, else: verbose_file_name(image)
format = format =
image.image_format image.image_format
@ -119,8 +119,18 @@ defmodule PhilomenaWeb.ImageView do
"#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}" "#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}"
end end
defp escaped_file_name(image) do defp verbose_file_name(image) do
URI.encode(image.file_name_cache, &(&1 == ?+ or URI.char_unreserved?(&1))) # 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 =
image.tags
|> Enum.map_join("_", & &1.slug)
|> String.to_charlist()
|> Enum.filter(&(&1 in ?a..?z or &1 in '0123456789_-+'))
|> List.to_string()
|> String.slice(0..150)
"#{image.id}__#{file_name_slug_fragment}"
end end
def image_url_root do def image_url_root do