Added update (pre minio delete)

This commit is contained in:
Martin Schett 2020-12-06 19:36:30 +01:00
parent f313d18313
commit c440b66894
4 changed files with 79 additions and 1 deletions

View File

@ -42,6 +42,35 @@ def send_image(identifier, image_path, metadata_payload):
return True 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): def get_image(identifier):
print("Getting image with identifier " + identifier) print("Getting image with identifier " + identifier)
@ -190,6 +219,7 @@ while (command.lower() not in ["exit", "quit", "end"]):
print("imagefolder <path> - selected new image folder") print("imagefolder <path> - selected new image folder")
print("show - opens image next in line in default os program") print("show - opens image next in line in default os program")
print("trigger - next image in line is sent to backend") 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("fetch - gets the next image from database if exists")
print("fetchall - get all images") print("fetchall - get all images")
print("delete - delets the next image from db if exists") 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 index = index + 1
print_cursor() 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 command.lower() == "fetch":
if metadata is None: if metadata is None:
print("No metadata loaded") print("No metadata loaded")

View File

@ -42,7 +42,7 @@ class MongoDBService:
except: except:
print("Could not find Metadata") print("Could not find Metadata")
if metadata is None: if metadata is None:
return JsonResponse({'Result': 'Error - could not find metadata.'}, status=404, safe=False) return None
else: else:
if '_id' in metadata: if '_id' in metadata:
del metadata['_id'] del metadata['_id']

View File

@ -28,6 +28,7 @@ urlpatterns = [
url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all), url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all),
url(r'^image/delete/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_delete), url(r'^image/delete/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_delete),
url(r'^image/post$', ImageEndpoint.image_api_post), url(r'^image/post$', ImageEndpoint.image_api_post),
url(r'^image/update/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_update),
url(r'^check', HealthEndpoint.check_systems) url(r'^check', HealthEndpoint.check_systems)
] ]

View File

@ -142,6 +142,40 @@ class ImageEndpoint:
return JsonResponse({'Result': result_bool}, safe=False) 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: class HealthEndpoint:
@staticmethod @staticmethod
@api_view(['GET']) @api_view(['GET'])