25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-24 16:23:40 +01:00

mod_http_upload: Also expand @HOST@ in 'docroot'

In some environments, it might be desirable to use separate document
roots for each virtual host.
This commit is contained in:
Holger Weiss 2016-02-20 20:13:30 +01:00
parent b971449f12
commit 44f581c3b5
2 changed files with 15 additions and 11 deletions

View File

@ -86,7 +86,8 @@
%% Utility functions. %% Utility functions.
-export([get_proc_name/2, -export([get_proc_name/2,
expand_home/1]). expand_home/1,
expand_host/2]).
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("ejabberd_http.hrl"). -include("ejabberd_http.hrl").
@ -274,6 +275,7 @@ init({ServerHost, Opts}) ->
fun(B) when is_boolean(B) -> B end, fun(B) when is_boolean(B) -> B end,
true), true),
DocRoot1 = expand_home(str:strip(DocRoot, right, $/)), DocRoot1 = expand_home(str:strip(DocRoot, right, $/)),
DocRoot2 = expand_host(DocRoot1, ServerHost),
case ServiceURL of case ServiceURL of
undefined -> undefined ->
ok; ok;
@ -290,7 +292,7 @@ init({ServerHost, Opts}) ->
undefined -> undefined ->
ok; ok;
Mode -> Mode ->
file:change_mode(DocRoot1, Mode) file:change_mode(DocRoot2, Mode)
end, end,
case Thumbnail of case Thumbnail of
true -> true ->
@ -310,7 +312,7 @@ init({ServerHost, Opts}) ->
secret_length = SecretLength, jid_in_url = JIDinURL, secret_length = SecretLength, jid_in_url = JIDinURL,
file_mode = FileMode, dir_mode = DirMode, file_mode = FileMode, dir_mode = DirMode,
thumbnail = Thumbnail, thumbnail = Thumbnail,
docroot = DocRoot1, docroot = DocRoot2,
put_url = expand_host(str:strip(PutURL, right, $/), ServerHost), put_url = expand_host(str:strip(PutURL, right, $/), ServerHost),
get_url = expand_host(str:strip(GetURL, right, $/), ServerHost), get_url = expand_host(str:strip(GetURL, right, $/), ServerHost),
service_url = ServiceURL}}. service_url = ServiceURL}}.
@ -510,6 +512,12 @@ expand_home(Subject) ->
Parts = binary:split(Subject, <<"@HOME@">>, [global]), Parts = binary:split(Subject, <<"@HOME@">>, [global]),
str:join(Parts, list_to_binary(Home)). str:join(Parts, list_to_binary(Home)).
-spec expand_host(binary(), binary()) -> binary().
expand_host(Subject, Host) ->
Parts = binary:split(Subject, <<"@HOST@">>, [global]),
str:join(Parts, Host).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal functions. %% Internal functions.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -738,12 +746,6 @@ map_int_to_char(N) when N =< 9 -> N + 48; % Digit.
map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character. map_int_to_char(N) when N =< 35 -> N + 55; % Upper-case character.
map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character. map_int_to_char(N) when N =< 61 -> N + 61. % Lower-case character.
-spec expand_host(binary(), binary()) -> binary().
expand_host(Subject, Host) ->
Parts = binary:split(Subject, <<"@HOST@">>, [global]),
str:join(Parts, Host).
-spec yield_content_type(binary()) -> binary(). -spec yield_content_type(binary()) -> binary().
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE; yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
@ -975,8 +977,9 @@ remove_user(User, Server) ->
(node) -> node (node) -> node
end, end,
sha1), sha1),
DocRoot1 = expand_host(expand_home(DocRoot), ServerHost),
UserStr = make_user_string(jid:make(User, Server, <<"">>), JIDinURL), UserStr = make_user_string(jid:make(User, Server, <<"">>), JIDinURL),
UserDir = str:join([expand_home(DocRoot), UserStr], <<$/>>), UserDir = str:join([DocRoot1, UserStr], <<$/>>),
case del_tree(UserDir) of case del_tree(UserDir) of
ok -> ok ->
?INFO_MSG("Removed HTTP upload directory of ~s@~s", [User, Server]); ?INFO_MSG("Removed HTTP upload directory of ~s@~s", [User, Server]);

View File

@ -132,6 +132,7 @@ init({ServerHost, Opts}) ->
fun iolist_to_binary/1, fun iolist_to_binary/1,
<<"@HOME@/upload">>), <<"@HOME@/upload">>),
DocRoot2 = mod_http_upload:expand_home(str:strip(DocRoot1, right, $/)), DocRoot2 = mod_http_upload:expand_home(str:strip(DocRoot1, right, $/)),
DocRoot3 = mod_http_upload:expand_host(DocRoot2, ServerHost),
Timers = if MaxDays == infinity -> []; Timers = if MaxDays == infinity -> [];
true -> true ->
{ok, T1} = timer:send_after(?INITIAL_TIMEOUT, sweep), {ok, T1} = timer:send_after(?INITIAL_TIMEOUT, sweep),
@ -144,7 +145,7 @@ init({ServerHost, Opts}) ->
access_soft_quota = AccessSoftQuota, access_soft_quota = AccessSoftQuota,
access_hard_quota = AccessHardQuota, access_hard_quota = AccessHardQuota,
max_days = MaxDays, max_days = MaxDays,
docroot = DocRoot2, docroot = DocRoot3,
timers = Timers}}. timers = Timers}}.
-spec handle_call(_, {pid(), _}, state()) -> {noreply, state()}. -spec handle_call(_, {pid(), _}, state()) -> {noreply, state()}.