Commit ebea0a267157893b6720a25b2bb8c36109740782

Thomas de Grivel 2022-01-02T19:16:52

display text files

diff --git a/config/config.exs b/config/config.exs
index 8a53e11..d4b40f3 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -21,6 +21,10 @@ config :kmxgit, KmxgitWeb.Endpoint,
   pubsub_server: Kmxgit.PubSub,
   live_view: [signing_salt: "0HhihW2z"]
 
+config :kmxgit, Kmxgit.UserManager.Guardian,
+  issuer: "kmxgit",
+  secret_key: File.read!("config/.guardian.secret_key")
+
 config :dart_sass,
   version: "1.39.0",
   default: [
@@ -58,9 +62,10 @@ config :logger, :console,
 # Use Jason for JSON parsing in Phoenix
 config :phoenix, :json_library, Jason
 
-config :kmxgit, Kmxgit.UserManager.Guardian,
-  issuer: "kmxgit",
-  secret_key: File.read!("config/.guardian.secret_key")
+config :mime, :types, %{
+  "text/markdown" => ["markdown", "md"],
+  "text/plain" => ["ac", "am", "c", "cc", "cxx", "ex", "eex", "h", "heex", "hh"]
+}
 
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
diff --git a/lib/kmxgit_web/controllers/repository_controller.ex b/lib/kmxgit_web/controllers/repository_controller.ex
index 76b8784..b2d0c1b 100644
--- a/lib/kmxgit_web/controllers/repository_controller.ex
+++ b/lib/kmxgit_web/controllers/repository_controller.ex
@@ -115,7 +115,7 @@ defmodule KmxgitWeb.RepositoryController do
           :blob ->
             if (git.content) do
               conn
-              |> put_resp_content_type("application/binary")
+              |> put_resp_content_type("application/octet-stream")
               |> put_resp_header("Content-Disposition", "attachment; filename=#{git.filename |> URI.encode()}")
               |> resp(200, git.content)
             else
@@ -240,8 +240,8 @@ defmodule KmxgitWeb.RepositoryController 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"
+                   [_, ext] -> MIME.type(ext) || "application/octet-stream"
+                   _ -> if String.valid?(content), do: "text/plain", else: "application/octet-stream"
                  end
           %{git | content: content, content_type: type, filename: name}
         {:error, error} -> %{git | status: error}