diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index 414ec7b..a46c500 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -3,6 +3,7 @@ import logging import json from app_be.services.dropboxservice import DropboxService +from app_be.services.hashservice import create_sha512 from app_be.services.wrapperservice import WrapperService from app_be.views.mongo_db import MongoManager @@ -65,11 +66,23 @@ class ImageEndpoint: del metadata['_id'] dropbox_image_bytes = DropboxService.read_file(metadata['filename']) - if dropbox_image_bytes is None: return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, safe=False) + stored_hash = metadata.get('sha512', '') + actual_dropbox_hash = create_sha512(dropbox_image_bytes) + + if stored_hash != actual_dropbox_hash: + return JsonResponse('Stored hash does not match generated one! ' + 'stored: {} actual: {}'.format(stored_hash, actual_dropbox_hash), safe=False) + + # TODO - check hash of MinIO image, too + # actual_minio_hash = '' # create_sha512(minio_image_bytes) + # if stored_hash != actual_minio_hash: + # return JsonResponse('Stored hash does not match generated one! ' + # 'stored: {} actual: {}'.format(stored_hash, actual_minio_hash), safe=False) + payload = { 'id': identifier, 'metadata': metadata, @@ -94,9 +107,7 @@ class ImageEndpoint: col = db.metadata decoded_image = WrapperService.unwrap_file(b64encoded_image) - h = hashlib.sha512() - h.update(decoded_image) - metadata['sha512'] = h.hexdigest() + metadata['sha512'] = create_sha512(decoded_image) try: metadata['identifier'] = identifier @@ -106,7 +117,6 @@ class ImageEndpoint: except: print("Could not insert Metadata") - if not DropboxService.create_file(filename, decoded_image): print("Could not save image to dropbox")