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.
This commit is contained in:
Tom Hacohen 2020-07-10 09:29:19 +03:00
parent fae15fe420
commit 9a518b3907

View File

@ -29,15 +29,19 @@ def process_revisions_for_item(item, revision_data):
chunks = revision_data.pop('chunks_relation') chunks = revision_data.pop('chunks_relation')
for chunk in chunks: for chunk in chunks:
uid = chunk[0] uid = chunk[0]
chunk_obj = models.CollectionItemChunk.objects.filter(uid=uid).first()
if len(chunk) > 1: if len(chunk) > 1:
content = chunk[1] content = chunk[1]
chunk = models.CollectionItemChunk(uid=uid, item=item) # If the chunk already exists we assume it's fine. Otherwise, we upload it.
chunk.chunkFile.save('IGNORED', ContentFile(content)) if chunk_obj is None:
chunk.save() chunk_obj = models.CollectionItemChunk(uid=uid, item=item)
chunks_objs.append(chunk) chunk_obj.chunkFile.save('IGNORED', ContentFile(content))
chunk_obj.save()
else: else:
chunk = models.CollectionItemChunk.objects.get(uid=uid) if chunk_obj is None:
chunks_objs.append(chunk) raise serializers.ValidationError('Tried to create a new chunk without content')
chunks_objs.append(chunk_obj)
stoken = models.Stoken.objects.create() stoken = models.Stoken.objects.create()