n2o_static.erl 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -module(n2o_static).
  2. -description('N4U Static Bridge to files in LING image, MAD bundle or OS').
  3. -compile([export_all, nowarn_export_all]).
  4. -include_lib("kernel/include/file.hrl").
  5. init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
  6. rest_init(Req, {dir, Path, Extra}) when is_binary(Path) -> rest_init(Req, {dir, binary_to_list(Path), Extra});
  7. rest_init(Req, {dir, Path, Extra}) ->
  8. {PathInfo, Req2} = cowboy_req:path_info(Req),
  9. Info = {ok, #file_info{type=regular,size=0}},
  10. FileName = filename:join([Path|PathInfo]),
  11. wf:info(?MODULE,"Rest Init: ~p~n\r",[FileName]),
  12. {ok, Req2, {FileName, Info, Extra}}.
  13. malformed_request(Req, State) -> {State =:= error, Req, State}.
  14. forbidden(Req, State={_, {ok, #file_info{type=directory}}, _}) -> {true, Req, State};
  15. forbidden(Req, State={_, {error, eacces}, _}) -> {true, Req, State};
  16. forbidden(Req, State={_, {ok, #file_info{access=Access}}, _}) when Access =:= write; Access =:= none -> {true, Req, State};
  17. forbidden(Req, State) -> {false, Req, State}.
  18. content_types_provided(Req, State={Path, _, Extra}) ->
  19. wf:info(?MODULE,"Content Type Provided: ~p~n\r",[Path]),
  20. case lists:keyfind(mimetypes, 1, Extra) of
  21. false -> {[{cow_mimetypes:web(Path), get_file}], Req, State};
  22. {mimetypes, Module, Function} -> {[{Module:Function(Path), get_file}], Req, State};
  23. {mimetypes, Type} -> {[{Type, get_file}], Req, State}
  24. end.
  25. resource_exists(Req, State={_, {ok, #file_info{type=regular}}, _}) -> {true, Req, State};
  26. resource_exists(Req, State) -> {false, Req, State}.
  27. generate_etag(Req, State={Path, {ok, #file_info{size=Size, mtime=Mtime}}, Extra}) ->
  28. case lists:keyfind(etag, 1, Extra) of
  29. false -> {generate_default_etag(Size, Mtime), Req, State};
  30. {etag, Module, Function} -> {Module:Function(Path, Size, Mtime), Req, State};
  31. {etag, false} -> {undefined, Req, State}
  32. end.
  33. generate_default_etag(Size, Mtime) ->
  34. {strong, list_to_binary(integer_to_list(
  35. erlang:phash2({Size, Mtime}, 16#ffffffff)))}.
  36. last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) -> {Modified, Req, State}.
  37. get_file(Req, State={Path, {ok, #file_info{size=_Size}}, _}) ->
  38. StringPath = wf:to_list(unicode:characters_to_binary(Path,utf8,utf8)),
  39. [_Type,Name|RestPath]=SplitPath = filename:split(StringPath),
  40. wf:info(?MODULE,"Split Path: ~p~n\r",[SplitPath]),
  41. %wf:info(?MODULE,"Code Path: ~p~n\r",[filename:join([code:lib_dir(Name)|RestPath])]),
  42. FileName = filename:absname(StringPath),
  43. wf:info(?MODULE,"Abs Name: ~p~n\r",[FileName]),
  44. Raw = case file:read_file(FileName) of
  45. {ok,Bin} -> Bin;
  46. {error,_} ->
  47. case mad_repl:load_file(StringPath) of
  48. {ok,ETSFile} -> ETSFile;
  49. {error,_} ->
  50. case file:read_file(filename:join([code:lib_dir(Name)|RestPath])) of
  51. {ok,ReleaseFile} -> ReleaseFile;
  52. {error,_} -> <<>> end end end,
  53. wf:info(?MODULE,"Cowboy Requested Static File: ~p~n\r ~p~n\r",[Raw,filename:absname(StringPath)]),
  54. Sendfile = fun (Socket, Transport) ->
  55. case Transport:send(Socket, Raw) of
  56. {ok, _} -> ok;
  57. {error, closed} -> ok;
  58. {error, etimedout} -> ok;
  59. _ -> ok end end,
  60. {{stream, size(Raw), Sendfile}, Req, State}.