Пример #1
0
 def clean_source(self):
     source = self.cleaned_data.get('source')
     if source:
         try:
             if source.name.endswith('.zip'):
                 zip_file = SafeZip(source)
                 # testzip() returns None if there are no broken CRCs.
                 if zip_file.zip_file.testzip() is not None:
                     raise zipfile.BadZipfile()
             elif source.name.endswith(('.tar.gz', '.tar.bz2', '.tgz')):
                 # For tar files we need to do a little more work.
                 mode = 'r:bz2' if source.name.endswith('bz2') else 'r:gz'
                 with tarfile.open(mode=mode, fileobj=source) as archive:
                     archive_members = archive.getmembers()
                     for member in archive_members:
                         archive_member_validator(archive, member)
             else:
                 valid_extensions_string = u'(%s)' % u', '.join(
                     VALID_SOURCE_EXTENSIONS)
                 raise forms.ValidationError(
                     ugettext(
                         'Unsupported file type, please upload an archive '
                         'file {extensions}.'.format(
                             extensions=valid_extensions_string)))
         except (zipfile.BadZipfile, tarfile.ReadError, IOError, EOFError):
             raise forms.ValidationError(
                 ugettext('Invalid or broken archive.'))
     return source
Пример #2
0
 def clean_source(self):
     source = self.cleaned_data.get('source')
     if source:
         try:
             if source.name.endswith('.zip'):
                 zip_file = SafeZip(source)
                 # testzip() returns None if there are no broken CRCs.
                 if zip_file.zip_file.testzip() is not None:
                     raise zipfile.BadZipfile()
             elif source.name.endswith(('.tar.gz', '.tar.bz2')):
                 # For tar files we need to do a little more work.
                 # Fortunately tarfile.open() already handles compression
                 # formats for us automatically.
                 with tarfile.open(fileobj=source) as archive:
                     archive_members = archive.getmembers()
                     for member in archive_members:
                         archive_member_validator(archive, member)
             else:
                 valid_extensions_string = u'(%s)' % u', '.join(
                     VALID_SOURCE_EXTENSIONS)
                 raise forms.ValidationError(
                     ugettext(
                         'Unsupported file type, please upload an archive '
                         'file {extensions}.'.format(
                             extensions=valid_extensions_string)))
         except (zipfile.BadZipfile, tarfile.ReadError, IOError):
             raise forms.ValidationError(
                 ugettext('Invalid or broken archive.'))
     return source
Пример #3
0
    def to_internal_value(self, data):
        data = super().to_internal_value(data)

        # Ensure the file type is one we support.
        if not data.name.endswith(VALID_SOURCE_EXTENSIONS):
            error_msg = (
                'Unsupported file type, please upload an archive file ({extensions}).'
            )
            raise exceptions.ValidationError(
                error_msg.format(
                    extensions=(', '.join(VALID_SOURCE_EXTENSIONS))))

        # Check inside to see if the file extension matches the content.
        try:
            _, ext = os.path.splitext(data.name)
            if ext == '.zip':
                # testzip() returns None if there are no broken CRCs.
                if SafeZip(data).zip_file.testzip() is not None:
                    raise zipfile.BadZipFile()
            else:
                # For tar files we need to do a little more work.
                mode = 'r:bz2' if ext == '.bz2' else 'r:gz'
                with tarfile.open(mode=mode, fileobj=data) as archive:
                    for member in archive.getmembers():
                        archive_member_validator(archive, member)
        except (zipfile.BadZipFile, tarfile.ReadError, OSError, EOFError):
            raise exceptions.ValidationError('Invalid or broken archive.')

        return data