diff --git a/middleware/app_be/views/mongo_db.py b/middleware/app_be/views/mongo_db.py index 3326c1f..ce5c8e8 100644 --- a/middleware/app_be/views/mongo_db.py +++ b/middleware/app_be/views/mongo_db.py @@ -1,5 +1,6 @@ import pymongo + class MongoManager: __instance = None @@ -17,6 +18,7 @@ class MongoManager: db = MongoManager.__instance.AIC coll = db.metadata db.coll["identifier"] + db.coll["sha512"] db.coll["location"] - coll.create_index("identifier",unique=True) - coll.create_index([("location", pymongo.GEO2D)]) \ No newline at end of file + coll.create_index("identifier", unique=True) + coll.create_index([("location", pymongo.GEO2D)]) diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index b65a0b0..414ec7b 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -1,6 +1,6 @@ +import hashlib import logging import json -import base64 from app_be.services.dropboxservice import DropboxService from app_be.services.wrapperservice import WrapperService @@ -43,7 +43,6 @@ class ImageEndpoint: return JsonResponse({'Result': 'success1'}, safe=False) - @staticmethod @api_view(['GET']) def image_api_get_single(request, identifier): @@ -68,7 +67,8 @@ class ImageEndpoint: 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) + return JsonResponse({'Result': 'Error - could not find image in dropbox.', 'id': identifier}, status=404, + safe=False) payload = { 'id': identifier, @@ -93,15 +93,20 @@ class ImageEndpoint: db = instance.AIC col = db.metadata + decoded_image = WrapperService.unwrap_file(b64encoded_image) + h = hashlib.sha512() + h.update(decoded_image) + metadata['sha512'] = h.hexdigest() + try: metadata['identifier'] = identifier - metadata['location'] = [metadata['longitude'],metadata['latitude']] + metadata['location'] = [metadata['longitude'], metadata['latitude']] col.insert_one(metadata) except: print("Could not insert Metadata") - decoded_image = WrapperService.unwrap_file(b64encoded_image) + if not DropboxService.create_file(filename, decoded_image): print("Could not save image to dropbox") @@ -124,4 +129,4 @@ class ImageEndpoint: except: print("Could not delete Metadata") - return JsonResponse({'Result': 'success1'}, safe=False) \ No newline at end of file + return JsonResponse({'Result': 'success1'}, safe=False)