aboutsummaryrefslogtreecommitdiffstats
path: root/examples/file_server/src/directory_lister.erl
blob: 2ca9d303b2b926c838830afbf8529918690b3228 (plain) (blame)
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
%% Feel free to use, reuse and abuse the code in this file.

-module(directory_lister).
-behaviour(cowboy_middleware).

-export([execute/2]).

execute(Req, Env=#{handler := cowboy_static}) ->
	redirect_directory(Req, Env);
execute(Req, Env) ->
	{ok, Req, Env}.

redirect_directory(Req, Env=#{handler_opts := {_, _, _, Extra}}) ->
	Path = cowboy_req:path_info(Req),
	Path1 = << <<S/binary, $/>> || S <- Path >>,
	{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
	FullPath = resource_path(Path1),
	case valid_path(Path) and filelib:is_dir(FullPath) of
		true -> handle_directory(Req, Env, Path1, FullPath, DirHandler);
		false -> {ok, Req, Env}
	end.

handle_directory(Req, Env, Prefix, Path, DirHandler) ->
	{ok, Req, Env#{handler => DirHandler, handler_opts => {Prefix, Path}}}.

valid_path([]) -> true;
valid_path([<<"..">> | _T]) -> false;
valid_path([<<"/", _/binary>> | _T]) -> false;
valid_path([_H | Rest]) -> valid_path(Rest).

resource_path(Path) ->
	filename:join([code:priv_dir(file_server), Path]).