fix escaping error in image filenames

This commit is contained in:
byte[] 2021-02-09 17:14:34 -05:00
parent a5904b1fcd
commit 4be6e23400
2 changed files with 8 additions and 3 deletions

View file

@ -326,8 +326,9 @@ defmodule Philomena.Images.Image do
file_name_slug_fragment =
tags
|> Enum.map_join("_", & &1.slug)
|> String.replace("%", "")
|> String.replace("/", "")
|> String.to_charlist()
|> Enum.filter(&(&1 in ?a..?z or &1 in '0123456789_-'))
|> List.to_string()
|> String.slice(0..150)
file_name_cache = "#{image_id}__#{file_name_slug_fragment}"

View file

@ -108,7 +108,7 @@ defmodule PhilomenaWeb.ImageView do
root = image_url_root()
view = if download, do: "download", else: "view"
filename = if short, do: image.id, else: image.file_name_cache
filename = if short, do: image.id, else: escaped_file_name(image)
format =
image.image_format
@ -119,6 +119,10 @@ defmodule PhilomenaWeb.ImageView do
"#{root}/#{view}/#{year}/#{month}/#{day}/#{filename}.#{format}"
end
defp escaped_file_name(image) do
URI.encode(image.file_name_cache, &(&1 == ?+ or URI.char_unreserved?(&1)))
end
def image_url_root do
Application.get_env(:philomena, :image_url_root)
end