better display of file contents, also do not show content for directories containing only one file. support display of images and downloading blobs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 38d7ecd..76b8784 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -191,6 +191,7 @@ defmodule KmxgitWeb.RepositoryController do
defp setup_git(repo, branch, path, conn) do
%{branches: [],
content: nil,
+ content_type: nil,
filename: nil,
files: [],
readme: [],
@@ -198,7 +199,7 @@ defmodule KmxgitWeb.RepositoryController do
valid: true}
|> git_put_branches(repo, conn)
|> git_put_files(repo, branch, path, conn)
- |> git_put_content(repo)
+ |> git_put_content(repo, path)
|> git_put_readme(repo)
end
@@ -234,15 +235,23 @@ defmodule KmxgitWeb.RepositoryController do
git
end
- defp git_put_content(git = %{files: [%{name: name, sha1: sha1, type: "blob"}], valid: true}, repo) do
- IO.inspect(git)
- case GitManager.content(Repository.full_slug(repo), sha1) do
- {:ok, content} -> %{git | content: content, filename: name}
- {:error, error} -> %{git | status: error}
+ defp git_put_content(git = %{files: [%{name: name, sha1: sha1, type: "blob"}], valid: true}, repo, path) do
+ if (path == name) do
+ case GitManager.content(Repository.full_slug(repo), sha1) do
+ {:ok, content} ->
+ type = case Regex.run(~r/[.]([^.]+)$/, path) do
+ [_, ext] -> MIME.type(ext)
+ _ -> "application/octet-stream"
+ end
+ %{git | content: content, content_type: type, filename: name}
+ {:error, error} -> %{git | status: error}
+ end
+ else
+ git
end
end
- defp git_put_content(git, _) do
+ defp git_put_content(git, _, _) do
git
end
@@ -477,7 +486,6 @@ defmodule KmxgitWeb.RepositoryController do
slug = Enum.join(params["slug"], "/")
repo = RepositoryManager.get_repository_by_owner_and_slug(params["owner"], slug)
if repo && Repository.member?(repo, current_user) do
- IO.inspect(params)
fork_to = params["repository"]["fork_to"]
slug = String.split(fork_to, "/") |> Enum.at(0) |> SlugManager.get_slug()
if slug do
diff --git a/lib/kmxgit_web/templates/repository/show.html.heex b/lib/kmxgit_web/templates/repository/show.html.heex
index 4912277..6b83810 100644
--- a/lib/kmxgit_web/templates/repository/show.html.heex
+++ b/lib/kmxgit_web/templates/repository/show.html.heex
@@ -29,7 +29,13 @@
<%= if @git.content do %>
<hr/>
<h2><%= gettext("Content") %></h2>
- <pre><%= @git.content %></pre>
+ <%= if String.match?(@git.content_type, ~r(^text/)) do %>
+ <pre><%= @git.content %></pre>
+ <% end %>
+ <%= if String.match?(@git.content_type, ~r(^image/)) do %>
+ <img src={"data:#{@git.content_type};base64,#{Base.encode64(@git.content)}"}/>
+ <% end %>
+ <%= link gettext("Download"), to: Routes.repository_path(@conn, :show, Repository.owner_slug(@repo), Repository.splat(@repo, ["_blob", @branch | String.split(@path, "/")])), class: "btn btn-primary" %>
<% else %>
<hr/>
<h2><%= gettext("Files") %></h2>