<commit_before>%%%--------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C) 2016-2020 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%--------------------------------------------------------------------
%%% @doc
%%% This module is responsible for handing requests operating on file's
%%% cdmi metadata.
%%% @end
%%%--------------------------------------------------------------------
-module(cdmi_metadata_req).
-author("<NAME>").

-include("modules/fslogic/metadata.hrl").
-include("proto/oneprovider/provider_messages.hrl").

%% API
-export([
    get_transfer_encoding/2, set_transfer_encoding/5,
    get_cdmi_completion_status/2, set_cdmi_completion_status/5,
    get_mimetype/2, set_mimetype/5
]).


%%%===================================================================
%%% API
%%%===================================================================


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_transfer_encoding_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_transfer_encoding(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_transfer_encoding(UserCtx, FileCtx0) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?read_attributes]
    ),
    get_transfer_encoding_insecure(UserCtx, FileCtx1).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_transfer_encoding_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_transfer_encoding(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:transfer_encoding(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_transfer_encoding(UserCtx, FileCtx0, Encoding, Create, Replace) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?write_attributes]
    ),
    set_transfer_encoding_insecure(UserCtx, FileCtx1, Encoding, Create, Replace).


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_cdmi_completion_status_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_cdmi_completion_status(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_cdmi_completion_status(UserCtx, FileCtx0) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?read_attributes]
    ),
    get_cdmi_completion_status_insecure(UserCtx, FileCtx1).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_cdmi_completion_status_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_cdmi_completion_status(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:cdmi_completion_status(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_cdmi_completion_status(UserCtx, FileCtx0, CompletionStatus, Create, Replace) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?write_attributes]
    ),
    set_cdmi_completion_status_insecure(
        UserCtx, FileCtx1,
        CompletionStatus, Create, Replace
    ).


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_mimetype_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_mimetype(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_mimetype(UserCtx, FileCtx0) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?read_attributes]
    ),
    get_mimetype_insecure(UserCtx, FileCtx1).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_mimetype_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_mimetype(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:mimetype(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_mimetype(UserCtx, FileCtx0, Mimetype, Create, Replace) ->
    FileCtx1 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx0,
        [traverse_ancestors, ?write_attributes]
    ),
    set_mimetype_insecure(UserCtx, FileCtx1, Mimetype, Create, Replace).


%%%===================================================================
%%% Internal functions
%%%===================================================================


%%--------------------------------------------------------------------
%% @private
%% @doc
%% Returns encoding suitable for rest transfer.
%% @end<commit_msg>VFS-6685 Check file existence in cdmi_metadata_req.erl<commit_after>%%%--------------------------------------------------------------------
%%% @author <NAME>
%%% @copyright (C) 2016-2020 ACK CYFRONET AGH
%%% This software is released under the MIT license
%%% cited in 'LICENSE.txt'.
%%% @end
%%%--------------------------------------------------------------------
%%% @doc
%%% This module is responsible for handing requests operating on file's
%%% cdmi metadata.
%%% @end
%%%--------------------------------------------------------------------
-module(cdmi_metadata_req).
-author("<NAME>").

-include("modules/fslogic/metadata.hrl").
-include("proto/oneprovider/provider_messages.hrl").

%% API
-export([
    get_transfer_encoding/2, set_transfer_encoding/5,
    get_cdmi_completion_status/2, set_cdmi_completion_status/5,
    get_mimetype/2, set_mimetype/5
]).


%%%===================================================================
%%% API
%%%===================================================================


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_transfer_encoding_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_transfer_encoding(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_transfer_encoding(UserCtx, FileCtx0) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?read_attributes]
    ),
    get_transfer_encoding_insecure(UserCtx, FileCtx2).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_transfer_encoding_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_transfer_encoding(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:transfer_encoding(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_transfer_encoding(UserCtx, FileCtx0, Encoding, Create, Replace) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?write_attributes]
    ),
    set_transfer_encoding_insecure(UserCtx, FileCtx2, Encoding, Create, Replace).


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_cdmi_completion_status_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_cdmi_completion_status(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_cdmi_completion_status(UserCtx, FileCtx0) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?read_attributes]
    ),
    get_cdmi_completion_status_insecure(UserCtx, FileCtx2).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_cdmi_completion_status_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_cdmi_completion_status(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:cdmi_completion_status(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_cdmi_completion_status(UserCtx, FileCtx0, CompletionStatus, Create, Replace) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?write_attributes]
    ),
    set_cdmi_completion_status_insecure(
        UserCtx, FileCtx2,
        CompletionStatus, Create, Replace
    ).


%%--------------------------------------------------------------------
%% @doc
%% @equiv get_mimetype_insecure/2 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec get_mimetype(user_ctx:ctx(), file_ctx:ctx()) ->
    fslogic_worker:provider_response().
get_mimetype(UserCtx, FileCtx0) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?read_attributes]
    ),
    get_mimetype_insecure(UserCtx, FileCtx2).


%%--------------------------------------------------------------------
%% @doc
%% @equiv set_mimetype_insecure/5 with permission checks
%% @end
%%--------------------------------------------------------------------
-spec set_mimetype(
    user_ctx:ctx(),
    file_ctx:ctx(),
    custom_metadata:mimetype(),
    Create :: boolean(),
    Replace :: boolean()
) ->
    fslogic_worker:provider_response().
set_mimetype(UserCtx, FileCtx0, Mimetype, Create, Replace) ->
    FileCtx1 = file_ctx:assert_file_exists(FileCtx0),
    FileCtx2 = fslogic_authz:ensure_authorized(
        UserCtx, FileCtx1,
        [traverse_ancestors, ?write_attributes]
    ),
    set_mimetype_insecure(UserCtx, FileCtx2, Mimetype, Create, Replace).


%%%===================================================================
%%% Internal functions
%%%===================================================================


%%--------------------------------------------------------------------
%% @private
%% @doc
%% Returns encoding suitable for rest transfer.
%% @end
{'fraction_non_alphanumeric': 0.35321924931455045, 'fraction_numerical': 0.009832655762503546, 'mean_word_length': 1.9564002235885969, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4934881', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2796, 'n_words': 460}