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:
parent
a54fc5eb01
commit
95180bb8e7
@ -60,20 +60,13 @@ class MongoDBService:
|
|||||||
db = instance.AIC
|
db = instance.AIC
|
||||||
col = db.metadata
|
col = db.metadata
|
||||||
|
|
||||||
try:
|
metadata['identifier'] = identifier
|
||||||
metadata['identifier'] = identifier
|
metadata['location'] = [metadata['longitude'], metadata['latitude']]
|
||||||
metadata['location'] = [metadata['longitude'], metadata['latitude']]
|
metadata['sha512'] = create_sha512(decoded_image)
|
||||||
metadata['sha512'] = create_sha512(decoded_image)
|
col.insert_one(metadata)
|
||||||
col.insert_one(metadata)
|
|
||||||
|
|
||||||
except:
|
|
||||||
print("Could not insert Metadata")
|
|
||||||
return False
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def updateSingle(identifier, decoded_image) -> bool:
|
def updateSingle(identifier, decoded_image, meta=None) -> bool:
|
||||||
print("MongoDBService: Trying to update file with identifier " + identifier)
|
print("MongoDBService: Trying to update file with identifier " + identifier)
|
||||||
instance = MongoManager.getInstance()
|
instance = MongoManager.getInstance()
|
||||||
db = instance.AIC
|
db = instance.AIC
|
||||||
@ -87,6 +80,8 @@ class MongoDBService:
|
|||||||
identifier_changed = identifier + '_' + str(metadata_orig['version']) # Set identifier to include version
|
identifier_changed = identifier + '_' + str(metadata_orig['version']) # Set identifier to include version
|
||||||
metadata_new['version'] = str(int(metadata_new['version']) + 1) # Increment version by one
|
metadata_new['version'] = str(int(metadata_new['version']) + 1) # Increment version by one
|
||||||
|
|
||||||
|
metadata_new.update(meta)
|
||||||
|
|
||||||
print("MongoDBService: identifier_changed: ", identifier_changed)
|
print("MongoDBService: identifier_changed: ", identifier_changed)
|
||||||
col.update_one(old, {"$set": {"identifier": identifier_changed, "filename": identifier_changed + '.jpg'}})
|
col.update_one(old, {"$set": {"identifier": identifier_changed, "filename": identifier_changed + '.jpg'}})
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,6 @@ class TestApiClass:
|
|||||||
|
|
||||||
|
|
||||||
class ImageEndpoint:
|
class ImageEndpoint:
|
||||||
|
|
||||||
storageServiceList = [DropboxService, MinioService]
|
storageServiceList = [DropboxService, MinioService]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -41,7 +40,7 @@ class ImageEndpoint:
|
|||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
def image_api_get_single_version(request, identifier, version):
|
def image_api_get_single_version(request, identifier, version):
|
||||||
logger.debug('Image GET single with version call: {}'.format(request))
|
logger.debug('Image GET single with version call: {}'.format(request))
|
||||||
return ImageEndpoint.get_image(identifier+'_'+version)
|
return ImageEndpoint.get_image(identifier + '_' + version)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@api_view(['GET'])
|
@api_view(['GET'])
|
||||||
@ -100,7 +99,7 @@ class ImageEndpoint:
|
|||||||
oldest_last_modified = modified
|
oldest_last_modified = modified
|
||||||
service_with_oldest = service
|
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',
|
return JsonResponse({'Result': 'Error - image is not available on any storage service',
|
||||||
'id': identifier}, status=404, safe=False)
|
'id': identifier}, status=404, safe=False)
|
||||||
# replace image on every service with oldest available
|
# replace image on every service with oldest available
|
||||||
@ -129,7 +128,17 @@ class ImageEndpoint:
|
|||||||
@api_view(['POST'])
|
@api_view(['POST'])
|
||||||
def image_api_post(request: HttpRequest):
|
def image_api_post(request: HttpRequest):
|
||||||
logger.debug('Image POST call: {}'.format(request))
|
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']
|
b64encoded_image = payload['image_data']
|
||||||
identifier = payload['id']
|
identifier = payload['id']
|
||||||
metadata = payload['metadata']
|
metadata = payload['metadata']
|
||||||
@ -139,16 +148,21 @@ class ImageEndpoint:
|
|||||||
|
|
||||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||||
|
|
||||||
if not MongoDBService.createSingle(metadata, identifier, decoded_image):
|
try:
|
||||||
print("Could not save metadata")
|
MongoDBService.createSingle(metadata, identifier, decoded_image)
|
||||||
return JsonResponse({'Result': 'Error - could not upload to MongoDB', 'id': identifier, 'filename': filename},status=500,safe=False)
|
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:
|
for service in ImageEndpoint.storageServiceList:
|
||||||
if not service.create_file(filename, decoded_image):
|
if not service.create_file(filename, decoded_image):
|
||||||
print("Could not save image to " + service.name)
|
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
|
@staticmethod
|
||||||
@api_view(['DELETE'])
|
@api_view(['DELETE'])
|
||||||
@ -196,7 +210,17 @@ class ImageEndpoint:
|
|||||||
def image_api_update(request, identifier):
|
def image_api_update(request, identifier):
|
||||||
logger.debug('Image UPDATE single call: {}'.format(request))
|
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']
|
b64encoded_image = payload['image_data']
|
||||||
identifier = payload['id']
|
identifier = payload['id']
|
||||||
metadata = payload['metadata']
|
metadata = payload['metadata']
|
||||||
@ -212,7 +236,7 @@ class ImageEndpoint:
|
|||||||
|
|
||||||
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
decoded_image = WrapperService.unwrap_file(b64encoded_image)
|
||||||
|
|
||||||
MongoDBService.updateSingle(identifier, decoded_image)
|
MongoDBService.updateSingle(identifier, decoded_image, metadata)
|
||||||
|
|
||||||
for service in ImageEndpoint.storageServiceList:
|
for service in ImageEndpoint.storageServiceList:
|
||||||
orig_new_name = identifier + '_' + str(metadata['version']) + '.jpg'
|
orig_new_name = identifier + '_' + str(metadata['version']) + '.jpg'
|
||||||
@ -240,7 +264,6 @@ class ImageEndpoint:
|
|||||||
|
|
||||||
|
|
||||||
class HealthEndpoint:
|
class HealthEndpoint:
|
||||||
|
|
||||||
storageServiceList = [DropboxService, MinioService]
|
storageServiceList = [DropboxService, MinioService]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user