remove try catch, it is catched a step above to keep the exception information;
added additional parse try with: -payload = request.data.dict() -payload['metadata'] = json.loads(payload['metadata']); update metadata on update; prohibit . in filename;
This commit is contained in:
@@ -23,7 +23,6 @@ class TestApiClass:
|
||||
|
||||
|
||||
class ImageEndpoint:
|
||||
|
||||
storageServiceList = [DropboxService, MinioService]
|
||||
|
||||
@staticmethod
|
||||
@@ -41,7 +40,7 @@ class ImageEndpoint:
|
||||
@api_view(['GET'])
|
||||
def image_api_get_single_version(request, identifier, version):
|
||||
logger.debug('Image GET single with version call: {}'.format(request))
|
||||
return ImageEndpoint.get_image(identifier+'_'+version)
|
||||
return ImageEndpoint.get_image(identifier + '_' + version)
|
||||
|
||||
@staticmethod
|
||||
@api_view(['GET'])
|
||||
@@ -100,7 +99,7 @@ class ImageEndpoint:
|
||||
oldest_last_modified = modified
|
||||
service_with_oldest = service
|
||||
|
||||
if service_with_oldest is None: # failsave if no image is available
|
||||
if service_with_oldest is None: # failsave if no image is available
|
||||
return JsonResponse({'Result': 'Error - image is not available on any storage service',
|
||||
'id': identifier}, status=404, safe=False)
|
||||
# replace image on every service with oldest available
|
||||
@@ -129,7 +128,17 @@ class ImageEndpoint:
|
||||
@api_view(['POST'])
|
||||
def image_api_post(request: HttpRequest):
|
||||
logger.debug('Image POST call: {}'.format(request))
|
||||
payload = json.loads(request.body)
|
||||
|
||||
try:
|
||||
payload = json.loads(request.body)
|
||||
except json.decoder.JSONDecodeError:
|
||||
try:
|
||||
payload = request.data.dict()
|
||||
payload['metadata'] = json.loads(payload['metadata'])
|
||||
except Exception as e:
|
||||
return JsonResponse({'Result': 'Error - could not parse uploaded data',
|
||||
'Error': str(e)}, status=500, safe=False)
|
||||
|
||||
b64encoded_image = payload['image_data']
|
||||
identifier = payload['id']
|
||||
metadata = payload['metadata']
|
||||
@@ -139,16 +148,21 @@ class ImageEndpoint:
|
||||
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_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)
|
||||
try:
|
||||
MongoDBService.createSingle(metadata, identifier, decoded_image)
|
||||
except Exception as e:
|
||||
print("Could not save metadata:", e)
|
||||
return JsonResponse({'Result': 'Error - could not upload to MongoDB',
|
||||
'Error': str(e), 'id': identifier, 'filename': filename}, status=500, safe=False)
|
||||
|
||||
for service in ImageEndpoint.storageServiceList:
|
||||
if not service.create_file(filename, decoded_image):
|
||||
print("Could not save image to " + service.name)
|
||||
return JsonResponse({'Result': 'Error - could not upload to ' + service.name, 'id': identifier, 'filename': filename},status=500, safe=False)
|
||||
return JsonResponse(
|
||||
{'Result': 'Error - could not upload to ' + service.name, 'id': identifier, 'filename': filename},
|
||||
status=500, safe=False)
|
||||
|
||||
return JsonResponse({'id': identifier, 'filename': filename},safe=False)
|
||||
return JsonResponse({'id': identifier, 'filename': filename}, safe=False)
|
||||
|
||||
@staticmethod
|
||||
@api_view(['DELETE'])
|
||||
@@ -196,7 +210,17 @@ class ImageEndpoint:
|
||||
def image_api_update(request, identifier):
|
||||
logger.debug('Image UPDATE single call: {}'.format(request))
|
||||
|
||||
payload = json.loads(request.body)
|
||||
|
||||
try:
|
||||
payload = json.loads(request.body)
|
||||
except json.decoder.JSONDecodeError:
|
||||
try:
|
||||
payload = request.data.dict()
|
||||
payload['metadata'] = json.loads(payload['metadata'])
|
||||
except Exception as e:
|
||||
return JsonResponse({'Result': 'Error - could not parse uploaded data',
|
||||
'Error': str(e)}, status=500, safe=False)
|
||||
|
||||
b64encoded_image = payload['image_data']
|
||||
identifier = payload['id']
|
||||
metadata = payload['metadata']
|
||||
@@ -212,7 +236,7 @@ class ImageEndpoint:
|
||||
|
||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||
|
||||
MongoDBService.updateSingle(identifier, decoded_image)
|
||||
MongoDBService.updateSingle(identifier, decoded_image, metadata)
|
||||
|
||||
for service in ImageEndpoint.storageServiceList:
|
||||
orig_new_name = identifier + '_' + str(metadata['version']) + '.jpg'
|
||||
@@ -240,7 +264,6 @@ class ImageEndpoint:
|
||||
|
||||
|
||||
class HealthEndpoint:
|
||||
|
||||
storageServiceList = [DropboxService, MinioService]
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user