mod_http_upload: Make case indentation consistent

Use four spaces (or the corresponding amount of tab characters) for case
indentation everywhere in mod_http_upload and mod_http_upload_quota.
This commit is contained in:
Holger Weiss 2015-11-09 19:23:52 +01:00
parent 0a24936e92
commit ea4964dccd
2 changed files with 300 additions and 292 deletions

View File

@ -118,12 +118,13 @@ start(ServerHost, Opts) ->
case gen_mod:get_opt(rm_on_unregister, Opts, case gen_mod:get_opt(rm_on_unregister, Opts,
fun(B) when is_boolean(B) -> B end, fun(B) when is_boolean(B) -> B end,
true) of true) of
true -> true ->
ejabberd_hooks:add(remove_user, ServerHost, ?MODULE, ejabberd_hooks:add(remove_user, ServerHost, ?MODULE,
remove_user, 50), remove_user, 50),
ejabberd_hooks:add(anonymous_purge_hook, ServerHost, ?MODULE, ejabberd_hooks:add(anonymous_purge_hook, ServerHost, ?MODULE,
remove_user, 50); remove_user, 50);
false -> ok false ->
ok
end, end,
Proc = get_proc_name(ServerHost, ?PROCNAME), Proc = get_proc_name(ServerHost, ?PROCNAME),
Spec = {Proc, Spec = {Proc,
@ -140,12 +141,13 @@ stop(ServerHost) ->
case gen_mod:get_module_opt(ServerHost, ?MODULE, rm_on_unregister, case gen_mod:get_module_opt(ServerHost, ?MODULE, rm_on_unregister,
fun(B) when is_boolean(B) -> B end, fun(B) when is_boolean(B) -> B end,
true) of true) of
true -> true ->
ejabberd_hooks:delete(remove_user, ServerHost, ?MODULE, ejabberd_hooks:delete(remove_user, ServerHost, ?MODULE,
remove_user, 50), remove_user, 50),
ejabberd_hooks:delete(anonymous_purge_hook, ServerHost, ?MODULE, ejabberd_hooks:delete(anonymous_purge_hook, ServerHost, ?MODULE,
remove_user, 50); remove_user, 50);
false -> ok false ->
ok
end, end,
Proc = get_proc_name(ServerHost, ?PROCNAME), Proc = get_proc_name(ServerHost, ?PROCNAME),
supervisor:terminate_child(ejabberd_sup, Proc), supervisor:terminate_child(ejabberd_sup, Proc),
@ -255,34 +257,34 @@ init({ServerHost, Opts}) ->
fun(B) when is_boolean(B) -> B end, fun(B) when is_boolean(B) -> B end,
true), true),
case ServiceURL of case ServiceURL of
undefined -> undefined ->
ok; ok;
<<"http://", _/binary>> -> <<"http://", _/binary>> ->
application:start(inets); application:start(inets);
<<"https://", _/binary>> -> <<"https://", _/binary>> ->
application:start(inets), application:start(inets),
application:start(crypto), application:start(crypto),
application:start(asn1), application:start(asn1),
application:start(public_key), application:start(public_key),
application:start(ssl) application:start(ssl)
end, end,
case DirMode of case DirMode of
undefined -> undefined ->
ok; ok;
Mode -> Mode ->
file:change_mode(DocRoot, Mode) file:change_mode(DocRoot, Mode)
end, end,
case Thumbnail of case Thumbnail of
true -> true ->
case string:str(os:cmd("identify"), "Magick") of case string:str(os:cmd("identify"), "Magick") of
0 -> 0 ->
?ERROR_MSG("Cannot find 'identify' command, please install " ?ERROR_MSG("Cannot find 'identify' command, please install "
"ImageMagick or disable thumbnail creation", []); "ImageMagick or disable thumbnail creation", []);
_ -> _ ->
ok ok
end; end;
false -> false ->
ok ok
end, end,
ejabberd_router:register_route(Host), ejabberd_router:register_route(Host),
{ok, #state{server_host = ServerHost, host = Host, name = Name, {ok, #state{server_host = ServerHost, host = Host, name = Name,
@ -307,14 +309,14 @@ handle_call({use_slot, Slot}, _From, #state{file_mode = FileMode,
thumbnail = Thumbnail, thumbnail = Thumbnail,
docroot = DocRoot} = State) -> docroot = DocRoot} = State) ->
case get_slot(Slot, State) of case get_slot(Slot, State) of
{ok, {Size, Timer}} -> {ok, {Size, Timer}} ->
timer:cancel(Timer), timer:cancel(Timer),
NewState = del_slot(Slot, State), NewState = del_slot(Slot, State),
Path = str:join([DocRoot | Slot], <<$/>>), Path = str:join([DocRoot | Slot], <<$/>>),
{reply, {ok, Size, Path, FileMode, DirMode, GetPrefix, Thumbnail}, {reply, {ok, Size, Path, FileMode, DirMode, GetPrefix, Thumbnail},
NewState}; NewState};
error -> error ->
{reply, {error, <<"Invalid slot">>}, State} {reply, {error, <<"Invalid slot">>}, State}
end; end;
handle_call(get_docroot, _From, #state{docroot = DocRoot} = State) -> handle_call(get_docroot, _From, #state{docroot = DocRoot} = State) ->
{reply, {ok, DocRoot}, State}; {reply, {ok, DocRoot}, State};
@ -333,12 +335,12 @@ handle_cast(Request, State) ->
handle_info({route, From, To, #xmlel{name = <<"iq">>} = Stanza}, State) -> handle_info({route, From, To, #xmlel{name = <<"iq">>} = Stanza}, State) ->
Request = jlib:iq_query_info(Stanza), Request = jlib:iq_query_info(Stanza),
{Reply, NewState} = case process_iq(From, Request, State) of {Reply, NewState} = case process_iq(From, Request, State) of
R when is_record(R, iq) -> R when is_record(R, iq) ->
{R, State}; {R, State};
{R, S} -> {R, S} ->
{R, S}; {R, S};
not_request -> not_request ->
{none, State} {none, State}
end, end,
if Reply /= none -> if Reply /= none ->
ejabberd_router:route(To, From, jlib:iq_to_xml(Reply)); ejabberd_router:route(To, From, jlib:iq_to_xml(Reply));
@ -377,77 +379,77 @@ process(LocalPath, #request{method = 'PUT', host = Host, ip = IP,
data = Data}) -> data = Data}) ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME), Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
case catch gen_server:call(Proc, {use_slot, LocalPath}) of case catch gen_server:call(Proc, {use_slot, LocalPath}) of
{ok, Size, Path, FileMode, DirMode, GetPrefix, Thumbnail} {ok, Size, Path, FileMode, DirMode, GetPrefix, Thumbnail}
when byte_size(Data) == Size -> when byte_size(Data) == Size ->
?DEBUG("Storing file from ~s for ~s: ~s", ?DEBUG("Storing file from ~s for ~s: ~s",
[?ADDR_TO_STR(IP), Host, Path]), [?ADDR_TO_STR(IP), Host, Path]),
case store_file(Path, Data, FileMode, DirMode, case store_file(Path, Data, FileMode, DirMode,
GetPrefix, LocalPath, Thumbnail) of GetPrefix, LocalPath, Thumbnail) of
ok -> ok ->
http_response(Host, 201); http_response(Host, 201);
{ok, Headers, OutData} -> {ok, Headers, OutData} ->
http_response(Host, 201, Headers, OutData); http_response(Host, 201, Headers, OutData);
{error, Error} -> {error, Error} ->
?ERROR_MSG("Cannot store file ~s from ~s for ~s: ~p", ?ERROR_MSG("Cannot store file ~s from ~s for ~s: ~p",
[Path, ?ADDR_TO_STR(IP), Host, Error]), [Path, ?ADDR_TO_STR(IP), Host, Error]),
http_response(Host, 500) http_response(Host, 500)
end; end;
{ok, Size, Path} -> {ok, Size, Path} ->
?INFO_MSG("Rejecting file ~s from ~s for ~s: Size is ~B, not ~B", ?INFO_MSG("Rejecting file ~s from ~s for ~s: Size is ~B, not ~B",
[Path, ?ADDR_TO_STR(IP), Host, byte_size(Data), Size]), [Path, ?ADDR_TO_STR(IP), Host, byte_size(Data), Size]),
http_response(Host, 413); http_response(Host, 413);
{error, Error} -> {error, Error} ->
?INFO_MSG("Rejecting file from ~s for ~s: ~p", ?INFO_MSG("Rejecting file from ~s for ~s: ~p",
[?ADDR_TO_STR(IP), Host, Error]), [?ADDR_TO_STR(IP), Host, Error]),
http_response(Host, 403); http_response(Host, 403);
Error -> Error ->
?ERROR_MSG("Cannot handle PUT request from ~s for ~s: ~p", ?ERROR_MSG("Cannot handle PUT request from ~s for ~s: ~p",
[?ADDR_TO_STR(IP), Host, Error]), [?ADDR_TO_STR(IP), Host, Error]),
http_response(Host, 500) http_response(Host, 500)
end; end;
process(LocalPath, #request{method = Method, host = Host, ip = IP}) process(LocalPath, #request{method = Method, host = Host, ip = IP})
when Method == 'GET'; when Method == 'GET';
Method == 'HEAD' -> Method == 'HEAD' ->
Proc = gen_mod:get_module_proc(Host, ?PROCNAME), Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
case catch gen_server:call(Proc, get_docroot) of case catch gen_server:call(Proc, get_docroot) of
{ok, DocRoot} -> {ok, DocRoot} ->
Path = str:join([DocRoot | LocalPath], <<$/>>), Path = str:join([DocRoot | LocalPath], <<$/>>),
case file:read_file(Path) of case file:read_file(Path) of
{ok, Data} -> {ok, Data} ->
?INFO_MSG("Serving ~s to ~s", [Path, ?ADDR_TO_STR(IP)]), ?INFO_MSG("Serving ~s to ~s", [Path, ?ADDR_TO_STR(IP)]),
FileName = lists:last(LocalPath), FileName = lists:last(LocalPath),
ContentType = guess_content_type(FileName), ContentType = guess_content_type(FileName),
Headers1 = case ContentType of Headers1 = case ContentType of
<<"image/", _SubType/binary>> -> []; <<"image/", _SubType/binary>> -> [];
<<"text/", _SubType/binary>> -> []; <<"text/", _SubType/binary>> -> [];
_ -> _ ->
[{<<"Content-Disposition">>, [{<<"Content-Disposition">>,
<<"attachment; filename=", <<"attachment; filename=",
$", FileName/binary, $">>}] $", FileName/binary, $">>}]
end, end,
Headers2 = [{<<"Content-Type">>, ContentType} | Headers1], Headers2 = [{<<"Content-Type">>, ContentType} | Headers1],
http_response(Host, 200, Headers2, Data); http_response(Host, 200, Headers2, Data);
{error, eacces} -> {error, eacces} ->
?INFO_MSG("Cannot serve ~s to ~s: Permission denied", ?INFO_MSG("Cannot serve ~s to ~s: Permission denied",
[Path, ?ADDR_TO_STR(IP)]), [Path, ?ADDR_TO_STR(IP)]),
http_response(Host, 403); http_response(Host, 403);
{error, enoent} -> {error, enoent} ->
?INFO_MSG("Cannot serve ~s to ~s: No such file", ?INFO_MSG("Cannot serve ~s to ~s: No such file",
[Path, ?ADDR_TO_STR(IP)]), [Path, ?ADDR_TO_STR(IP)]),
http_response(Host, 404); http_response(Host, 404);
{error, eisdir} -> {error, eisdir} ->
?INFO_MSG("Cannot serve ~s to ~s: Is a directory", ?INFO_MSG("Cannot serve ~s to ~s: Is a directory",
[Path, ?ADDR_TO_STR(IP)]), [Path, ?ADDR_TO_STR(IP)]),
http_response(Host, 404); http_response(Host, 404);
{error, Error} -> {error, Error} ->
?INFO_MSG("Cannot serve ~s to ~s: ~s", ?INFO_MSG("Cannot serve ~s to ~s: ~s",
[Path, ?ADDR_TO_STR(IP), ?FORMAT(Error)]), [Path, ?ADDR_TO_STR(IP), ?FORMAT(Error)]),
http_response(Host, 500) http_response(Host, 500)
end; end;
Error -> Error ->
?ERROR_MSG("Cannot handle ~s request from ~s for ~s: ~p", ?ERROR_MSG("Cannot handle ~s request from ~s for ~s: ~p",
[Method, ?ADDR_TO_STR(IP), Host, Error]), [Method, ?ADDR_TO_STR(IP), Host, Error]),
http_response(Host, 500) http_response(Host, 500)
end; end;
process(_LocalPath, #request{method = 'OPTIONS', host = Host, ip = IP}) -> process(_LocalPath, #request{method = 'OPTIONS', host = Host, ip = IP}) ->
?DEBUG("Responding to OPTIONS request from ~s for ~s", ?DEBUG("Responding to OPTIONS request from ~s for ~s",
@ -507,31 +509,33 @@ process_iq(From,
when XMLNS == ?NS_HTTP_UPLOAD; when XMLNS == ?NS_HTTP_UPLOAD;
XMLNS == ?NS_HTTP_UPLOAD_OLD -> XMLNS == ?NS_HTTP_UPLOAD_OLD ->
case acl:match_rule(ServerHost, Access, From) of case acl:match_rule(ServerHost, Access, From) of
allow -> allow ->
case parse_request(SubEl, Lang) of case parse_request(SubEl, Lang) of
{ok, File, Size, ContentType} -> {ok, File, Size, ContentType} ->
case create_slot(State, From, File, Size, ContentType, Lang) of case create_slot(State, From, File, Size, ContentType,
{ok, Slot} -> Lang) of
{ok, Timer} = timer:send_after(?SLOT_TIMEOUT, {ok, Slot} ->
{slot_timed_out, Slot}), {ok, Timer} = timer:send_after(?SLOT_TIMEOUT,
NewState = add_slot(Slot, Size, Timer, State), {slot_timed_out,
SlotEl = slot_el(Slot, State, XMLNS), Slot}),
{IQ#iq{type = result, sub_el = [SlotEl]}, NewState}; NewState = add_slot(Slot, Size, Timer, State),
{ok, PutURL, GetURL} -> SlotEl = slot_el(Slot, State, XMLNS),
SlotEl = slot_el(PutURL, GetURL, XMLNS), {IQ#iq{type = result, sub_el = [SlotEl]}, NewState};
IQ#iq{type = result, sub_el = [SlotEl]}; {ok, PutURL, GetURL} ->
{error, Error} -> SlotEl = slot_el(PutURL, GetURL, XMLNS),
IQ#iq{type = error, sub_el = [SubEl, Error]} IQ#iq{type = result, sub_el = [SlotEl]};
end; {error, Error} ->
{error, Error} -> IQ#iq{type = error, sub_el = [SubEl, Error]}
?DEBUG("Cannot parse request from ~s", end;
[jlib:jid_to_string(From)]), {error, Error} ->
IQ#iq{type = error, sub_el = [SubEl, Error]} ?DEBUG("Cannot parse request from ~s",
end; [jlib:jid_to_string(From)]),
deny -> IQ#iq{type = error, sub_el = [SubEl, Error]}
?DEBUG("Denying HTTP upload slot request from ~s", end;
[jlib:jid_to_string(From)]), deny ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]} ?DEBUG("Denying HTTP upload slot request from ~s",
[jlib:jid_to_string(From)]),
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]}
end; end;
process_iq(_From, #iq{sub_el = SubEl} = IQ, _State) -> process_iq(_From, #iq{sub_el = SubEl} = IQ, _State) ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}; IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
@ -545,25 +549,25 @@ process_iq(_From, invalid, _State) ->
parse_request(#xmlel{name = <<"request">>, attrs = Attrs} = Request, Lang) -> parse_request(#xmlel{name = <<"request">>, attrs = Attrs} = Request, Lang) ->
case xml:get_attr(<<"xmlns">>, Attrs) of case xml:get_attr(<<"xmlns">>, Attrs) of
{value, XMLNS} when XMLNS == ?NS_HTTP_UPLOAD; {value, XMLNS} when XMLNS == ?NS_HTTP_UPLOAD;
XMLNS == ?NS_HTTP_UPLOAD_OLD -> XMLNS == ?NS_HTTP_UPLOAD_OLD ->
case {xml:get_subtag_cdata(Request, <<"filename">>), case {xml:get_subtag_cdata(Request, <<"filename">>),
xml:get_subtag_cdata(Request, <<"size">>), xml:get_subtag_cdata(Request, <<"size">>),
xml:get_subtag_cdata(Request, <<"content-type">>)} of xml:get_subtag_cdata(Request, <<"content-type">>)} of
{File, SizeStr, ContentType} when byte_size(File) > 0 -> {File, SizeStr, ContentType} when byte_size(File) > 0 ->
case catch jlib:binary_to_integer(SizeStr) of case catch jlib:binary_to_integer(SizeStr) of
Size when is_integer(Size), Size > 0 -> Size when is_integer(Size), Size > 0 ->
{ok, File, Size, yield_content_type(ContentType)}; {ok, File, Size, yield_content_type(ContentType)};
_ -> _ ->
Text = <<"Please specify file size.">>, Text = <<"Please specify file size.">>,
{error, ?ERRT_BAD_REQUEST(Lang, Text)} {error, ?ERRT_BAD_REQUEST(Lang, Text)}
end; end;
_ -> _ ->
Text = <<"Please specify file name.">>, Text = <<"Please specify file name.">>,
{error, ?ERRT_BAD_REQUEST(Lang, Text)} {error, ?ERRT_BAD_REQUEST(Lang, Text)}
end; end;
_ -> _ ->
{error, ?ERR_BAD_REQUEST} {error, ?ERR_BAD_REQUEST}
end; end;
parse_request(_El, _Lang) -> {error, ?ERR_BAD_REQUEST}. parse_request(_El, _Lang) -> {error, ?ERR_BAD_REQUEST}.
@ -588,16 +592,16 @@ create_slot(#state{service_url = undefined,
UserDir = <<DocRoot/binary, $/, UserStr/binary>>, UserDir = <<DocRoot/binary, $/, UserStr/binary>>,
case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow, case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow,
[JID, UserDir, Size, Lang]) of [JID, UserDir, Size, Lang]) of
allow -> allow ->
RandStr = make_rand_string(SecretLength), RandStr = make_rand_string(SecretLength),
FileStr = make_file_string(File), FileStr = make_file_string(File),
?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)", ?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)",
[jlib:jid_to_string(JID), File]), [jlib:jid_to_string(JID), File]),
{ok, [UserStr, RandStr, FileStr]}; {ok, [UserStr, RandStr, FileStr]};
deny -> deny ->
{error, ?ERR_SERVICE_UNAVAILABLE}; {error, ?ERR_SERVICE_UNAVAILABLE};
#xmlel{} = Error -> #xmlel{} = Error ->
{error, Error} {error, Error}
end; end;
create_slot(#state{service_url = ServiceURL}, create_slot(#state{service_url = ServiceURL},
#jid{luser = U, lserver = S} = JID, File, Size, ContentType, #jid{luser = U, lserver = S} = JID, File, Size, ContentType,
@ -611,37 +615,38 @@ create_slot(#state{service_url = ServiceURL},
"&size=" ++ ?URL_ENC(SizeStr) ++ "&size=" ++ ?URL_ENC(SizeStr) ++
"&content_type=" ++ ?URL_ENC(ContentType), "&content_type=" ++ ?URL_ENC(ContentType),
case httpc:request(get, {GetRequest, []}, HttpOptions, Options) of case httpc:request(get, {GetRequest, []}, HttpOptions, Options) of
{ok, {Code, Body}} when Code >= 200, Code =< 299 -> {ok, {Code, Body}} when Code >= 200, Code =< 299 ->
case binary:split(Body, <<$\n>>, [global, trim]) of case binary:split(Body, <<$\n>>, [global, trim]) of
[<<"http", _/binary>> = PutURL, <<"http", _/binary>> = GetURL] -> [<<"http", _/binary>> = PutURL,
?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)", <<"http", _/binary>> = GetURL] ->
[jlib:jid_to_string(JID), File]), ?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)",
{ok, PutURL, GetURL}; [jlib:jid_to_string(JID), File]),
Lines -> {ok, PutURL, GetURL};
?ERROR_MSG("Can't parse data received for ~s from <~s>: ~p", Lines ->
[jlib:jid_to_string(JID), ServiceURL, Lines]), ?ERROR_MSG("Can't parse data received for ~s from <~s>: ~p",
{error, ?ERR_SERVICE_UNAVAILABLE} [jlib:jid_to_string(JID), ServiceURL, Lines]),
end; {error, ?ERR_SERVICE_UNAVAILABLE}
{ok, {402, _Body}} -> end;
?INFO_MSG("Got status code 402 for ~s from <~s>", {ok, {402, _Body}} ->
[jlib:jid_to_string(JID), ServiceURL]), ?INFO_MSG("Got status code 402 for ~s from <~s>",
{error, ?ERR_RESOURCE_CONSTRAINT}; [jlib:jid_to_string(JID), ServiceURL]),
{ok, {403, _Body}} -> {error, ?ERR_RESOURCE_CONSTRAINT};
?INFO_MSG("Got status code 403 for ~s from <~s>", {ok, {403, _Body}} ->
[jlib:jid_to_string(JID), ServiceURL]), ?INFO_MSG("Got status code 403 for ~s from <~s>",
{error, ?ERR_NOT_ALLOWED}; [jlib:jid_to_string(JID), ServiceURL]),
{ok, {413, _Body}} -> {error, ?ERR_NOT_ALLOWED};
?INFO_MSG("Got status code 413 for ~s from <~s>", {ok, {413, _Body}} ->
[jlib:jid_to_string(JID), ServiceURL]), ?INFO_MSG("Got status code 413 for ~s from <~s>",
{error, ?ERR_NOT_ACCEPTABLE}; [jlib:jid_to_string(JID), ServiceURL]),
{ok, {Code, _Body}} -> {error, ?ERR_NOT_ACCEPTABLE};
?ERROR_MSG("Got unexpected status code for ~s from <~s>: ~B", {ok, {Code, _Body}} ->
[jlib:jid_to_string(JID), ServiceURL, Code]), ?ERROR_MSG("Got unexpected status code for ~s from <~s>: ~B",
{error, ?ERR_SERVICE_UNAVAILABLE}; [jlib:jid_to_string(JID), ServiceURL, Code]),
{error, Reason} -> {error, ?ERR_SERVICE_UNAVAILABLE};
?ERROR_MSG("Error requesting upload slot for ~s from <~s>: ~p", {error, Reason} ->
[jlib:jid_to_string(JID), ServiceURL, Reason]), ?ERROR_MSG("Error requesting upload slot for ~s from <~s>: ~p",
{error, ?ERR_SERVICE_UNAVAILABLE} [jlib:jid_to_string(JID), ServiceURL, Reason]),
{error, ?ERR_SERVICE_UNAVAILABLE}
end. end.
-spec add_slot(slot(), pos_integer(), timer:tref(), state()) -> state(). -spec add_slot(slot(), pos_integer(), timer:tref(), state()) -> state().
@ -793,10 +798,10 @@ do_store_file(Path, Data, FileMode, DirMode) ->
end, end,
ok = Ok % Raise an exception if file:write/2 failed. ok = Ok % Raise an exception if file:write/2 failed.
catch catch
_:{badmatch, {error, Error}} -> _:{badmatch, {error, Error}} ->
{error, ?FORMAT(Error)}; {error, ?FORMAT(Error)};
_:Error -> _:Error ->
{error, Error} {error, Error}
end. end.
-spec guess_content_type(binary()) -> binary(). -spec guess_content_type(binary()) -> binary().
@ -834,11 +839,11 @@ http_response(Host, Code, ExtraHeaders, Body) ->
end, end,
[]), []),
Headers = case proplists:is_defined(<<"Content-Type">>, ExtraHeaders) of Headers = case proplists:is_defined(<<"Content-Type">>, ExtraHeaders) of
true -> true ->
[ServerHeader | ExtraHeaders]; [ServerHeader | ExtraHeaders];
false -> false ->
[ServerHeader, {<<"Content-Type">>, <<"text/plain">>} | [ServerHeader, {<<"Content-Type">>, <<"text/plain">>} |
ExtraHeaders] ExtraHeaders]
end ++ CustomHeaders, end ++ CustomHeaders,
{Code, Headers, Body}. {Code, Headers, Body}.
@ -954,16 +959,16 @@ del_tree(Dir) ->
{ok, Entries} = file:list_dir(Dir), {ok, Entries} = file:list_dir(Dir),
lists:foreach(fun(Path) -> lists:foreach(fun(Path) ->
case filelib:is_dir(Path) of case filelib:is_dir(Path) of
true -> true ->
ok = del_tree(Path); ok = del_tree(Path);
false -> false ->
ok = file:delete(Path) ok = file:delete(Path)
end end
end, [Dir ++ "/" ++ Entry || Entry <- Entries]), end, [Dir ++ "/" ++ Entry || Entry <- Entries]),
ok = file:del_dir(Dir) ok = file:del_dir(Dir)
catch catch
_:{badmatch, {error, Error}} -> _:{badmatch, {error, Error}} ->
{error, ?FORMAT(Error)}; {error, ?FORMAT(Error)};
_:Error -> _:Error ->
{error, Error} {error, Error}
end. end.

