From fcf9a4acd6f6b2167e2c38bf83b04c70cd3c6381 Mon Sep 17 00:00:00 2001 From: James Aimonetti Date: Fri, 4 Aug 2017 20:13:06 +0000 Subject: [PATCH] Task naming 4 2 (#4030) * name file when using default output * don't overwrite set headers * let api_resource do the reply * handle input file as well --- applications/crossbar/src/api_resource.erl | 10 +++-- .../crossbar/src/modules/cb_tasks.erl | 38 ++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/applications/crossbar/src/api_resource.erl b/applications/crossbar/src/api_resource.erl index 3265316c257..8522c6e407c 100644 --- a/applications/crossbar/src/api_resource.erl +++ b/applications/crossbar/src/api_resource.erl @@ -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 diff --git a/applications/crossbar/src/modules/cb_tasks.erl b/applications/crossbar/src/modules/cb_tasks.erl index 2c26e36d7bf..6d012643696 100644 --- a/applications/crossbar/src/modules/cb_tasks.erl +++ b/applications/crossbar/src/modules/cb_tasks.erl @@ -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), + + <>; +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), + + <>; +download_filename(_Context, Name) -> + Name. %%-------------------------------------------------------------------- %% @public @@ -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) ->