<commit_before>from __future__ import unicode_literals

import magic

from django.utils.translation import ugettext_lazy as _
from django.utils.deconstruct import deconstructible
from django.core.exceptions import ValidationError


@deconstructible
class MimeTypeValidator(object):
    """Validator for files, checking the mimetype."""

    mime_message = _(
        "MIME type '%(mimetype)s' is not valid. "
        "Allowed types are: %(allowed_mimetypes)s."
    )

    def __init__(self, allowed_mimetypes=None, message=None):
        self.allowed_mimetypes = allowed_mimetypes
        if message is not None:
            self.mime_message = message

    def __call__(self, value):
        # Check the content type
        mimetype = magic.from_buffer(value.read(1024), mime=True)
        if self.allowed_mimetypes and mimetype not in self.allowed_mimetypes:
            message = self.mime_message % {
                'mimetype': mimetype,
                'allowed_mimetypes': ', '.join(self.allowed_mimetypes)
            }
            raise ValidationError(message)<commit_msg>Added validator to check integrity of tarfile<commit_after>from __future__ import unicode_literals

# import tarfile
import magic
import os

from django.utils.translation import ugettext_lazy as _
from django.utils.deconstruct import deconstructible
from django.core.exceptions import ValidationError


@deconstructible
class MimeTypeValidator(object):
    """Validator for files, checking the mimetype."""

    mime_message = _(
        "MIME type '%(mimetype)s' is not valid. "
        "Allowed types are: %(allowed_mimetypes)s."
    )

    def __init__(self, allowed_mimetypes=None, message=None):
        self.allowed_mimetypes = allowed_mimetypes
        if message is not None:
            self.mime_message = message

    def __call__(self, value):
        # Check the content type
        mimetype = magic.from_buffer(value.read(1024), mime=True)
        if self.allowed_mimetypes and mimetype not in self.allowed_mimetypes:
            message = self.mime_message % {
                'mimetype': mimetype,
                'allowed_mimetypes': ', '.join(self.allowed_mimetypes)
            }
            raise ValidationError(message)


@deconstructible
class IntegrityTarValidator(object):
    """ """

    message = _("Unsafe filenames on tar.")

    def __init__(self, target_dir, message=None):
        """ """
        self.target_dir = os.path.abspath(target_dir)
        if message is not None:
            self.message = message

    def __call__(self, value):
        """TODO: cant open tarfile if value is a InMemoryUploadedFile"""
        # with tarfile.open(fileobj=fileobj) as tar:
        #     for filename in tar.getnames():
        #         fullpath = os.path.join(self.target_dir, filename)
        #         if not os.path.abspath(fullpath) == fullpath:
        #             raise ValidationError(self.message)
{'fraction_non_alphanumeric': 0.08832591578226635, 'fraction_numerical': 0.0027387880862718246, 'mean_word_length': 2.8046875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2541524', 'n_tokens_mistral': 865, 'n_tokens_neox': 838, 'n_words': 213}