diff --git a/middleware/.run/IoTClient.run.xml b/middleware/.run/IoTClient.run.xml new file mode 100644 index 0000000..af2cb73 --- /dev/null +++ b/middleware/.run/IoTClient.run.xml @@ -0,0 +1,24 @@ + + + + + \ No newline at end of file diff --git a/middleware/app_be/services/minioservice.py b/middleware/app_be/services/minioservice.py index 992adb7..242f1c8 100644 --- a/middleware/app_be/services/minioservice.py +++ b/middleware/app_be/services/minioservice.py @@ -1,3 +1,5 @@ +from typing import Union + import requests from django.conf import settings @@ -31,11 +33,11 @@ class MinioService: :param filename: filename :return: bytes representation of the file or None on error """ - file_bytes: bytes + file_bytes: Union[bytes, None] = None try: url = settings.AWS_HOST + filename response = requests.get(url, stream=True) file_bytes=response.content except: print("Error downloading file from minio") - return file_bytes \ No newline at end of file + return file_bytes diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index 051adaa..5a3a545 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -1,16 +1,13 @@ -import hashlib -import logging import json +import logging from app_be.services.dropboxservice import DropboxService -from app_be.services.mongodbservice import MongoDBService -from app_be.services.minioservice import MinioService from app_be.services.hashservice import create_sha512 +from app_be.services.minioservice import MinioService +from app_be.services.mongodbservice import MongoDBService from app_be.services.wrapperservice import WrapperService - -from app_be.views.mongo_db import MongoManager -from django.http import JsonResponse from django.http import HttpRequest +from django.http import JsonResponse from rest_framework.decorators import api_view logger = logging.getLogger(__name__) @@ -31,7 +28,6 @@ class ImageEndpoint: logger.debug('Image GET all call: {}'.format(request)) print(request) - metadata = MongoDBService.getAll() print(metadata) @@ -42,30 +38,42 @@ class ImageEndpoint: def image_api_get_single(request, identifier): logger.debug('Image GET single call: {}'.format(request)) - + # get metadata from MongoDB metadata = MongoDBService.getSingle(identifier) - print(metadata) + if not metadata: + return JsonResponse({'Result': 'Error - could not find any metadata in mongoDB.', + 'id': identifier}, status=404, safe=False) + logger.debug('Mongo Meta: {}'.format(metadata)) + # get stored SHA512 hash + stored_hash = metadata.get('sha512', '') + logger.debug('Sorted SHA512: {}'.format(stored_hash)) + + # get image bytes from Dropbox dropbox_image_bytes = DropboxService.read_file(metadata['filename']) - #minio_image_bytes = MinioService.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', '') - print(stored_hash) + # calc dropbox image hash actual_dropbox_hash = create_sha512(dropbox_image_bytes) - + logger.debug('Actual Dropbox SHA512: {}'.format(actual_dropbox_hash)) 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) + # get image bytes from MinIO + minio_image_bytes = MinioService.read_file(metadata['filename']) + if minio_image_bytes is None: + return JsonResponse({'Result': 'Error - could not find image in minIO.', 'id': identifier}, status=404, + safe=False) + + # calc minIO image hash + actual_minio_hash = create_sha512(minio_image_bytes) + logger.debug('Actual MinIO SHA512: {}'.format(actual_minio_hash)) + 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, @@ -87,7 +95,7 @@ class ImageEndpoint: decoded_image = WrapperService.unwrap_file(b64encoded_image) - MongoDBService.createSingle(metadata,identifier,decoded_image) + MongoDBService.createSingle(metadata, identifier, decoded_image) if not DropboxService.create_file(filename, decoded_image): print("Could not save image to dropbox") @@ -102,9 +110,7 @@ class ImageEndpoint: logger.debug('Image DELETE single call: {}'.format(request)) - resp = MongoDBService.deleteSingle(identifier) print(resp) - return JsonResponse({'Result': 'success1'}, safe=False)