Skip to content

Commit

Permalink
[4.3] ftps support (#6667)
Browse files Browse the repository at this point in the history
* change kz_att_ftp to support ftps

* change userpass in ftps to compulsary
  • Loading branch information
holy-batman authored Nov 10, 2020
1 parent 518385b commit c0cc08d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
18 changes: 12 additions & 6 deletions core/kazoo_attachments/src/kz_att_ftp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,22 @@ fetch_attachment(HandlerProps, DbName, DocId, AName) ->
'ok' | {'error', binary(), binary() | atom() | term()}.
send_request(Url, Contents) ->
case http_uri:parse(kz_term:to_list(Url)) of
{'ok',{'ftp', UserPass, Host, Port, FullPath,_Query}} ->
send_request(Host, Port, UserPass, FullPath, Contents);
{'ok',{Scheme, UserPass, Host, Port, FullPath,_Query}} ->
send_request(Scheme, Host, Port, UserPass, FullPath, Contents);
_ -> {'error', <<"error parsing url: ", Url/binary>>}
end.

-spec send_request(string(), integer(), string(), string(), binary()) ->
-spec send_request(string(), string(), integer(), string(), string(), binary()) ->
'ok' | {'error', binary() | atom() | term()}.
send_request(Host, Port, UserPass, FullPath, Contents) ->
send_request(Scheme, Host, Port, UserPass, FullPath, Contents) ->
{User, Pass} = case string:tokens(UserPass, ":") of
[U, P] -> {U, P};
_ -> ftp_anonymous_user_pass()
end,
Dir = filename:dirname(FullPath),
File = filename:basename(FullPath),
try
Options = [{'port', Port}],
case ftp:open(Host, Options) of
case ftp:open(Host, get_options(Scheme, Port)) of
{'ok', Pid} ->
Routines = [fun() -> ftp:user(Pid, User, Pass) end
,fun() -> ftp:type(Pid, 'binary') end
Expand All @@ -119,6 +118,13 @@ send_request(Host, Port, UserPass, FullPath, Contents) ->
{'error', Err}
end.

get_options('ftp', Port) ->
[{'port', Port}];
get_options('ftps', Port) ->
[{'port', Port}
,{'tls', []}
].

-spec ftp_cmds(list()) -> 'ok' | {'error', any()}.
ftp_cmds([]) -> 'ok';
ftp_cmds([Fun|Funs]) ->
Expand Down
11 changes: 10 additions & 1 deletion core/kazoo_schemas/src/kz_json_schema_extensions.erl
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,22 @@ is_valid_scheme(<<"http">>) -> 'true';
is_valid_scheme(<<"https">>) -> 'true';
is_valid_scheme(_) -> 'false'.

is_valid_ftp_url({'ok',{'ftp', _UserPass, Host, _Port, _FullPath,_Query}}) ->
is_valid_ftp_url({'ok',{'ftp', _UserPass, Host, _Port, _FullPath, _Query}}) ->
is_valid_host(kz_term:to_binary(Host));
is_valid_ftp_url({'ok',{'ftps', UserPass, Host, _Port, _FullPath, _Query}}) ->
lists:all(fun is_valid_url_part/1
,[{fun is_valid_ftp_userpass/1, string:tokens(UserPass, ":")}
,{fun is_valid_host/1, kz_term:to_binary(Host)}
]
);
is_valid_ftp_url(<<URL/binary>>) ->
is_valid_ftp_url(http_uri:parse(kz_term:to_list(URL)));
is_valid_ftp_url(_URL) ->
'false'.

is_valid_ftp_userpass([_User, _Pass]) -> 'true';
is_valid_ftp_userpass(_) -> 'false'.

-spec is_valid_host(kz_http_util:location()) -> boolean().
is_valid_host(<<Host/binary>>) ->
is_valid_host(kz_term:to_lower_binary(Host), kz_network_utils:is_ip(Host));
Expand Down

0 comments on commit c0cc08d

Please sign in to comment.