def receive_artifact(self, chunk): """Handles assembling of Manifest as it's being uploaded.""" with NamedTemporaryFile("ab") as temp_file: size = 0 hashers = {} for algorithm in Artifact.DIGEST_FIELDS: hashers[algorithm] = getattr(hashlib, algorithm)() while True: subchunk = chunk.read(2000000) if not subchunk: break temp_file.write(subchunk) size += len(subchunk) for algorithm in Artifact.DIGEST_FIELDS: hashers[algorithm].update(subchunk) temp_file.flush() digests = {} for algorithm in Artifact.DIGEST_FIELDS: digests[algorithm] = hashers[algorithm].hexdigest() artifact = Artifact(file=temp_file.name, size=size, **digests) try: artifact.save() except IntegrityError: artifact = Artifact.objects.get(sha256=artifact.sha256) artifact.touch() return artifact
def _save_artifact_blocking(artifact_attributes): saved_artifact = Artifact(**artifact_attributes) try: saved_artifact.save() except IntegrityError: del artifact_attributes["file"] saved_artifact = Artifact.objects.get(**artifact_attributes) saved_artifact.touch() return saved_artifact