Skip to content

Commit

Permalink
Refactor the project to handle more features with the Ksuite.
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgy Benjamin committed Dec 16, 2023
1 parent f3bd838 commit e090027
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 69 deletions.
9 changes: 9 additions & 0 deletions lib/ksuite_middleware_web/controllers/error_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule KsuiteMiddlewareWeb.ErrorController do
use KsuiteMiddlewareWeb, :controller

def not_found(conn, _params),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(404, Application.app_dir(:ksuite_middleware, "priv/static/not-found.html"))
end
57 changes: 57 additions & 0 deletions lib/ksuite_middleware_web/controllers/kdrive_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule KsuiteMiddlewareWeb.KdriveController do
use KsuiteMiddlewareWeb, :controller

alias KsuiteMiddleware.KsuiteClient

def pass_thru(conn, %{"file_id" => id}) when is_integer(id) do
with {:ok, response} <- KsuiteClient.download(id) do
conn |> put_tesla_response(response)
else
_ -> conn |> resp(500, "an unknown error occurred.")
end
end

def pass_thru(conn, %{"file_id" => raw_id}) do
with {file_id, _} <- Integer.parse(raw_id),
{:ok, response} <- KsuiteClient.download(file_id) do
conn |> put_tesla_response(response)
else
_ ->
conn |> resp(500, "an unknown error occurred.")
end
end

def pass_thru(conn, _params),
do: conn |> resp(400, "The file id was missing.")

# Private

defp put_tesla_response(%Plug.Conn{} = conn, %Tesla.Env{status: 401}),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(400, Application.app_dir(:ksuite_middleware, "priv/static/bad-api-key.html"))

defp put_tesla_response(%Plug.Conn{} = conn, %Tesla.Env{} = response) do
%Tesla.Env{status: status, body: body} = response

with {:ok, content_type} <- safe_get_header(response, "content-type") do
conn
|> put_resp_content_type(content_type)
|> resp(status, body)
else
_ ->
conn
|> resp(status, body)
end
end

defp safe_get_header(%Tesla.Env{} = response, header_name) when is_bitstring(header_name) do
with header_value <- Tesla.get_header(response, header_name),
false <- is_nil(header_value) do
{:ok, header_value}
else
_ -> {:header_not_found, response}
end
end
end
58 changes: 0 additions & 58 deletions lib/ksuite_middleware_web/controllers/main_controller.ex
Original file line number Diff line number Diff line change
@@ -1,67 +1,9 @@
defmodule KsuiteMiddlewareWeb.MainController do
use KsuiteMiddlewareWeb, :controller

alias KsuiteMiddleware.KsuiteClient

def index(conn, _params),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(200, Application.app_dir(:ksuite_middleware, "priv/static/index.html"))

def not_found(conn, _params),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(404, Application.app_dir(:ksuite_middleware, "priv/static/not-found.html"))

def pass_thru(conn, %{"file_id" => id}) when is_integer(id) do
with {:ok, response} <- KsuiteClient.download(id) do
conn |> put_tesla_response(response)
else
_ -> conn |> resp(500, "an unknown error occurred.")
end
end

def pass_thru(conn, %{"file_id" => raw_id}) do
with {file_id, _} <- Integer.parse(raw_id),
{:ok, response} <- KsuiteClient.download(file_id) do
conn |> put_tesla_response(response)
else
_ ->
conn |> resp(500, "an unknown error occurred.")
end
end

def pass_thru(conn, _params),
do: conn |> resp(400, "The file id was missing.")

defp put_tesla_response(%Plug.Conn{} = conn, %Tesla.Env{status: 401}),
do:
conn
|> put_resp_header("content-type", "text/html; charset=utf-8")
|> send_file(404, Application.app_dir(:ksuite_middleware, "priv/static/bad-api-key.html"))

defp put_tesla_response(%Plug.Conn{} = conn, %Tesla.Env{} = response) do
%Tesla.Env{status: status, body: body} = response

with {:ok, content_type} <- safe_get_header(response, "content-type") do
conn
|> put_resp_content_type(content_type)
|> resp(status, body)
else
_ ->
conn
|> resp(status, body)
end
end

defp safe_get_header(%Tesla.Env{} = response, header_name) when is_bitstring(header_name) do
with header_value <- Tesla.get_header(response, header_name),
false <- is_nil(header_value) do
{:ok, header_value}
else
_ -> {:header_not_found, response}
end
end
end
4 changes: 2 additions & 2 deletions lib/ksuite_middleware_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule KsuiteMiddlewareWeb.Router do
scope "/files", KsuiteMiddlewareWeb do
pipe_through :api

get "/:file_id", MainController, :pass_thru
get "/:file_id", KdriveController, :pass_thru
end

# Enable LiveDashboard in development
Expand All @@ -41,6 +41,6 @@ defmodule KsuiteMiddlewareWeb.Router do
scope "/", KsuiteMiddlewareWeb do
pipe_through :browser

get "/*path", MainController, :not_found
get "/*path", ErrorController, :not_found
end
end
12 changes: 7 additions & 5 deletions priv/static/bad-api-key.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>

<head>
<title>Couloir42</title>
<title>Ksuite Middleware - 400</title>
<style>
body {
text-align: center;
Expand Down Expand Up @@ -39,11 +39,13 @@

<body>
<article>
<h1>Ksuite - Middleware</h1>
<h1>Ksuite Middleware - 400</h1>
<div>
<p>Ooopps!</p>
<p>It appears that the API key is incorrect. Please verify whether the environment variable KSUITE_API_TOKEN
has been properly defined.</p>
<p>
Whoopsie-daisy! Looks like we got a little lost in API land. Our secret handshake seems a bit off.
Double-check that magic spell in the enchanted scroll labeled `KSUITE_API_TOKEN` and make sure it's been
properly whispered into the winds of the environment variable realm.
</p>
</div>
</article>
</body>
Expand Down
11 changes: 7 additions & 4 deletions priv/static/not-found.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>

<head>
<title>Ksuite Middleware</title>
<title>Ksuite Middleware - 404</title>
<style>
body {
text-align: center;
Expand Down Expand Up @@ -39,10 +39,13 @@

<body>
<article>
<h1>Ksuite Middleware</h1>
<h1>Ksuite Middleware - 404</h1>
<div>
<p>Ooopps!</p>
<p>The was file not found.</p>
<p>
Oh snap! It seems we've hit a hiccup in our treasure hunt—what you seek is playing hide and seek in the
digital jungle! Keep those eagle eyes peeled, and perhaps try a different map or a clever keyword dance
to coax the elusive information out of hiding.
</p>
</div>
</article>
</body>
Expand Down

0 comments on commit e090027

Please sign in to comment.