View File

@ -143,42 +143,42 @@ handle_cast({handle_slot_request, #jid{user = U, server = S} = JID, Path, Size},
access_hard_quota = AccessHardQuota, access_hard_quota = AccessHardQuota,
disk_usage = DiskUsage} = State) -> disk_usage = DiskUsage} = State) ->
HardQuota = case acl:match_rule(ServerHost, AccessHardQuota, JID) of HardQuota = case acl:match_rule(ServerHost, AccessHardQuota, JID) of
Hard when is_integer(Hard), Hard > 0 -> Hard when is_integer(Hard), Hard > 0 ->
Hard * 1024 * 1024; Hard * 1024 * 1024;
_ -> _ ->
0 0
end, end,
SoftQuota = case acl:match_rule(ServerHost, AccessSoftQuota, JID) of SoftQuota = case acl:match_rule(ServerHost, AccessSoftQuota, JID) of
Soft when is_integer(Soft), Soft > 0 -> Soft when is_integer(Soft), Soft > 0 ->
Soft * 1024 * 1024; Soft * 1024 * 1024;
_ -> _ ->
0 0
end, end,
OldSize = case dict:find({U, S}, DiskUsage) of OldSize = case dict:find({U, S}, DiskUsage) of
{ok, Value} -> {ok, Value} ->
Value; Value;
error -> error ->
undefined undefined
end, end,
NewSize = case {HardQuota, SoftQuota} of NewSize = case {HardQuota, SoftQuota} of
{0, 0} -> {0, 0} ->
?DEBUG("No quota specified for ~s", ?DEBUG("No quota specified for ~s",
[jlib:jid_to_string(JID)]), [jlib:jid_to_string(JID)]),
Size; Size;
{0, _} -> {0, _} ->
?WARNING_MSG("No hard quota specified for ~s", ?WARNING_MSG("No hard quota specified for ~s",
[jlib:jid_to_string(JID)]), [jlib:jid_to_string(JID)]),
enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota); enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota);
{_, 0} -> {_, 0} ->
?WARNING_MSG("No soft quota specified for ~s", ?WARNING_MSG("No soft quota specified for ~s",
[jlib:jid_to_string(JID)]), [jlib:jid_to_string(JID)]),
enforce_quota(Path, Size, OldSize, HardQuota, HardQuota); enforce_quota(Path, Size, OldSize, HardQuota, HardQuota);
_ when SoftQuota > HardQuota -> _ when SoftQuota > HardQuota ->
?WARNING_MSG("Bad quota for ~s (soft: ~p, hard: ~p)", ?WARNING_MSG("Bad quota for ~s (soft: ~p, hard: ~p)",
[jlib:jid_to_string(JID), [jlib:jid_to_string(JID),
SoftQuota, HardQuota]), SoftQuota, HardQuota]),
enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota); enforce_quota(Path, Size, OldSize, SoftQuota, SoftQuota);
_ -> _ ->
?DEBUG("Enforcing quota for ~s", ?DEBUG("Enforcing quota for ~s",
[jlib:jid_to_string(JID)]), [jlib:jid_to_string(JID)]),
enforce_quota(Path, Size, OldSize, SoftQuota, HardQuota) enforce_quota(Path, Size, OldSize, SoftQuota, HardQuota)
@ -196,17 +196,19 @@ handle_info(sweep, #state{server_host = ServerHost,
when is_integer(MaxDays), MaxDays > 0 -> when is_integer(MaxDays), MaxDays > 0 ->
?DEBUG("Got 'sweep' message for ~s", [ServerHost]), ?DEBUG("Got 'sweep' message for ~s", [ServerHost]),
case file:list_dir(DocRoot) of case file:list_dir(DocRoot) of
{ok, Entries} -> {ok, Entries} ->
BackThen = secs_since_epoch() - (MaxDays * 86400), BackThen = secs_since_epoch() - (MaxDays * 86400),
DocRootS = binary_to_list(DocRoot), DocRootS = binary_to_list(DocRoot),
PathNames = lists:map(fun(Entry) -> DocRootS ++ "/" ++ Entry end, PathNames = lists:map(fun(Entry) ->
Entries), DocRootS ++ "/" ++ Entry
UserDirs = lists:filter(fun filelib:is_dir/1, PathNames), end, Entries),
lists:foreach(fun(UserDir) -> delete_old_files(UserDir, BackThen) end, UserDirs = lists:filter(fun filelib:is_dir/1, PathNames),
UserDirs); lists:foreach(fun(UserDir) ->
{error, Error} -> delete_old_files(UserDir, BackThen)
?ERROR_MSG("Cannot open document root ~s: ~s", end, UserDirs);
[DocRoot, ?FORMAT(Error)]) {error, Error} ->
?ERROR_MSG("Cannot open document root ~s: ~s",
[DocRoot, ?FORMAT(Error)])
end, end,
{noreply, State}; {noreply, State};
handle_info(Info, State) -> handle_info(Info, State) ->
@ -276,14 +278,14 @@ enforce_quota(UserDir, SlotSize, _OldSize, MinSize, MaxSize) ->
-spec delete_old_files(file:filename_all(), integer()) -> ok. -spec delete_old_files(file:filename_all(), integer()) -> ok.
delete_old_files(UserDir, Timestamp) -> delete_old_files(UserDir, CutOff) ->
FileInfo = gather_file_info(UserDir), FileInfo = gather_file_info(UserDir),
case [Path || {Path, _Size, Time} <- FileInfo, Time < Timestamp] of case [Path || {Path, _Size, Time} <- FileInfo, Time < CutOff] of
[] -> [] ->
ok; ok;
OldFiles -> OldFiles ->
lists:foreach(fun(File) -> del_file_and_dir(File) end, OldFiles), lists:foreach(fun(File) -> del_file_and_dir(File) end, OldFiles),
file:del_dir(UserDir) % In case it's empty, now. file:del_dir(UserDir) % In case it's empty, now.
end. end.
-spec gather_file_info(file:filename_all()) -spec gather_file_info(file:filename_all())
@ -293,49 +295,50 @@ gather_file_info(Dir) when is_binary(Dir) ->
gather_file_info(binary_to_list(Dir)); gather_file_info(binary_to_list(Dir));
gather_file_info(Dir) -> gather_file_info(Dir) ->
case file:list_dir(Dir) of case file:list_dir(Dir) of
{ok, Entries} -> {ok, Entries} ->
lists:foldl(fun(Entry, Acc) -> lists:foldl(fun(Entry, Acc) ->
Path = Dir ++ "/" ++ Entry, Path = Dir ++ "/" ++ Entry,
case file:read_file_info(Path, [{time, posix}]) of case file:read_file_info(Path,
{ok, #file_info{type = directory}} -> [{time, posix}]) of
gather_file_info(Path) ++ Acc; {ok, #file_info{type = directory}} ->
{ok, #file_info{type = regular, gather_file_info(Path) ++ Acc;
mtime = Time, {ok, #file_info{type = regular,
size = Size}} -> mtime = Time,
[{Path, Size, Time} | Acc]; size = Size}} ->
{ok, _Info} -> [{Path, Size, Time} | Acc];
?DEBUG("Won't stat(2) non-regular file ~s", {ok, _Info} ->
[Path]), ?DEBUG("Won't stat(2) non-regular file ~s",
Acc; [Path]),
{error, Error} -> Acc;
?ERROR_MSG("Cannot stat(2) ~s: ~s", {error, Error} ->
[Path, ?FORMAT(Error)]), ?ERROR_MSG("Cannot stat(2) ~s: ~s",
Acc [Path, ?FORMAT(Error)]),
end Acc
end, [], Entries); end
{error, enoent} -> end, [], Entries);
?DEBUG("Directory ~s doesn't exist", [Dir]), {error, enoent} ->
[]; ?DEBUG("Directory ~s doesn't exist", [Dir]),
{error, Error} -> [];
?ERROR_MSG("Cannot open directory ~s: ~s", [Dir, ?FORMAT(Error)]), {error, Error} ->
[] ?ERROR_MSG("Cannot open directory ~s: ~s", [Dir, ?FORMAT(Error)]),
[]
end. end.
-spec del_file_and_dir(file:name_all()) -> ok. -spec del_file_and_dir(file:name_all()) -> ok.
del_file_and_dir(File) -> del_file_and_dir(File) ->
case file:delete(File) of case file:delete(File) of
ok -> ok ->
?INFO_MSG("Removed ~s", [File]), ?INFO_MSG("Removed ~s", [File]),
Dir = filename:dirname(File), Dir = filename:dirname(File),
case file:del_dir(Dir) of case file:del_dir(Dir) of
ok -> ok ->
?DEBUG("Removed ~s", [Dir]); ?DEBUG("Removed ~s", [Dir]);
{error, Error} -> {error, Error} ->
?DEBUG("Cannot remove ~s: ~s", [Dir, ?FORMAT(Error)]) ?DEBUG("Cannot remove ~s: ~s", [Dir, ?FORMAT(Error)])
end; end;
{error, Error} -> {error, Error} ->
?WARNING_MSG("Cannot remove ~s: ~s", [File, ?FORMAT(Error)]) ?WARNING_MSG("Cannot remove ~s: ~s", [File, ?FORMAT(Error)])
end. end.
-spec secs_since_epoch() -> non_neg_integer(). -spec secs_since_epoch() -> non_neg_integer().