Skip to content

Commit

Permalink
Task naming 4 2 (2600hz#4030)
Browse files Browse the repository at this point in the history
* name file when using default output

* don't overwrite set headers

* let api_resource do the reply

* handle input file as well
  • Loading branch information
jamesaimonetti authored and fenollp committed Aug 4, 2017
1 parent 5549c96 commit fcf9a4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
10 changes: 6 additions & 4 deletions applications/crossbar/src/api_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -885,10 +885,12 @@ to_csv(Req, Context) ->
case cb_context:fetch(Context1, 'is_chunked') of
'true' -> {'halt', Req1, Context1};
_ ->
RespHeaders = [{<<"Content-Type">>, <<"application/octet-stream">>}
,{<<"Content-Disposition">>, <<"attachment; filename=\"data.csv\"">>}
| cb_context:resp_headers(Context1)
],
RespHeaders =
props:insert_values([{<<"content-type">>, <<"application/octet-stream">>}
,{<<"content-disposition">>, <<"attachment; filename=\"data.csv\"">>}
]
,cb_context:resp_headers(Context1)
),
{csv_body(cb_context:resp_data(Context))
,api_util:set_resp_headers(Req1, cb_context:set_resp_headers(Context1, RespHeaders))
,Context1
Expand Down
38 changes: 33 additions & 5 deletions applications/crossbar/src/modules/cb_tasks.erl
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,39 @@ ctp(Context) ->
-spec to_csv({cowboy_req:req(), cb_context:context()}) ->
{cowboy_req:req(), cb_context:context()}.
to_csv({Req, Context}) ->
Filename = requested_attachment_name(Context),
Filename = download_filename(Context, requested_attachment_name(Context)),
Headers = props:set_values([{<<"content-type">>, <<"text/csv">>}
,{<<"content-disposition">>, <<"attachment; filename=\"", Filename/binary, "\"">>}
]
,cowboy_req:get('resp_headers', Req)
),
{'ok', Req1} = cowboy_req:reply(200, Headers, cb_context:resp_data(Context), Req),
{Req1, Context}.
{Req, cb_context:set_resp_headers(Context, Headers)}.

-spec download_filename(cb_context:context(), ne_binary()) -> ne_binary().
download_filename(Context, ?KZ_TASKS_ANAME_OUT) ->
TaskJObj = cb_context:doc(Context),

Category = kzd_task:category(TaskJObj),
Action = kzd_task:action(TaskJObj),
TaskId = kz_doc:id(TaskJObj),

<<Category/binary, "_"
,Action/binary, "_"
,TaskId/binary, "_out.csv"
>>;
download_filename(Context, ?KZ_TASKS_ANAME_IN) ->
TaskJObj = cb_context:doc(Context),

Category = kzd_task:category(TaskJObj),
Action = kzd_task:action(TaskJObj),
TaskId = kz_doc:id(TaskJObj),

<<Category/binary, "_"
,Action/binary, "_"
,TaskId/binary, "_in.csv"
>>;
download_filename(_Context, Name) ->
Name.

%%--------------------------------------------------------------------
%% @public
Expand Down Expand Up @@ -459,9 +484,12 @@ handle_read_result(TaskId, OrigContext, ReadContext) ->
case cb_context:resp_data(ReadContext) of
[] -> crossbar_util:response_bad_identifier(TaskId, OrigContext);
[TaskJObj] ->
lager:debug("task: ~p", [TaskJObj]),
JObj = kz_json:set_value(<<"_read_only">>, TaskJObj, kz_json:new()),
cb_context:set_resp_data(ReadContext, JObj)
cb_context:setters(ReadContext
,[{fun cb_context:set_doc/2, JObj}
,{fun cb_context:set_resp_data/2, JObj}
]
)
end.

read_attachment(TaskId, Context, AccountId) ->
Expand Down

0 comments on commit fcf9a4a

Please sign in to comment.