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')
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()