Created
October 8, 2013 00:09
-
-
Save djnym/6877279 to your computer and use it in GitHub Desktop.
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
-module (mondemand_server_fs_resource). | |
-export ([ init/1 ]). | |
-export ([ allowed_methods/2, | |
resource_exists/2, | |
content_types_provided/2, | |
provide_content/2 | |
]). | |
-record (state, {root, filepath, is_dir}). | |
-include_lib ("kernel/include/file.hrl"). | |
-include_lib ("webmachine/include/webmachine.hrl"). | |
init (Config) -> | |
{root, Root} = proplists:lookup (root, Config), | |
{ok, #state { root = Root } }. | |
allowed_methods (ReqData, State) -> | |
{['GET'], ReqData, State}. | |
resource_exists(ReqData, State = #state { root = Root }) -> | |
FilePath = filename:join ([Root, wrq:disp_path(ReqData)]), | |
case filelib:is_dir (FilePath) of | |
true -> | |
{true, ReqData, State#state { filepath = FilePath, is_dir = true }}; | |
false -> | |
case filelib:is_regular (FilePath) of | |
true -> | |
{true, ReqData, State#state { filepath = FilePath, is_dir = false }}; | |
false -> | |
{false, ReqData, State} | |
end | |
end. | |
content_types_provided (ReqData, | |
State = #state { is_dir = IsDir }) -> | |
ContentType = | |
case IsDir of | |
true -> "text/html"; | |
_ -> webmachine_util:guess_mime (wrq:disp_path(ReqData)) | |
end, | |
{[{ContentType, provide_content}], ReqData, State}. | |
provide_content (ReqData, | |
State = #state { filepath = FilePath, is_dir = IsDir }) -> | |
ResponseContent = | |
case IsDir of | |
true -> | |
Path = wrq:disp_path (ReqData), | |
dir_to_html (Path, dir_contents (FilePath)); | |
false -> | |
file_contents (FilePath) | |
end, | |
{ ResponseContent, ReqData, State}. | |
file_contents (FilePath) -> | |
case file:read_file (FilePath) of | |
{ok, Bin} -> Bin; | |
_ -> error | |
end. | |
dir_to_html (Base, FilePath) -> | |
error_logger:info_msg ("Deal with dir ~p, ~p",[Base, FilePath]), | |
[ "<!DOCTYPE html><html><head><title>Index of ",Base,"</head><body>" | |
"<ul>", | |
[ ["<li><a href='",Base,"/",F,"'>",F,"</a>"] | |
|| F | |
<- dir_contents (FilePath) | |
], | |
"</ul>", | |
"</body></html>" | |
]. | |
dir_contents (FilePath) -> | |
case file:list_dir (FilePath) of | |
{ok, F} -> F; | |
{error, _} -> [] | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The crash I get.
=ERROR REPORT==== 7-Oct-2013::16:55:14 ===
webmachine error: path="/stats/raw/2013/10/02/ox_http_gateway/"
[{erlang,hd,[[]],[]},
{webmachine_decision_core,decision,1,
[{file,"webmachine_decision_core.erl"},{line,558}]},
{webmachine_decision_core,handle_request,2,
[{file,"webmachine_decision_core.erl"},{line,33}]},
{webmachine_mochiweb,loop,2,[{file,"src/webmachine_mochiweb.erl"},{line,71}]},
{mochiweb_http,headers,5,[{file,"src/mochiweb_http.erl"},{line,101}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,227}]}]