Current section
Files
Jump to
Current section
Files
src/howdy@static_resource.erl
-module(howdy@static_resource).
-compile(no_auto_import).
-export([get_file_contents/1, get_spa_file_contents/1]).
-spec get_file_contents(binary()) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
get_file_contents(File_path) ->
fun(Context) ->
Full_path_result = get_wildcard_segment_url(
erlang:element(2, Context),
File_path
),
case Full_path_result of
{ok, Full_path} ->
case filelib:is_file(Full_path) of
true ->
case gleam@erlang@file:read_bits(Full_path) of
{ok, File_contents} ->
_pipe = howdy@response:of_bit_string(
File_contents
),
howdy@response:with_content_type(
_pipe,
howdy@mime:from_path(Full_path)
);
{error, _@1} ->
howdy@response:of_interal_error(
<<"Fatal error"/utf8>>
)
end;
false ->
howdy@response:of_not_found(<<"File not found"/utf8>>)
end;
{error, _@2} ->
howdy@response:of_not_found(<<"File not found"/utf8>>)
end
end.
-spec get_spa_file_contents(binary()) -> fun((howdy@context:context()) -> gleam@http@response:response(gleam@bit_builder:bit_builder())).
get_spa_file_contents(File_path) ->
fun(_) -> case filelib:is_file(File_path) of
true ->
case gleam@erlang@file:read_bits(File_path) of
{ok, File_contents} ->
_pipe = howdy@response:of_bit_string(File_contents),
howdy@response:with_content_type(
_pipe,
howdy@mime:from_path(File_path)
);
{error, _@1} ->
howdy@response:of_interal_error(<<"Fatal error"/utf8>>)
end;
false ->
howdy@response:of_not_found(<<"File not found"/utf8>>)
end end.
-spec get_wildcard_segment_url(list(howdy@url_parser:url_segment()), binary()) -> {ok,
binary()} |
{error, nil}.
get_wildcard_segment_url(Segments, Root) ->
case gleam@list:last(Segments) of
{error, _try} -> {error, _try};
{ok, Segment} ->
case Segment of
{wildcard_segment, _@1, _@2, Url} ->
{ok, join_paths(Root, Url)};
_@3 ->
{error, nil}
end
end.
-spec join_paths(binary(), binary()) -> binary().
join_paths(Root, Path) ->
Modified_path = gleam@string:replace(Path, <<"\\"/utf8>>, <<"/"/utf8>>),
case {gleam@string:ends_with(Root, <<"/"/utf8>>),
gleam@string:starts_with(Modified_path, <<"/"/utf8>>)} of
{true, true} ->
gleam@string:concat(
[Root, gleam@string:drop_left(Modified_path, 1)]
);
{false, false} ->
gleam@string:concat([Root, <<"/"/utf8>>, Modified_path]);
{_@1, _@2} ->
gleam@string:concat([Root, Modified_path])
end.