Added plug to strip file extensions

This commit is contained in:
Kieran Eglin 2024-03-23 13:20:28 -07:00
parent 566941228c
commit 0d9003cffd
No known key found for this signature in database
GPG key ID: 193984967FCF432D
2 changed files with 30 additions and 0 deletions

View file

@ -47,5 +47,33 @@ defmodule PinchflatWeb.Endpoint do
plug Plug.MethodOverride
plug Plug.Head
plug Plug.Session, @session_options
plug :strip_trailing_extension
plug PinchflatWeb.Router
defp strip_trailing_extension(%{path_info: []} = conn, _opts), do: conn
defp strip_trailing_extension(conn, _opts) do
path =
conn.path_info
|> List.last()
|> String.split(".")
|> Enum.reverse()
case path do
[_] ->
conn
[_format | fragments] ->
new_path =
fragments
|> Enum.reverse()
|> Enum.join(".")
path_fragments = List.replace_at(conn.path_info, -1, new_path)
%{conn | path_info: path_fragments}
end
end
end

View file

@ -1,6 +1,8 @@
defmodule PinchflatWeb.Router do
use PinchflatWeb, :router
# IMPORTANT: `strip_trailing_extension` in endpoint.ex removes
# the extension from the path
pipeline :browser do
plug :basic_auth
plug :accepts, ["html"]