From 9a518b3907ffd0ba95ead85724d2674988d46fcb Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 10 Jul 2020 09:29:19 +0300 Subject: [PATCH] Chunks: add error handling for chunks having content or not existing. If the chunk already has a content and we try to upload it again, we assume the previous content was correct and this one is the same (chunks are immutable). We can't actually ensure they are the same due to the encryption, though they should be. If a chunk is being uploaded for the first time and doesn't have a content, throw a validation error rather than throwing an ugly error. --- django_etebase/serializers.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index f2da771..3f1b084 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -29,15 +29,19 @@ def process_revisions_for_item(item, revision_data): chunks = revision_data.pop('chunks_relation') for chunk in chunks: uid = chunk[0] + chunk_obj = models.CollectionItemChunk.objects.filter(uid=uid).first() if len(chunk) > 1: content = chunk[1] - chunk = models.CollectionItemChunk(uid=uid, item=item) - chunk.chunkFile.save('IGNORED', ContentFile(content)) - chunk.save() - chunks_objs.append(chunk) + # If the chunk already exists we assume it's fine. Otherwise, we upload it. + if chunk_obj is None: + chunk_obj = models.CollectionItemChunk(uid=uid, item=item) + chunk_obj.chunkFile.save('IGNORED', ContentFile(content)) + chunk_obj.save() else: - chunk = models.CollectionItemChunk.objects.get(uid=uid) - chunks_objs.append(chunk) + if chunk_obj is None: + raise serializers.ValidationError('Tried to create a new chunk without content') + + chunks_objs.append(chunk_obj) stoken = models.Stoken.objects.create()