Compare commits

...

2 Commits

Author SHA1 Message Date
2a573db72e 文件下载器 2025-04-19 20:09:47 +08:00
6361671176 downloader 2025-04-19 19:45:37 +08:00

View File

@ -12,15 +12,14 @@
-behaviour(gen_server). -behaviour(gen_server).
%% API %% API
-export([start_link/2, download/1]). -export([start_link/0, download/3]).
-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()
}). }).
%%%=================================================================== %%%===================================================================
@ -28,26 +27,28 @@
%%%=================================================================== %%%===================================================================
test() -> test() ->
{ok, Pid} = start_link("https://codeload.github.com/genadyo/LivePhotoDemo/zip/refs/heads/master", "/tmp/test.tar"), {ok, Pid} = start_link(),
Ref = download(Pid), Url = "https://codeload.github.com/genadyo/LivePhotoDemo/zip/refs/heads/master",
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()) -> reference(). -spec download(Pid :: pid(), Url :: string(), TargetDir :: string()) -> reference().
download(Pid) when is_pid(Pid) -> download(Pid, Url, TargetDir) when is_pid(Pid), is_list(Url), is_list(TargetDir) ->
Ref = make_ref(), Ref = make_ref(),
ReceiverPid = self(), ReceiverPid = self(),
gen_server:cast(Pid, {download, ReceiverPid, Ref}), gen_server:cast(Pid, {download, ReceiverPid, Ref, Url, TargetDir}),
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(Url :: string(), TargetFile :: string()) -> -spec(start_link() ->
{ok, Pid :: pid()} | ignore | {error, Reason :: term()}). {ok, Pid :: pid()} | ignore | {error, Reason :: term()}).
start_link(Url, TargetFile) when is_list(Url), is_list(TargetFile) -> start_link() ->
gen_server:start_link(?MODULE, [Url, TargetFile], []). gen_server:start_link(?MODULE, [], []).
%%%=================================================================== %%%===================================================================
%%% gen_server callbacks %%% gen_server callbacks
@ -58,8 +59,8 @@ start_link(Url, TargetFile) when is_list(Url), is_list(TargetFile) ->
-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([Url, TargetFile]) -> init([]) ->
{ok, #state{url = Url, target_file = TargetFile}}. {ok, #state{}}.
%% @private %% @private
%% @doc Handling call messages %% @doc Handling call messages
@ -80,11 +81,16 @@ 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}, State = #state{url = Url, target_file = TargetFile}) -> handle_cast({download, ReceiverPid, Ref, Url, TargetDir}, State = #state{}) ->
case hackney:get(Url, [], <<>>, [async]) of SSLOpts = {ssl_options, [
%
{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} ->
{ok, File} = file:open(TargetFile, [write, binary]), case receive_data(ClientRef, TargetDir, TargetFile) of
case receive_data(ClientRef, File) of
ok -> ok ->
ReceiverPid ! {download_response, Ref, ok}; ReceiverPid ! {download_response, Ref, ok};
{error, Reason} -> {error, Reason} ->
@ -126,23 +132,56 @@ code_change(_OldVsn, State = #state{}, _Extra) ->
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
receive_data(ClientRef, File) -> %% ResponseLine
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_data(ClientRef, File); receive_data0(ClientRef, TargetDir, DefaultFile);
{hackney_response, ClientRef, {status, StatusCode, _Reason}} -> {hackney_response, ClientRef, {status, StatusCode, _Reason}} ->
lager:debug("call me here"), {error, {http_status, StatusCode}}
{error, {http_status, StatusCode}}; end.
{hackney_response, ClientRef, Data} -> %% ,
file:write(File, Data), receive_data0(ClientRef, TargetDir, DefaultFile) ->
receive_data(ClientRef, File); receive
{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, {error, Reason}} -> {hackney_response, ClientRef, Data} ->
ok = file:close(File), file:write(File, Data),
{error, Reason} receive_data1(ClientRef, File)
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).