diff options
Diffstat (limited to 'src/misc.erl')
-rw-r--r-- | src/misc.erl | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/src/misc.erl b/src/misc.erl index 06d81cb88..80824f03e 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -33,7 +33,8 @@ atom_to_binary/1, binary_to_atom/1, tuple_to_binary/1, l2i/1, i2l/1, i2l/2, expr_to_term/1, term_to_expr/1, now_to_usec/1, usec_to_now/1, encode_pid/1, decode_pid/2, - compile_exprs/2, join_atoms/2, try_read_file/1, have_eimp/0]). + compile_exprs/2, join_atoms/2, try_read_file/1, have_eimp/0, + css_dir/0, img_dir/0, js_dir/0, read_css/1, read_img/1, read_js/1]). %% Deprecated functions -export([decode_base64/1, encode_base64/1]). @@ -219,6 +220,51 @@ have_eimp() -> true. have_eimp() -> false. -endif. +-spec css_dir() -> file:filename(). +css_dir() -> + case os:getenv("EJABBERD_CSS_PATH") of + false -> + case code:priv_dir(ejabberd) of + {error, _} -> filename:join(["priv", "css"]); + Path -> filename:join([Path, "css"]) + end; + Path -> Path + end. + +-spec img_dir() -> file:filename(). +img_dir() -> + case os:getenv("EJABBERD_IMG_PATH") of + false -> + case code:priv_dir(ejabberd) of + {error, _} -> filename:join(["priv", "img"]); + Path -> filename:join([Path, "img"]) + end; + Path -> Path + end. + +-spec js_dir() -> file:filename(). +js_dir() -> + case os:getenv("EJABBERD_JS_PATH") of + false -> + case code:priv_dir(ejabberd) of + {error, _} -> filename:join(["priv", "js"]); + Path -> filename:join([Path, "js"]) + end; + Path -> Path + end. + +-spec read_css(file:filename()) -> {ok, binary()} | {error, file:posix()}. +read_css(File) -> + read_file(filename:join(css_dir(), File)). + +-spec read_img(file:filename()) -> {ok, binary()} | {error, file:posix()}. +read_img(File) -> + read_file(filename:join(img_dir(), File)). + +-spec read_js(file:filename()) -> {ok, binary()} | {error, file:posix()}. +read_js(File) -> + read_file(filename:join(js_dir(), File)). + %%%=================================================================== %%% Internal functions %%%=================================================================== @@ -230,3 +276,14 @@ set_node_id(PidStr, NodeBin) -> [H|_] = string:tokens(ExtPidStr, "."), [_|T] = string:tokens(PidStr, "."), erlang:list_to_pid(string:join([H|T], ".")). + +-spec read_file(file:filename()) -> {ok, binary()} | {error, file:posix()}. +read_file(Path) -> + case file:read_file(Path) of + {ok, Data} -> + {ok, Data}; + {error, Why} = Err -> + ?ERROR_MSG("Failed to read file ~s: ~s", + [Path, file:format_error(Why)]), + Err + end. |