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
|
||||
col = db.metadata
|
||||
|
||||
try:
|
||||
metadata['identifier'] = identifier
|
||||
metadata['location'] = [metadata['longitude'], metadata['latitude']]
|
||||
metadata['sha512'] = create_sha512(decoded_image)
|
||||
col.insert_one(metadata)
|
||||
|
||||
except:
|
||||
print("Could not insert Metadata")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@staticmethod
|
||||
def updateSingle(identifier, decoded_image) -> bool:
|
||||
def updateSingle(identifier, decoded_image, meta=None) -> bool:
|
||||
print("MongoDBService: Trying to update file with identifier " + identifier)
|
||||
instance = MongoManager.getInstance()
|
||||
db = instance.AIC
|
||||
@ -87,6 +80,8 @@ class MongoDBService:
|
||||
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.update(meta)
|
||||
|
||||
print("MongoDBService: identifier_changed: ", identifier_changed)
|
||||
col.update_one(old, {"$set": {"identifier": identifier_changed, "filename": identifier_changed + '.jpg'}})
|
||||
|
||||
|
||||
@ -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'])
|
||||
@ -129,7 +128,17 @@ class ImageEndpoint:
|
||||
@api_view(['POST'])
|
||||
def image_api_post(request: HttpRequest):
|
||||
logger.debug('Image POST call: {}'.format(request))
|
||||
|
||||
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))
|
||||
|
||||
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user