nitro_static.erl 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. -module(nitro_static).
  2. -description('NITRO Static bridge for MAD containers').
  3. -author('Maxim Sokhatsky').
  4. -compile(export_all).
  5. -include_lib("kernel/include/file.hrl").
  6. init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
  7. rest_init(Req, {dir, Path, Extra}) when is_binary(Path) -> rest_init(Req, {dir, binary_to_list(Path), Extra});
  8. rest_init(Req, {dir, Path, Extra}) ->
  9. {PathInfo, Req2} = cowboy_req:path_info(Req),
  10. Info = {ok, #file_info{type=regular,size=0}},
  11. FileName = filename:join([Path|PathInfo]),
  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. fixpath(Path) -> {Module,Fun} = application:get_env(n2o,fixpath,{nitro_static,fix}),
  19. Module:Fun(Path).
  20. fix(A) -> A.
  21. content_types_provided(Req, State={Path, _, Extra}) ->
  22. case lists:keyfind(mimetypes, 1, Extra) of
  23. false -> {[{cow_mimetypes:web(Path), get_file}], Req, State};
  24. {mimetypes, Module, Function} ->
  25. {[{Module:Function(fixpath(Path)), get_file}], Req, State};
  26. {mimetypes, Type} -> {[{Type, get_file}], Req, State}
  27. end.
  28. resource_exists(Req, State={_, {ok, #file_info{type=regular}}, _}) -> {true, Req, State};
  29. resource_exists(Req, State) -> {false, Req, State}.
  30. generate_etag(Req, State={Path, {ok, #file_info{size=Size, mtime=Mtime}}, Extra}) ->
  31. case lists:keyfind(etag, 1, Extra) of
  32. false -> {generate_default_etag(Size, Mtime), Req, State};
  33. {etag, Module, Function} -> {Module:Function(Path, Size, Mtime), Req, State};
  34. {etag, false} -> {undefined, Req, State}
  35. end.
  36. generate_default_etag(Size, Mtime) ->
  37. {strong, list_to_binary(integer_to_list(erlang:phash2({Size, Mtime}, 16#ffffffff)))}.
  38. last_modified(Req, State={_, {ok, #file_info{mtime=Modified}}, _}) -> {Modified, Req, State}.
  39. get_file(Req, State={P, {ok, #file_info{size=_Size}}, _}) ->
  40. Path = fixpath(P),
  41. StringPath = nitro:to_list(unicode:characters_to_binary(Path,utf8,utf8)),
  42. [_Type,Name|RestPath] = filename:split(StringPath),
  43. FileName = filename:absname(StringPath),
  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. Sendfile = fun (Socket, Transport) -> Transport:send(Socket, Raw) end,
  54. {{stream, size(Raw), Sendfile}, Req, State}.