-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the project to handle more features with the Ksuite.
- Loading branch information
Burgy Benjamin
committed
Dec 16, 2023
1 parent
f3bd838
commit e090027
Showing
6 changed files
with
82 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
57
lib/ksuite_middleware_web/controllers/kdrive_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters