From d093dbb5b9c26de2acc8e8ba5407a3272fad1e1f Mon Sep 17 00:00:00 2001 From: Manuel Hude Date: Wed, 9 Dec 2020 17:12:46 +0100 Subject: [PATCH] added return and failsafe --- middleware/app_be/services/mongodbservice.py | 3 +++ middleware/app_be/views/rest_api.py | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/middleware/app_be/services/mongodbservice.py b/middleware/app_be/services/mongodbservice.py index 7d8017f..325e7e0 100644 --- a/middleware/app_be/services/mongodbservice.py +++ b/middleware/app_be/services/mongodbservice.py @@ -63,6 +63,9 @@ class MongoDBService: except: print("Could not insert Metadata") + return False + + return True @staticmethod def deleteSingle(identifier): diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index 7d66728..c8536be 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -60,7 +60,7 @@ class ImageEndpoint: 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) + 'stored: {} actual: {}'.format(stored_hash, actual_dropbox_hash), status=400, safe=False) # get image bytes from MinIO minio_image_bytes = MinioService.read_file(metadata['filename']) @@ -73,7 +73,7 @@ class ImageEndpoint: 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), status=404, safe=False) + 'stored: {} actual: {}'.format(stored_hash, actual_minio_hash), status=400, safe=False) payload = { 'id': identifier, @@ -95,14 +95,17 @@ class ImageEndpoint: decoded_image = WrapperService.unwrap_file(b64encoded_image) - MongoDBService.createSingle(metadata, identifier, decoded_image) - + if not MongoDBService.createSingle(metadata, identifier, decoded_image): + print("Could not save metadata") + return JsonResponse({'Result': 'Error - could not upload to MongoDB', 'id': identifier, 'filename': filename}, status=500,safe=False) if not DropboxService.create_file(filename, decoded_image): print("Could not save image to dropbox") + return JsonResponse({'Result': 'Error - could not upload to Dropbox', 'id': identifier, 'filename': filename},status=500, safe=False) if not MinioService.create_file(filename, decoded_image): print("Could not save image to minio") - return JsonResponse({'id': identifier, 'filename': filename}, - safe=False) # 'metadata': metadata response beinhaltet ObjectId welche nicht serializable is? + return JsonResponse({'Result': 'Error - could not upload to MinIO', 'id': identifier, 'filename': filename}, status=500, safe=False) + + return JsonResponse({'id': identifier, 'filename': filename},safe=False) @staticmethod @api_view(['DELETE'])