Added update (pre minio delete)
This commit is contained in:
parent
f313d18313
commit
c440b66894
@ -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 <path> - 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")
|
||||
|
||||
@ -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']
|
||||
|
||||
@ -28,6 +28,7 @@ urlpatterns = [
|
||||
url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all),
|
||||
url(r'^image/delete/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_delete),
|
||||
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)
|
||||
]
|
||||
|
||||
|
||||
@ -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'])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user