<commit_before>%% ---------------------------------------------------------------------
%%
%% Copyright (c) 2007-2013 Basho Technologies, Inc.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------

%% @doc WM resouce for Delete Multiple Objects

-module(riak_cs_wm_bucket_delete).

-export([init/1,
         allowed_methods/0,
         post_is_create/2,
         process_post/2
        ]).

-export([authorize/2]).

-include("riak_cs.hrl").
-include_lib("webmachine/include/webmachine.hrl").
-include_lib("xmerl/include/xmerl.hrl").

-define(RIAKCPOOL, bucket_list_pool).

-spec init(#context{}) -> {ok, #context{}}.
init(Ctx) ->
    {ok, Ctx#context{rc_pool=?RIAKCPOOL}}.

-spec allowed_methods() -> [atom()].
allowed_methods() ->
    %% POST is for Delete Multiple Objects
    ['POST'].

%% TODO: change to authorize/spec/cleanup unneeded cases
%% TODO: requires update for multi-delete
-spec authorize(#wm_reqdata{}, #context{}) -> {boolean(), #wm_reqdata{}, #context{}}.
authorize(RD, Ctx) ->
    Bucket = list_to_binary(wrq:path_info(bucket, RD)),
    {false, RD, Ctx#context{bucket=Bucket}}.

post_is_create(RD, Ctx) ->
    {false, RD, Ctx}.

-spec process_post(#wm_reqdata{}, #context{}) -> {term(), #wm_reqdata{}, #context{}}.
process_post(RD, Ctx=#context{bucket=Bucket,
                              riak_client=RcPid, user=User,
                              response_module=ResponseMod,
                              policy_module=PolicyMod}) ->
    UserName = riak_cs_wm_utils:extract_name(User),
    riak_cs_dtrace:dt_bucket_entry(?MODULE, <<"multiple_delete">>, [], [UserName, Bucket]),

    Keys = parse_body(binary_to_list(wrq:req_body(RD))),
    lager:debug("deleting keys at ~p: ~p", [Bucket, Keys]),

    case riak_cs_bucket:fetch_bucket_object(Bucket, RcPid) of
        {error, _} = Error ->
            ResponseMod:api_error(Error, RD, Ctx);
        {ok, BucketObj} ->

            Policy = riak_cs_wm_utils:translate_bucket_policy(PolicyMod, BucketObj),
            CanonicalId = riak_cs_wm_utils:extract_canonical_id(User),
            Access0 = PolicyMod:reqdata_to_access(RD, object, CanonicalId),

            %% map: keys => delete_results => xmlElements
            Results = [ handle_key(RcPid, Bucket, list_to_binary(Key),
                                   check_permission(
                                     RcPid, Bucket, list_to_binary(Key),
                                     Access0, CanonicalId, Policy, PolicyMod, BucketObj))
                        || Key <- Keys ],

            %% xmlDoc => return body.
            Xml = riak_cs_xml:to_xml([{'DeleteResult', [{'xmlns', ?S3_XMLNS}], Results}]),

            RD2 = wrq:set_resp_body(Xml, RD),<commit_msg>Fix unicode key name in delete multiple objects<commit_after>%% ---------------------------------------------------------------------
%%
%% Copyright (c) 2007-2013 Basho Technologies, Inc.  All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------

%% @doc WM resouce for Delete Multiple Objects

-module(riak_cs_wm_bucket_delete).

-export([init/1,
         allowed_methods/0,
         post_is_create/2,
         process_post/2
        ]).

-export([authorize/2]).

-include("riak_cs.hrl").
-include_lib("webmachine/include/webmachine.hrl").
-include_lib("xmerl/include/xmerl.hrl").

-define(RIAKCPOOL, bucket_list_pool).

-spec init(#context{}) -> {ok, #context{}}.
init(Ctx) ->
    {ok, Ctx#context{rc_pool=?RIAKCPOOL}}.

-spec allowed_methods() -> [atom()].
allowed_methods() ->
    %% POST is for Delete Multiple Objects
    ['POST'].

%% TODO: change to authorize/spec/cleanup unneeded cases
%% TODO: requires update for multi-delete
-spec authorize(#wm_reqdata{}, #context{}) -> {boolean(), #wm_reqdata{}, #context{}}.
authorize(RD, Ctx) ->
    Bucket = list_to_binary(wrq:path_info(bucket, RD)),
    {false, RD, Ctx#context{bucket=Bucket}}.

post_is_create(RD, Ctx) ->
    {false, RD, Ctx}.

-spec process_post(#wm_reqdata{}, #context{}) -> {term(), #wm_reqdata{}, #context{}}.
process_post(RD, Ctx=#context{bucket=Bucket,
                              riak_client=RcPid, user=User,
                              response_module=ResponseMod,
                              policy_module=PolicyMod}) ->
    UserName = riak_cs_wm_utils:extract_name(User),
    riak_cs_dtrace:dt_bucket_entry(?MODULE, <<"multiple_delete">>, [], [UserName, Bucket]),

    Keys = parse_body(binary_to_list(wrq:req_body(RD))),
    lager:debug("deleting keys at ~p: ~p", [Bucket, Keys]),

    case riak_cs_bucket:fetch_bucket_object(Bucket, RcPid) of
        {error, _} = Error ->
            ResponseMod:api_error(Error, RD, Ctx);
        {ok, BucketObj} ->

            Policy = riak_cs_wm_utils:translate_bucket_policy(PolicyMod, BucketObj),
            CanonicalId = riak_cs_wm_utils:extract_canonical_id(User),
            Access0 = PolicyMod:reqdata_to_access(RD, object, CanonicalId),

            %% map: keys => delete_results => xmlElements
            Results =
                lists:map(fun(Key) ->
                                  BinKey = unicode:characters_to_binary(Key),
                                  handle_key(RcPid, Bucket, BinKey,
                                   check_permission(
                                     RcPid, Bucket, BinKey,
                                     Access0, CanonicalId, Policy, PolicyMod, BucketObj))
                          end, Keys),

            %% xmlDoc => return body.
            Xml = riak_cs_xml:to_xml([{'DeleteResult', [{'xmlns', ?S3_XMLNS}], Results}]),

            RD2 = wrq:set_resp_body(Xml, RD),
{'fraction_non_alphanumeric': 0.16906580493537016, 'fraction_numerical': 0.006169212690951821, 'mean_word_length': 2.4233283056812467, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3289161', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2009, 'n_words': 481}