added hashservice.py;

implemented create_sha512();
store image hash to mongoDB;
compare stored with actual dropbox hash;
This commit is contained in:
Marco Zeisler 2020-11-30 19:16:36 +01:00
parent dc9912f90c
commit 6a64eb305b

View File

@ -3,6 +3,7 @@ import logging
import json import json
from app_be.services.dropboxservice import DropboxService 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.services.wrapperservice import WrapperService
from app_be.views.mongo_db import MongoManager from app_be.views.mongo_db import MongoManager
@ -65,11 +66,23 @@ class ImageEndpoint:
del metadata['_id'] del metadata['_id']
dropbox_image_bytes = DropboxService.read_file(metadata['filename']) dropbox_image_bytes = DropboxService.read_file(metadata['filename'])
if dropbox_image_bytes is None: if dropbox_image_bytes is None:
return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404,
safe=False) 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 = { payload = {
'id': identifier, 'id': identifier,
'metadata': metadata, 'metadata': metadata,
@ -94,9 +107,7 @@ class ImageEndpoint:
col = db.metadata col = db.metadata
decoded_image = WrapperService.unwrap_file(b64encoded_image) decoded_image = WrapperService.unwrap_file(b64encoded_image)
h = hashlib.sha512() metadata['sha512'] = create_sha512(decoded_image)
h.update(decoded_image)
metadata['sha512'] = h.hexdigest()
try: try:
metadata['identifier'] = identifier metadata['identifier'] = identifier
@ -106,7 +117,6 @@ class ImageEndpoint:
except: except:
print("Could not insert Metadata") print("Could not insert Metadata")
if not DropboxService.create_file(filename, decoded_image): if not DropboxService.create_file(filename, decoded_image):
print("Could not save image to dropbox") print("Could not save image to dropbox")