2014-02-06 19:36:25 +01:00
|
|
|
%% Feel free to use, reuse and abuse the code in this file.
|
|
|
|
|
|
|
|
-module(http_multipart_stream).
|
|
|
|
|
2014-09-26 15:58:44 +03:00
|
|
|
-export([init/2]).
|
|
|
|
|
|
|
|
init(Req, Opts) ->
|
2014-02-06 19:36:25 +01:00
|
|
|
Req2 = multipart(Req),
|
2014-09-30 20:12:13 +03:00
|
|
|
{ok, cowboy_req:reply(200, Req2), Opts}.
|
2014-02-06 19:36:25 +01:00
|
|
|
|
|
|
|
multipart(Req) ->
|
2016-08-10 15:09:04 +02:00
|
|
|
case cowboy_req:read_part(Req) of
|
2014-02-06 19:36:25 +01:00
|
|
|
{ok, [{<<"content-length">>, BinLength}], Req2} ->
|
|
|
|
Length = list_to_integer(binary_to_list(BinLength)),
|
|
|
|
{Length, Req3} = stream_body(Req2, 0),
|
|
|
|
multipart(Req3);
|
|
|
|
{done, Req2} ->
|
|
|
|
Req2
|
|
|
|
end.
|
|
|
|
|
|
|
|
stream_body(Req, N) ->
|
2016-08-10 15:09:04 +02:00
|
|
|
case cowboy_req:read_part_body(Req) of
|
2014-02-06 19:36:25 +01:00
|
|
|
{ok, Data, Req2} ->
|
|
|
|
{N + byte_size(Data), Req2};
|
|
|
|
{more, Data, Req2} ->
|
|
|
|
stream_body(Req2, N + byte_size(Data))
|
|
|
|
end.
|