aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEvgeny Khramtsov <xramtsov@gmail.com>2018-02-23 20:35:37 +0300
committerGitHub <noreply@github.com>2018-02-23 20:35:37 +0300
commit5bf753fd2d9c54f36b4bc3e6ec9176584c50e9aa (patch)
treea3f372ff5bab0825f4255d4107d7d4b7659c6407
parentmod_carboncopy: Apply cosmetic change (diff)
parentMove make_rand_string() to 'randoms' module (diff)
Merge pull request #2297 from af8a524db1/move_make_rand_string
Move make_rand_string() to 'randoms' module
-rw-r--r--src/mod_http_upload.erl23
-rw-r--r--src/randoms.erl20
2 files changed, 20 insertions, 23 deletions
diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl
index 9f5894499..9a5952926 100644
--- a/src/mod_http_upload.erl
+++ b/src/mod_http_upload.erl
@@ -589,7 +589,7 @@ create_slot(#state{service_url = undefined,
case ejabberd_hooks:run_fold(http_upload_slot_request, ServerHost, allow,
[JID, UserDir, Size, Lang]) of
allow ->
- RandStr = make_rand_string(SecretLength),
+ RandStr = randoms:get_alphanum_string(SecretLength),
FileStr = make_file_string(File),
?INFO_MSG("Got HTTP upload slot for ~s (file: ~s)",
[jid:encode(JID), File]),
@@ -687,27 +687,6 @@ make_user_string(#jid{luser = U}, node) ->
make_file_string(File) ->
re:replace(File, <<"[^a-zA-Z0-9_.-]">>, <<$_>>, [global, {return, binary}]).
--spec make_rand_string(non_neg_integer()) -> binary().
-
-make_rand_string(Length) ->
- list_to_binary(make_rand_string([], Length)).
-
--spec make_rand_string(string(), non_neg_integer()) -> string().
-
-make_rand_string(S, 0) -> S;
-make_rand_string(S, N) -> make_rand_string([make_rand_char() | S], N - 1).
-
--spec make_rand_char() -> char().
-
-make_rand_char() ->
- map_int_to_char(randoms:uniform(0, 61)).
-
--spec map_int_to_char(0..61) -> char().
-
-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 =< 61 -> N + 61. % Lower-case character.
-
-spec yield_content_type(binary()) -> binary().
yield_content_type(<<"">>) -> ?DEFAULT_CONTENT_TYPE;
diff --git a/src/randoms.erl b/src/randoms.erl
index a64012ca3..101241a3c 100644
--- a/src/randoms.erl
+++ b/src/randoms.erl
@@ -28,7 +28,7 @@
-author('alexey@process-one.net').
-export([get_string/0, uniform/0, uniform/1, uniform/2, bytes/1,
- round_robin/1]).
+ round_robin/1, get_alphanum_string/1]).
-define(THRESHOLD, 16#10000000000000000).
@@ -71,3 +71,21 @@ bytes(N) ->
-spec round_robin(pos_integer()) -> non_neg_integer().
round_robin(N) ->
p1_time_compat:unique_integer([monotonic, positive]) rem N.
+
+-spec get_alphanum_string(non_neg_integer()) -> binary().
+get_alphanum_string(Length) ->
+ list_to_binary(get_alphanum_string([], Length)).
+
+-spec get_alphanum_string(string(), non_neg_integer()) -> string().
+get_alphanum_string(S, 0) -> S;
+get_alphanum_string(S, N) ->
+ get_alphanum_string([make_rand_char() | S], N - 1).
+
+-spec make_rand_char() -> char().
+make_rand_char() ->
+ map_int_to_char(uniform(0, 61)).
+
+-spec map_int_to_char(0..61) -> char().
+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 =< 61 -> N + 61. % Lower-case character.