<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed 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.
###############################################################################

import hashlib
import six
import warnings

from girder import events
from girder.api import access
from girder.api.describe import autoDescribeRoute, Description
from girder.api.rest import RestException, setRawResponse, setResponseHeader, setContentDisposition
from girder.api.v1.file import File
from girder.constants import AccessType, TokenScope
from girder.models.model_base import ValidationException
from girder.models.file import File as FileModel
from girder.models.setting import Setting
from girder.utility import setting_utilities
from girder.utility.progress import ProgressContext, noProgress

SUPPORTED_ALGORITHMS = {'sha512'}
_CHUNK_LEN = 65536


class PluginSettings(object):
    AUTO_COMPUTE = 'hashsum_download.auto_compute'


class HashedFile(File):
    @property
    def supportedAlgorithms(self):  # pragma: no cover
        warnings.warn(
            'HashedFile.supportedAlgorithms is deprecated, use the module-level '
            'SUPPORTED_ALGORITHMS instead.', DeprecationWarning)
        return SUPPORTED_ALGORITHMS

    def __init__(self, node):
        super(HashedFile, self).__init__()

        node.route('GET', ('hashsum', ':algo', ':hash', 'download'), self.downloadWithHash)
        node.route('GET', (':id', 'hashsum_file', ':algo'), self.downloadKeyFile)
        node.route('POST', (':id', 'hashsum'), self.computeHashes)

    @access.cookie
    @access.public(scope=TokenScope.DATA_READ)
    @autoDescribeRoute(
        Description('Download the hashsum key file for a given file.')
        .modelParam('id', 'The ID of the file.', model=FileModel, level=AccessType.READ)
        .param('algo', 'The hashsum algorithm.', paramType='path', lower=True,
               enum=SUPPORTED_ALGORITHMS)
        .notes('This is meant to be used in conjunction with CMake\'s ExternalData module.')
        .produces('text/plain')
        .errorResponse()
        .errorResponse('Read access was denied on the file.', 403)
    )
    def downloadKeyFile(self, file, algo):
        self._validateAlgo(algo)

        if algo not in file:
            raise RestException('This file does not have the %s hash computed.' % algo)
        hash = file[algo]
        name = '.'.join((file['name'], algo))

        setResponseHeader('Content-Length', len(hash))
        setResponseHeader('Content-Type', 'text/plain')
        setContentDisposition(name)
        setRawResponse()

        return hash

    @access.cookie
    @access.public(scope=TokenScope.DATA_READ)
    @autoDescribeRoute(
        Description('Download a file by its hash sum.')
        .param('algo', 'The type of the given hash sum (case insensitive).',
               paramType='path', lower=True, enum=SUPPORTED_ALGORITHMS)<commit_msg>Include a trailing newline in hashsum download keyfiles<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed 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.
###############################################################################

import hashlib
import six
import warnings

from girder import events
from girder.api import access
from girder.api.describe import autoDescribeRoute, Description
from girder.api.rest import RestException, setRawResponse, setResponseHeader, setContentDisposition
from girder.api.v1.file import File
from girder.constants import AccessType, TokenScope
from girder.models.model_base import ValidationException
from girder.models.file import File as FileModel
from girder.models.setting import Setting
from girder.utility import setting_utilities
from girder.utility.progress import ProgressContext, noProgress

SUPPORTED_ALGORITHMS = {'sha512'}
_CHUNK_LEN = 65536


class PluginSettings(object):
    AUTO_COMPUTE = 'hashsum_download.auto_compute'


class HashedFile(File):
    @property
    def supportedAlgorithms(self):  # pragma: no cover
        warnings.warn(
            'HashedFile.supportedAlgorithms is deprecated, use the module-level '
            'SUPPORTED_ALGORITHMS instead.', DeprecationWarning)
        return SUPPORTED_ALGORITHMS

    def __init__(self, node):
        super(HashedFile, self).__init__()

        node.route('GET', ('hashsum', ':algo', ':hash', 'download'), self.downloadWithHash)
        node.route('GET', (':id', 'hashsum_file', ':algo'), self.downloadKeyFile)
        node.route('POST', (':id', 'hashsum'), self.computeHashes)

    @access.cookie
    @access.public(scope=TokenScope.DATA_READ)
    @autoDescribeRoute(
        Description('Download the hashsum key file for a given file.')
        .modelParam('id', 'The ID of the file.', model=FileModel, level=AccessType.READ)
        .param('algo', 'The hashsum algorithm.', paramType='path', lower=True,
               enum=SUPPORTED_ALGORITHMS)
        .notes('This is meant to be used in conjunction with CMake\'s ExternalData module.')
        .produces('text/plain')
        .errorResponse()
        .errorResponse('Read access was denied on the file.', 403)
    )
    def downloadKeyFile(self, file, algo):
        self._validateAlgo(algo)

        if algo not in file:
            raise RestException('This file does not have the %s hash computed.' % algo)
        keyFileBody = '%s\n' % file[algo]
        name = '.'.join((file['name'], algo))

        setResponseHeader('Content-Length', len(keyFileBody))
        setResponseHeader('Content-Type', 'text/plain')
        setContentDisposition(name)
        setRawResponse()

        return keyFileBody

    @access.cookie
    @access.public(scope=TokenScope.DATA_READ)
    @autoDescribeRoute(
        Description('Download a file by its hash sum.')
        .param('algo', 'The type of the given hash sum (case insensitive).',
               paramType='path', lower=True, enum=SUPPORTED_ALGORITHMS)
{'fraction_non_alphanumeric': 0.14075217513331462, 'fraction_numerical': 0.0047712601740106655, 'mean_word_length': 3.780013413816231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3859786', 'n_tokens_mistral': 1991, 'n_tokens_neox': 1939, 'n_words': 601}