Hi all,
I’m trying to compute a hash of file contents so that I can reprocess the file if it has changed, but I’m falling over at square one: handle the uploaded file without screwing up the final ‘save()’.
This is my ‘clean_file’ function:
def clean_file(self):
file_upload = self.cleaned_data.get("file")
hasher = hashlib.sha256()
with file_upload.open('rb') as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
content_hash = hasher.hexdigest()
# side effect: updates the content_hash field in the model instance
self.cleaned_data["content_hash"] = content_hash
return file_upload
I’m struggling to find anyone causing themselves this specific problem so I’m not sure how to work around it.