Exemple #1
0
    def validate(self, data):
        """
        Validate file by size and by all checksums provided.

        Args:
            data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their
                values

        Raises:
            :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any
                of the checksums don't match their actual values.
        """
        if 'size' in data:
            if data['file'].size != int(data['size']):
                raise serializers.ValidationError(_("The size did not match actual size of file."))
        else:
            data['size'] = data['file'].size

        for algorithm in hashlib.algorithms_guaranteed:
            if algorithm in models.Artifact.DIGEST_FIELDS:
                digest = data['file'].hashers[algorithm].hexdigest()

                if algorithm in data and digest != data[algorithm]:
                    raise serializers.ValidationError(_("The %s checksum did not match.")
                                                      % algorithm)
                else:
                    data[algorithm] = digest
                if algorithm in UNIQUE_ALGORITHMS:
                    validator = UniqueValidator(models.Artifact.objects.all(),
                                                message=_("{0} checksum must be "
                                                          "unique.").format(algorithm))
                    validator.field_name = algorithm
                    validator.instance = None
                    validator(digest)
        return data
Exemple #2
0
    def validate(self, data):
        """
        Validate file by size and by all checksums provided.

        Args:
            data (:class:`django.http.QueryDict`): QueryDict mapping Artifact model fields to their
                values

        Raises:
            :class:`rest_framework.exceptions.ValidationError`: When the expected file size or any
                of the checksums don't match their actual values.
        """
        super().validate(data)
        if "size" in data:
            if data["file"].size != int(data["size"]):
                raise serializers.ValidationError(
                    _("The size did not match actual size of file."))
        else:
            data["size"] = data["file"].size

        bad_algs = []
        for algorithm in models.Artifact.FORBIDDEN_DIGESTS:
            if algorithm in data:
                bad_algs.append(algorithm)
        if bad_algs:
            raise serializers.ValidationError(
                _(f"Checksum algorithms {bad_algs} forbidden for this Pulp instance."
                  ))

        for algorithm in reversed(models.Artifact.DIGEST_FIELDS):
            digest = data["file"].hashers[algorithm].hexdigest()

            if algorithm in data and digest != data[algorithm]:
                raise serializers.ValidationError(
                    _("The %s checksum did not match.") % algorithm)
            else:
                data[algorithm] = digest

            if algorithm in models.Artifact.RELIABLE_DIGEST_FIELDS:
                validator = UniqueValidator(
                    models.Artifact.objects.all(),
                    message=_(
                        "Artifact with {0} checksum of '{1}' already exists.").
                    format(algorithm, digest),
                )
                validator.instance = None
                validator(digest, self.fields[algorithm])

        return data