From c440b66894bb4132b98ba3250e2d1b9a9589ed66 Mon Sep 17 00:00:00 2001 From: Martin Schett Date: Sun, 6 Dec 2020 19:36:30 +0100 Subject: [PATCH] Added update (pre minio delete) --- iotclient/iot_client.py | 43 ++++++++++++++++++++ middleware/app_be/services/mongodbservice.py | 2 +- middleware/app_be/urls.py | 1 + middleware/app_be/views/rest_api.py | 34 ++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/iotclient/iot_client.py b/iotclient/iot_client.py index e1e3c28..795a6e1 100644 --- a/iotclient/iot_client.py +++ b/iotclient/iot_client.py @@ -42,6 +42,35 @@ def send_image(identifier, image_path, metadata_payload): return True +def update_image(identifier, image_path, metadata_payload): + print("Updating image with identifier " + identifier) + if not os.path.isfile(image_path): + print("No image found at path " + image_path) + return False + file_encoded_b64 = None + with open(image_path, "rb") as file_to_send: + file_encoded_b64 = base64.b64encode(file_to_send.read()) + + if file_encoded_b64 is None: + print("Error reading file") + return False + + file_payload = file_encoded_b64.decode('utf-8') + baseurl = "http://127.0.0.1:8000" + post_url = "/image/update/" + identifier + body = { + 'id': identifier, + 'metadata': metadata_payload, + 'image_data': file_payload + } + try: + response = requests.put(baseurl + post_url, json=body) + print_response(response) + except os.error: + print("Error sending request") + return True + + def get_image(identifier): print("Getting image with identifier " + identifier) @@ -190,6 +219,7 @@ while (command.lower() not in ["exit", "quit", "end"]): print("imagefolder - selected new image folder") print("show - opens image next in line in default os program") print("trigger - next image in line is sent to backend") + print("update - next image in line is updated in backend") print("fetch - gets the next image from database if exists") print("fetchall - get all images") print("delete - delets the next image from db if exists") @@ -272,6 +302,19 @@ while (command.lower() not in ["exit", "quit", "end"]): index = index + 1 print_cursor() + if command.lower() == "update": + if metadata is None: + print("No metadata loaded") + continue + if image_folder is None: + print("No image folder selected") + continue + meta_payload = metadata[index] + filename = meta_payload['filename'] + update_image(filename[:-4], image_folder + os.path.sep + filename, meta_payload) + index = index + 1 + print_cursor() + if command.lower() == "fetch": if metadata is None: print("No metadata loaded") diff --git a/middleware/app_be/services/mongodbservice.py b/middleware/app_be/services/mongodbservice.py index 003ed5d..7d8017f 100644 --- a/middleware/app_be/services/mongodbservice.py +++ b/middleware/app_be/services/mongodbservice.py @@ -42,7 +42,7 @@ class MongoDBService: except: print("Could not find Metadata") if metadata is None: - return JsonResponse({'Result': 'Error - could not find metadata.'}, status=404, safe=False) + return None else: if '_id' in metadata: del metadata['_id'] diff --git a/middleware/app_be/urls.py b/middleware/app_be/urls.py index 367403f..13fa166 100644 --- a/middleware/app_be/urls.py +++ b/middleware/app_be/urls.py @@ -28,6 +28,7 @@ urlpatterns = [ url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all), url(r'^image/delete/(?P[\w-]+)$', ImageEndpoint.image_api_delete), url(r'^image/post$', ImageEndpoint.image_api_post), + url(r'^image/update/(?P[\w-]+)$', ImageEndpoint.image_api_update), url(r'^check', HealthEndpoint.check_systems) ] diff --git a/middleware/app_be/views/rest_api.py b/middleware/app_be/views/rest_api.py index 7b73f87..3cfb9c2 100644 --- a/middleware/app_be/views/rest_api.py +++ b/middleware/app_be/views/rest_api.py @@ -142,6 +142,40 @@ class ImageEndpoint: return JsonResponse({'Result': result_bool}, safe=False) + @staticmethod + @api_view(['PUT']) + def image_api_update(request, identifier): + + logger.debug('Image UPDATE single call: {}'.format(request)) + + # get metadata from MongoDB + metadata = MongoDBService.getSingle(identifier) + if not metadata: + return JsonResponse({'Result': 'Error - Could not find image to be updated', + 'id': identifier}, status=404, safe=False) + + DropboxService.delete_file(metadata['filename']) + # MinioService.delete_file(metadata['filename']) + MongoDBService.deleteSingle(identifier) + + + payload = json.loads(request.body) + b64encoded_image = payload['image_data'] + identifier = payload['id'] + metadata = payload['metadata'] + filename = payload['metadata']['filename'] + + decoded_image = WrapperService.unwrap_file(b64encoded_image) + + MongoDBService.createSingle(metadata, identifier, decoded_image) + + if not DropboxService.create_file(filename, decoded_image): + print("Could not save image to dropbox") + if not MinioService.create_file(filename, decoded_image): + print("Could not save image to minio") + return JsonResponse({'id': identifier, 'filename': filename}, + safe=False) + class HealthEndpoint: @staticmethod @api_view(['GET'])