def post(self): f = request.files['file'] title = request.form["title"] content = request.form["content"] img_name = request.form["img_name"] s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=ACCESS_SECRET_KEY, config=Config(signature_version='s3v4')) # # Image Uploaded s3.Bucket(BUCKET_NAME).put_object(Key=img_name, Body=f, ACL='public-read') img_url = 'https://jkhatter-flask.s3-us-west-2.amazonaws.com/%s' % ( img_name) print(img_url) safe = image_check(img_url) # if BlogModel.find_by_id(id): # return({"message":"An item with name {} already exists.".format(title)}) if safe > 0.50: blog = BlogModel(title, content, img_name, img_url) try: blog.insert_update() except: return ({ "message": "An error occurred in inserting the item." }), 500 #internal server error return blog.json(), 201 else: return ({"message": "Image contains nudity content."}), 400
def put(self, id): f = request.files['file'] title = request.form["title"] content = request.form["content"] img_name = request.form["img_name"] blog = BlogModel.find_by_id(id) s3 = boto3.resource('s3', aws_access_key_id=ACCESS_KEY_ID, aws_secret_access_key=ACCESS_SECRET_KEY, config=Config(signature_version='s3v4')) # This will delete the old image obj = s3.Object(BUCKET_NAME, img_name) obj.delete() print("Delete done") # This will upload the new image s3.Bucket(BUCKET_NAME).put_object(Key=img_name, Body=f, ACL='public-read') img_url = 'https://jkhatter-flask.s3-us-west-2.amazonaws.com/%s' % ( img_name) print(img_url) safe = image_check(img_url) print(safe) if safe > 0.50: if blog is None: blog = BlogModel(title, content, img_name, img_url) else: blog.title = title blog.content = content blog.img_name = img_name blog.img_url = img_url blog.insert_update() return blog.json() else: return ({"message": "Image contains nudity content."}) return True