diff --git a/lib/philomena/images/image.ex b/lib/philomena/images/image.ex index f1c416c5..3d559d09 100644 --- a/lib/philomena/images/image.ex +++ b/lib/philomena/images/image.ex @@ -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}" diff --git a/lib/philomena_web/views/image_view.ex b/lib/philomena_web/views/image_view.ex index 4fe58083..68517f1f 100644 --- a/lib/philomena_web/views/image_view.ex +++ b/lib/philomena_web/views/image_view.ex @@ -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