Commit ea367fac08bc33a826455535831651e40097f131

Thomas de Grivel 2023-02-03T14:40:59

add / at end of directory URLs

diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index ca15b5e..39bbbf1 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -113,32 +113,36 @@ defmodule KmxgitWeb.RepositoryController do
     current_user = conn.assigns[:current_user]
     chunks = params["slug"] |> chunk_path()
     slug = chunks |> Enum.at(0) |> Enum.join("/")
-    op = get_op(chunks)
-    op_params = get_op_params(op, chunks)
-    repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
-    if op_params && repo && (repo.public_access || Repository.member?(repo, current_user)) do
-      org = repo.organisation
-      user = repo.user
-      git = setup_git(repo, conn, op, op_params)
-      first_tree = Enum.find_value(git.trees,
-        fn {:branch, "master", _} -> "master"
-          _ -> false
-        end) ||
-        case Enum.at(git.trees, 0) do
-          {_, first_tree, _} -> first_tree
-          nil -> nil
+    if conn.request_path == "/#{params["owner"]}/#{slug}" do
+      redirect conn, to: conn.request_path <> "/"
+    else
+      op = get_op(chunks)
+      op_params = get_op_params(op, chunks)
+      repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
+      if op_params && repo && (repo.public_access || Repository.member?(repo, current_user)) do
+        org = repo.organisation
+        user = repo.user
+        git = setup_git(repo, conn, op, op_params)
+        first_tree = Enum.find_value(git.trees,
+          fn {:branch, "master", _} -> "master"
+            _ -> false
+          end) ||
+          case Enum.at(git.trees, 0) do
+            {_, first_tree, _} -> first_tree
+            nil -> nil
+          end
+        tree1 = op_params.tree || first_tree
+        op_params = %OpParams{op_params | tree: tree1, git: git, org: org, repo: repo, user: user}
+        if git.valid do
+          show_op(conn, op || :tree, op_params)
+        else
+          IO.inspect(:invalid_git)
+          not_found(conn)
         end
-      tree1 = op_params.tree || first_tree
-      op_params = %OpParams{op_params | tree: tree1, git: git, org: org, repo: repo, user: user}
-      if git.valid do
-        show_op(conn, op || :tree, op_params)
       else
-        IO.inspect(:invalid_git)
+        IO.inspect(:no_repo)
         not_found(conn)
       end
-    else
-      IO.inspect(:no_repo)
-      not_found(conn)
     end
   end
 
diff --git a/lib/kmxgit_web/controllers/slug_controller.ex b/lib/kmxgit_web/controllers/slug_controller.ex
index 9d971cb..ad1269f 100644
--- a/lib/kmxgit_web/controllers/slug_controller.ex
+++ b/lib/kmxgit_web/controllers/slug_controller.ex
@@ -24,47 +24,51 @@ defmodule KmxgitWeb.SlugController do
   alias KmxgitWeb.UserView
 
   def show(conn, params) do
-    current_user = conn.assigns.current_user
-    slug = SlugManager.get_slug(params["slug"])
-    if !slug do
-      not_found(conn)
+    if ! String.match?(conn.request_path, ~r(/$)) do
+      redirect conn, to: conn.request_path <> "/"
     else
-      user = slug.user
-      if user do
-        owned_repos = User.owned_repositories(user)
-        contributor_repos = RepositoryManager.list_contributor_repositories(user)
-        repos = owned_repos ++ contributor_repos
-        |> Enum.filter(fn repo ->
-          repo.public_access || Repository.member?(repo, current_user)
-        end)
-        conn
-        |> assign(:disk_usage, User.disk_usage(user))
-        |> assign(:disk_usage_all, Repository.disk_usage(repos))
-        |> assign(:repos, repos)
-        |> assign(:page_title, gettext("User %{login}", login: User.login(user)))
-        |> assign(:user, user)
-        |> put_view(UserView)
-        |> render("show.html")
+      current_user = conn.assigns.current_user
+      slug = SlugManager.get_slug(params["slug"])
+      if !slug do
+        not_found(conn)
       else
-        org = slug.organisation
-        if org do
-          repos = org.owned_repositories
+        user = slug.user
+        if user do
+          owned_repos = User.owned_repositories(user)
+          contributor_repos = RepositoryManager.list_contributor_repositories(user)
+          repos = owned_repos ++ contributor_repos
           |> Enum.filter(fn repo ->
             repo.public_access || Repository.member?(repo, current_user)
           end)
-          |> Enum.sort(fn a, b ->
-            a.slug < b.slug
-          end)
           conn
-          |> assign(:current_organisation, org)
-          |> assign(:disk_usage, Organisation.disk_usage(org))
-          |> assign(:org, org)
-          |> assign(:page_title, org.name || org.slug_)
+          |> assign(:disk_usage, User.disk_usage(user))
+          |> assign(:disk_usage_all, Repository.disk_usage(repos))
           |> assign(:repos, repos)
-          |> put_view(OrganisationView)
+          |> assign(:page_title, gettext("User %{login}", login: User.login(user)))
+          |> assign(:user, user)
+          |> put_view(UserView)
           |> render("show.html")
         else
-          raise "invalid slug"
+          org = slug.organisation
+          if org do
+            repos = org.owned_repositories
+            |> Enum.filter(fn repo ->
+              repo.public_access || Repository.member?(repo, current_user)
+            end)
+            |> Enum.sort(fn a, b ->
+              a.slug < b.slug
+            end)
+            conn
+            |> assign(:current_organisation, org)
+            |> assign(:disk_usage, Organisation.disk_usage(org))
+            |> assign(:org, org)
+            |> assign(:page_title, org.name || org.slug_)
+            |> assign(:repos, repos)
+            |> put_view(OrganisationView)
+            |> render("show.html")
+          else
+            raise "invalid slug"
+          end
         end
       end
     end