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:
Marco Zeisler 2021-01-15 23:41:35 +01:00
parent a54fc5eb01
commit 95180bb8e7
2 changed files with 42 additions and 24 deletions

View File

@ -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'}})

View File

@ -23,7 +23,6 @@ class TestApiClass:
class ImageEndpoint: class ImageEndpoint:
storageServiceList = [DropboxService, MinioService] storageServiceList = [DropboxService, MinioService]
@staticmethod @staticmethod
@ -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))
try:
payload = json.loads(request.body) 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,14 +148,19 @@ 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)
@ -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))
try:
payload = json.loads(request.body) 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