1 |
|
-module(mod_http_upload_api). |
2 |
|
|
3 |
|
-export([get_urls/5]). |
4 |
|
|
5 |
|
-spec get_urls(Domain :: jid:lserver(), Filename :: nonempty_binary(), Size :: pos_integer(), |
6 |
|
ContentType :: binary() | null | undefined, Timeout :: pos_integer()) -> |
7 |
|
{ok, #{binary() => term()}} |
8 |
|
| {size_error | timeout_error | module_not_loaded_error | domain_not_found | |
9 |
|
file_too_large_error, string()}. |
10 |
|
get_urls(Domain, Filename, Size, ContentType, Timeout) -> |
11 |
16 |
ContentType1 = content_type(ContentType), |
12 |
16 |
case mongoose_domain_api:get_domain_host_type(Domain) of |
13 |
|
{ok, HostType} -> |
14 |
14 |
check_module_and_get_urls(HostType, Filename, Size, ContentType1, Timeout); |
15 |
|
_ -> |
16 |
2 |
{domain_not_found, "domain does not exist"} |
17 |
|
end. |
18 |
|
|
19 |
3 |
content_type(null) -> undefined; |
20 |
3 |
content_type(<<>>) -> undefined; |
21 |
10 |
content_type(Binary) -> Binary. |
22 |
|
|
23 |
|
check_module_and_get_urls(HostType, Filename, Size, ContentType, Timeout) -> |
24 |
14 |
case gen_mod:is_loaded(HostType, mod_http_upload) of |
25 |
|
true -> |
26 |
14 |
case mod_http_upload:get_urls(HostType, Filename, Size, ContentType, Timeout) of |
27 |
|
{PutURL, GetURL, Headers} -> |
28 |
11 |
Headers1 = lists:map(fun({Name, Value}) -> {ok, #{<<"name">> => Name, <<"value">> => Value}} end, |
29 |
|
maps:to_list(Headers)), |
30 |
11 |
{ok, #{<<"putUrl">> => PutURL, <<"getUrl">> => GetURL, |
31 |
|
<<"headers">> => Headers1}}; |
32 |
|
file_too_large_error -> |
33 |
3 |
{file_too_large_error, |
34 |
|
"Declared file size exceeds the host's maximum file size."} |
35 |
|
end; |
36 |
|
false -> |
37 |
:-( |
{module_not_loaded_error, "mod_http_upload is not loaded for this host"} |
38 |
|
end. |