def delete(): requestData = request.get_json() response = {} # Check if the body has the image or delete-all attributes if not requestData or ('image' not in requestData and 'delete-all' not in requestData): response["error"] = 'Input image or delete-all attribute in the body' return jsonify(response), status.HTTP_400_BAD_REQUEST imagesToDelete = [] # Populate the imagesToDelete list if 'delete-all' in requestData and requestData['delete-all'] == 'true': allImages = ImageDatabase.scan() for image in allImages["Items"]: imagesToDelete.append(image["imageName"]) elif 'image' in requestData: if isinstance(requestData['image'], list): imagesToDelete = requestData['image'] else: imagesToDelete.append(requestData['image']) # Delete the specified images response["deleted_images"] = [] for imageToDelete in imagesToDelete: deletedImage = {} deletedImage["image"] = imageToDelete deletedImage["status"] = delete_image(imageToDelete) response["deleted_images"].append(deletedImage) return response
def text_search(searchKeyword, response): # Search the characteristics of the image and the image name responseDDB = ImageDatabase.scan( FilterExpression=Attr('imageLabels').contains(searchKeyword.title()) | Attr('imageName').contains(searchKeyword)) populate_response(responseDDB, response)
def delete_image(imageToDelete): # Make sure that the image exists in the database responseDDB = ImageDatabase.query( KeyConditionExpression=Key('imageName').eq(imageToDelete)) if not responseDDB["Items"]: return "Image '" + imageToDelete + "' does not exist in the database" if responseDDB["Items"][0]["uploadedBy"] != global_vars.currentUser: return "Image '" + imageToDelete + "' not uploaded by " + global_vars.currentUser + ". Image not deleted" responseS3 = s3Client.delete_object(Bucket=imageDatabaseBucket, Key=imageToDelete) if responseS3['ResponseMetadata']['HTTPStatusCode'] != 204: return "Error while deleting the image '" + imageToDelete + "' from the S3 bucket" responseDDB = ImageDatabase.delete_item(Key={"imageName": imageToDelete}) if responseDDB['ResponseMetadata']['HTTPStatusCode'] != 200: return "Error while deleting database entry for the image '" + imageToDelete + "' from the DynamoDB table" return "Successfuly deleted image '" + imageToDelete + "'"
def add_to_s3_and_ddb(uploadedFile, labels, permission): fileName = uploadedFile.filename uploadedFile.seek(0) s3Client.upload_fileobj(uploadedFile, imageDatabaseBucket, fileName) s3Resource.Object(imageDatabaseBucket, fileName).wait_until_exists() response = ImageDatabase.put_item( Item={ 'imageName': fileName, 'imageLabels': labels, 'permission': permission, 'uploadedBy': global_vars.currentUser })
def verify_and_add_image(uploadedFile, permission): fileName = uploadedFile.filename if not "image" in uploadedFile.mimetype: return "File '" + fileName + "' is not an image" # Make sure that the image does not exist in the database response = ImageDatabase.query( KeyConditionExpression=Key('imageName').eq(fileName)) if response["Items"]: return "Image with name " + fileName + " already exists in the database" labels = get_labels(uploadedFile) add_to_s3_and_ddb(uploadedFile, labels, permission) return "Added image '" + fileName + "' to the image repository."
def image_search(uploadedFile, response): fileName = uploadedFile.filename if not "image" in uploadedFile.mimetype: return "File '" + fileName + "' is not an image" labels = get_labels(uploadedFile) preparedFilterExpression = "" preparedExpressionAttrValues = {} for idx, label in enumerate(labels): if preparedFilterExpression: preparedFilterExpression += " or " preparedFilterExpression += "contains(imageLabels, :label" + str( idx) + ")" preparedExpressionAttrValues[":label" + str(idx)] = label responseDDB = ImageDatabase.scan( FilterExpression=preparedFilterExpression, ExpressionAttributeValues=preparedExpressionAttrValues) populate_response(responseDDB, response)
def get_all_images(response): responseDDB = ImageDatabase.scan() populate_response(responseDDB, response)