Compare commits

..

No commits in common. "2a573db72ec1b1c1f99caf72be9e43d4908efbd9" and "4168c5d4a834b00abe693c93d52909d42027cb4d" have entirely different histories.

View File

@ -12,14 +12,15 @@
-behaviour(gen_server). -behaviour(gen_server).
%% API %% API
-export([start_link/0, download/3]). -export([start_link/2, download/1]).
-export([test/0]). -export([test/0]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-record(state, { -record(state, {
url :: string(),
target_file :: string()
}). }).
%%%=================================================================== %%%===================================================================
@ -27,28 +28,26 @@
%%%=================================================================== %%%===================================================================
test() -> test() ->
{ok, Pid} = start_link(), {ok, Pid} = start_link("https://codeload.github.com/genadyo/LivePhotoDemo/zip/refs/heads/master", "/tmp/test.tar"),
Url = "https://codeload.github.com/genadyo/LivePhotoDemo/zip/refs/heads/master", Ref = download(Pid),
TargetDir = "/tmp/",
Ref = download(Pid, Url, TargetDir),
receive receive
{download_response, Ref, Info} -> {download_response, Ref, Info} ->
lager:debug("info is: ~p", [Info]) lager:debug("info is: ~p", [Info])
end, end,
ok. ok.
-spec download(Pid :: pid(), Url :: string(), TargetDir :: string()) -> reference(). -spec download(Pid :: pid()) -> reference().
download(Pid, Url, TargetDir) when is_pid(Pid), is_list(Url), is_list(TargetDir) -> download(Pid) when is_pid(Pid) ->
Ref = make_ref(), Ref = make_ref(),
ReceiverPid = self(), ReceiverPid = self(),
gen_server:cast(Pid, {download, ReceiverPid, Ref, Url, TargetDir}), gen_server:cast(Pid, {download, ReceiverPid, Ref}),
Ref. Ref.
%% @doc Spawns the server and registers the local name (unique) %% @doc Spawns the server and registers the local name (unique)
-spec(start_link() -> -spec(start_link(Url :: string(), TargetFile :: string()) ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link() -> start_link(Url, TargetFile) when is_list(Url), is_list(TargetFile) ->
gen_server:start_link(?MODULE, [], []). gen_server:start_link(?MODULE, [Url, TargetFile], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
@ -59,8 +58,8 @@ start_link() ->
-spec(init(Args :: term()) -> -spec(init(Args :: term()) ->
{ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} | {ok, State :: #state{}} | {ok, State :: #state{}, timeout() | hibernate} |
{stop, Reason :: term()} | ignore). {stop, Reason :: term()} | ignore).
init([]) -> init([Url, TargetFile]) ->
{ok, #state{}}. {ok, #state{url = Url, target_file = TargetFile}}.
%% @private %% @private
%% @doc Handling call messages %% @doc Handling call messages
@ -81,16 +80,11 @@ handle_call(_Request, _From, State = #state{}) ->
{noreply, NewState :: #state{}} | {noreply, NewState :: #state{}} |
{noreply, NewState :: #state{}, timeout() | hibernate} | {noreply, NewState :: #state{}, timeout() | hibernate} |
{stop, Reason :: term(), NewState :: #state{}}). {stop, Reason :: term(), NewState :: #state{}}).
handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) -> handle_cast({download, ReceiverPid, Ref}, State = #state{url = Url, target_file = TargetFile}) ->
SSLOpts = {ssl_options, [ case hackney:get(Url, [], <<>>, [async]) of
%
{verify, verify_none}
]},
TargetFile = get_filename_from_url(Url),
case hackney:request(get, Url, [], <<>>, [async, {stream_to, self()}, {pool, false}, SSLOpts]) of
{ok, ClientRef} -> {ok, ClientRef} ->
case receive_data(ClientRef, TargetDir, TargetFile) of {ok, File} = file:open(TargetFile, [write, binary]),
case receive_data(ClientRef, File) of
ok -> ok ->
ReceiverPid ! {download_response, Ref, ok}; ReceiverPid ! {download_response, Ref, ok};
{error, Reason} -> {error, Reason} ->
@ -132,56 +126,23 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
%% ResponseLine receive_data(ClientRef, File) ->
receive_data(ClientRef, TargetDir, DefaultFile) when is_list(TargetDir), is_list(DefaultFile) ->
receive receive
{hackney_response, ClientRef, {headers, _Headers}} ->
receive_data(ClientRef, File);
{hackney_response, ClientRef, {status, 200, _Reason}} -> {hackney_response, ClientRef, {status, 200, _Reason}} ->
receive_data0(ClientRef, TargetDir, DefaultFile); receive_data(ClientRef, File);
{hackney_response, ClientRef, {status, StatusCode, _Reason}} -> {hackney_response, ClientRef, {status, StatusCode, _Reason}} ->
{error, {http_status, StatusCode}} lager:debug("call me here"),
end. {error, {http_status, StatusCode}};
%% , {hackney_response, ClientRef, Data} ->
receive_data0(ClientRef, TargetDir, DefaultFile) -> file:write(File, Data),
receive receive_data(ClientRef, File);
{hackney_response, ClientRef, {headers, Headers}} ->
TargetFilename = extra_filename(Headers, DefaultFile),
FullFilename = TargetDir ++ TargetFilename,
{ok, File} = file:open(FullFilename, [write, binary]),
receive_data1(ClientRef, File)
end.
%%
receive_data1(ClientRef, File) ->
receive
{hackney_response, ClientRef, {error, Reason}} ->
ok = file:close(File),
{error, Reason};
{hackney_response, ClientRef, done} -> {hackney_response, ClientRef, done} ->
ok = file:close(File), ok = file:close(File),
hackney:close(ClientRef), hackney:close(ClientRef),
ok; ok;
{hackney_response, ClientRef, Data} -> {hackney_response, ClientRef, {error, Reason}} ->
file:write(File, Data), ok = file:close(File),
receive_data1(ClientRef, File) {error, Reason}
end. end.
-spec extra_filename(Headers :: list(), Default :: string()) -> Filename :: string().
extra_filename(Headers, Default) when is_list(Headers), is_list(Default) ->
case lists:filter(fun({K, _}) -> string:lowercase(K) =:= <<"content-disposition">> end, Headers) of
[{_, <<"attachment; ", Rest/binary>>}|_] ->
Params0 = binary:split(Rest, <<";">>, [global]),
Params = lists:map(fun(P) -> list_to_tuple(binary:split(P, <<"=">>)) end, Params0),
case proplists:get_value(<<"filename">>, Params) of
undefined ->
Default;
Filename ->
binary_to_list(Filename)
end;
_ ->
Default
end.
-spec get_filename_from_url(Url :: string()) -> string().
get_filename_from_url(Url) when is_list(Url) ->
URIMap = uri_string:parse(Url),
Path = maps:get(path, URIMap),
filename:basename(Path).