def put(cls): data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return { "message": gettext("avatar_uploaded").format(basename) }, 200 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def put(cls): """ This endpoint is used to upload user avatars. Uploading a new avatar overwrites the existing one. """ data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = AVATAR_FOLDER avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: image = data["image"] ext = image_helper.get_extension(image.filename) avatar_name = filename + ext avatar_path = image_helper.save_image(image, folder, avatar_name) basename = image_helper.get_basename(avatar_path) return {"message": gettext("avatar_uploaded")}, 200 except UploadNotAllowed: extension = image_helper.get_extension(image) return { "message": gettext("image_extension_not_allowed").format(extension) }, 400
def put(cls): """ Upload user avatar. Users have unique avatar. """ data = request.files filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": "Failed to delete avatar"}, 500 try: ext = image_helper.get_extension(["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_image_basename(avatar_path) return {"message": f"Uploaded {basename}"}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": f"Extension {extension} not allowed"}, 400
def put(self): """This endpoint is used to upload user avatars all avatars are called after the user id uploading a new avatar overwrite the last one """ data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar) return {"message": gettext("avatar_uploaded").format(basename)} except UploadNotAllowed: extenstion = image_helper.get_extension(avatar_path) return { "message": gettext("image_illegal_extension").format(extenstion) }
def put(cls): """All Avatars are named after user's ID. Something like this: user_{id}.{ext} """ data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = 'avatars' avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {'message': getext('avatar_delete_failed')}, 500 try: ext = image_helper.get_extension(data['image'].filename) avatar = filename + ext avatar_path = image_helper.save_image(data['image'], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return {'message': getext('avatar_uploaded').format(basename)}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return { 'message': getext('image_illegal_extension').format(extension) }, 400
def put(cls): """Endpoint used to upload user avatar, named after user ID. Uploding new avatar overwrites existing""" data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {'message': gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return { 'message': gettext("avatar_uploaded").format(basename) }, 200 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { 'message': gettext("image_illegal_extension").format(extension) }
def put(cls): """ Used to upload a user's Avatar. All Avatars are named after the user's ID, example: user_{id}.{ext}. Uploading a new Avatar overwrites the existing one. """ data = imageSchema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": "Avatar Delete Failed."}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return {"message": f"Avatar '{basename}' uploaded."}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return {"message": f"Extension '{extension}' is not allowed."}, 400
def put(cls): """ This endpoint is used to upload user avatars. All ovatars are named after user's ID. Something like thisL user_{id}.ext Uploading a new avatar overwrites the existing one. """ data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data['image'].filename) avatar = filename + ext avatar_path = image_helper.save_image( data['image'], folder=folder, name=avatar ) basename = image_helper.get_basename(avatar_path) return {"message": gettext("avatar_uploaded").format(basename)}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return {"message": gettext("image_illegal_extension").format(extension)}, 400
def put(cls): """Upload user avatar. New avatar overwrites the existing one""" data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format( filename, folder) #check if exists in any format if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext avatar_path = image_helper.save_image( data["image"], folder=folder, name=avatar) # overwrite name with avatar variable basename = image_helper.get_basename(avatar_path) return {"messge": gettext("avatar_uploaded").format(basename)}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def put(self): """ This endpoint is used to upload user avatar. All avatars are named after the user's id in such format: user_{id}.{ext}. It will overwrite the existing avatar. """ data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": gettext("avatar_delete_failed")}, 500 try: ext = image_helper.get_extension(data["image"].filename) avatar = filename + ext # use our naming format + true extension avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return { "message": gettext("avatar_uploaded").format(basename) }, 200 except UploadNotAllowed: #forbidden file type extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def put(cls): """ this endpoint is for uploading avatars. all avatars relate to user id overwrites existing avatars :return: """ data = image_schema.load(request.files) # {" image:fileStorage} filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: traceback.print_exc() return {"message": gettext("avatar_delete_failed")}, 500 try: extension = image_helper.get_extension(data["image"].filename) avatar = filename + extension avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return { "message": gettext("avatar_uploaded").format(basename) }, 200 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def put(self): """ This endpoint is used to upload user_avatars. All avatars are named after the User's ID. """ data = profile_update_schema.load(request.files) description = request.form["description"] filename = f"user_{get_jwt_identity()}" user = UserModel.find_by_id(get_jwt_identity()) if description: try: user.description = description user.save_to_db() except: return {"message": "There was an issue updating your profile."} else: user.description = None user.save_to_db() if "image" in data.keys(): try: if not image_helper.is_filename_safe(data["image"].filename): return {"message": "Your file name is invalid."}, 401 ext = image_helper.get_extension(data["image"].filename) if ext not in allowed_extensions: return { "message": "Please Only Upload .jpg, .png, or .jpeg files." }, 401 for ext in allowed_extensions: try: if s3.get_object(Bucket="brewcipes", Key="profile_pics/" + filename + ext): s3.delete_object(Bucket="brewcipes", Key="profile_pics/" + filename + ext) user.profile_pic_link = None user.save_to_db() break except: continue avatar = filename + ext s3.upload_fileobj(data["image"], bucket_name, "profile_pics/" + avatar) user.profile_pic_link = "https://brewcipes.s3-us-west-2.amazonaws.com/profile_pics/{}".format( avatar) user.save_to_db() except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": IMAGE_ILLEGAL_EXTENSION.format(extension) }, 400 return {"message": "Profile successfully updated."}, 200
def create(): if request.method == 'POST': title = request.form['title'] body = request.form['body'] image_file = request.files['file'] error = None if not title: error = 'Title is required.' if error is not None: flash(error) if not image_file: flash("Please Attach a file") else: folder = f"user_{g.user.id}" # static/images try: image_path = image_helper.save_image(image_file, folder=folder) basename = image_helper.get_path(image_path) userModel = UserModel.find_user_by_id(g.user.id) post = PostModel(title=title, posts=body, image_url=image_path, user_id=userModel.id) post.save_to_db() except UploadNotAllowed: extension = image_helper.get_extension(image_file) flash("file with extension {} not allowed".format(extension)) return redirect(url_for('blog.index')) return render_template('blog/create.html')
def post(cls): """ This endpoint is used to upload an image file. It uses the JWT to retrieve user information and save the image in the user's folder. If a file with the same name exists in the user's folder, name conflicts will be automatically resolved by appending a underscore and a smallest unused integer. (eg. filename.png to filename_1.png). """ data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}/Prediction" try: # save(self, storage, folder=None, name=None) image_helper.save_image(data["image"], folder=folder) run = ml_model.MLModel.predict(data["image"]) prediction = "" for x in range(1, len(run[0]) + 1): prediction = prediction + " Prediction # " + str( x) + ": " + run[1][x - 1] + " " + str(run[0][x - 1]) + "," # here we only return the basename of the image and hide the internal folder structure from our user # basename = image_helper.get_basename(image_path) return {"message": "{}".format(prediction)}, 201 except UploadNotAllowed: # forbidden file type extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def put(cls, slug): """ Assign image to specific article instance. """ article = ArticleModel.find_by_slug(slug) if not article: return {"message": "Article does not exist"}, 404 data = image_schema.load(request.files) folder = "article_images" try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) previous_article_image = article.image_url article.image_url = basename article.save_to_db() if previous_article_image: image_helper.delete_image(previous_article_image, folder) return {"message": "Image {} uploaded".format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": "Extension not allowed"}, 400 except Exception: traceback.print_exc() return {"message": "Internal server error"}, 500
def update(id): user = UserModel.find_user_by_id(id) if not user: flash("User doesn't exist") if request.method == 'POST': full_names = request.form['full_names'] image_file = request.files['file'] error = None if not full_names: error = "Full Names Required" if error is not None: flash(error) if not image_file: flash("Please Attach a file") else: filename = f"user_{g.user.id}" folder = "avatars" try: ext = image_helper.get_extension(image_file.filename) avatar = filename + ext image_path = image_helper.save_image(image_file, folder=folder, name=avatar) basename = image_helper.get_path(image_path) user.full_names = full_names user.profile_pic = image_path user.save_to_db() #post = PostModel(title=title, posts=body, image_url=image_path, user_id=userModel.id) #post.save_to_db() except UploadNotAllowed: extension = image_helper.get_extension(image_file) flash("file with extension {} not allowed".format(extension)) return redirect(url_for('profile.get', id=user.id)) return render_template('/profile/edit_profile.html', user=user)
def post(cls): data = image_schema.load(request.files) # {"image" : FileStorage} user_id = get_jwt_identity() folder = "user_{}".format(user_id) try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) return {"message": IMAGE_UPLOADED}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": IMAGE_ILLEGAL_EXTENSION}, 400
def post(self): data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) return {"message": gettext("image_uploaded").format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": gettext("image_illegal_extension").format(extension)}, 400
def create(): bucket = 'rosius' content_type = request.mimetype if request.method == 'POST': title = request.form['title'] body = request.form['body'] image_file = request.files['file'] client = boto3.client( 's3', region_name='us-east-2', endpoint_url='https://s3.us-east-2.amazonaws.com', aws_access_key_id=os.environ.get('AWS_ACCESS_KEY'), aws_secret_access_key=os.environ.get('AWS_SECRET_KEY')) error = None if not title: error = 'Title is required.' if error is not None: flash(error) if not image_file: flash("Please Attach a file") else: folder = f"user_{g.user.id}" # static/images try: if image_helper.is_filename_safe(image_file): client.put_object(Body=image_file, Bucket=bucket, Key=image_file.filename, ACL="public-read", ContentType=content_type) #image_path = image_helper.save_image(image_file,folder=folder) #basename = image_helper.get_path(image_path) print("https://rosius.s3.us-east-2.amazonaws.com/" + image_file.filename) userModel = UserModel.find_user_by_id(g.user.id) post = PostModel( title=title, posts=body, image_url="https://rosius.s3.us-east-2.amazonaws.com/" + image_file.filename, user_id=userModel.id) post.save_to_db() except UploadNotAllowed: extension = image_helper.get_extension(image_file) flash("file with extension {} not allowed".format(extension)) return redirect(url_for('blog.index')) return render_template('blog/create.html')
def put(cls): data = image_schema.load(request.files) filename = f"user_{get_jwt_identity()}" folder = "avatars" avatar_path = image_helper.find_image_any_format(filename, folder) if avatar_path: try: os.remove(avatar_path) except: return {"message": AVATAR_DELETE_FAILED}, 500 try: ext = image_helper.get_extension(data['image'].filename) avatar = filename + ext avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar) basename = image_helper.get_basename(avatar_path) return {"message": AVATAR_UPLOADED.format(basename)}, 200 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return {"message": IMAGE_ILLEGAL_EXTENSION.format(extension)}, 400
def create_item(): if request.method == "POST": name = request.form['name'] model_id = request.args.get('model_id') #image logic username = session['username'] folder = f"user_{username}" # validation and sanity checks if model_id is None: return "Missing model number" if 'file1' not in request.files: return "Missing file part" if ItemModel.find_by_storeid_and_itemname(model_id, name): return gettext("web_items_alredyexists") file1 = request.files['file1'] #data = image_schema.load(file1) try: #image_path = image_helper.save_image(data["image"], folder=folder) image_path = image_helper.save_image(file1, folder=os.path.join( folder, model_id)) logger.info(image_path) basename = image_helper.get_basename(image_path) logger.info(basename) except UploadNotAllowed: # forbidden file type extension = image_helper.get_extension(file1) return f"Unable to upload the extension {extension}" #create item in database item = ItemModel(name=name, filename=file1.filename, objname=image_path, store_id=model_id) print(item) try: item.save_to_db() except: return "Error creating item" return redirect(url_for(".index", model_id=model_id)) #Get implementation model_id = request.args.get('model_id') return render_template("items/new_item.html", model_id=model_id)
def post(cls): """Used to upload an image file, save image to user's folder""" data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}" # e.g., static/images/user_1 try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) return {"message": gettext("image_uploaded").format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def post(cls): data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}/Post" f = open("imagename.txt", "r") content = f.read() ext = image_helper.get_extension(data["image"].filename) Image = content + ext try: # save(self, storage, folder=None, name=None) image_helper.save_image(data["image"], folder=folder, name=Image) print(Image) f.close() d = open("imagename.txt", "w+") d.write("") d.close() # here we only return the basename of the image and hide the internal folder structure from our user return {"message": "post image uplaod successfully."}, 201 except UploadNotAllowed: # forbidden file type extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def post(cls): data = image_schema.load(request.files) # {'image': FileStorage} user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(data['image'], folder=folder) basename = image_helper.get_basename(image_path) return {'message': getext('image_uploaded').format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return { 'message': getext('image_illegal_extension').format(extension) }, 400
def post(cls): """ Used to upload an image file. It uses JWT to retrieve user information and then saves the image to the user's folder. If there is a filename conflict, it appends a number at the end. """ data = imageSchema.load(request.files) # {"image": FileStorage} user_id = get_jwt_identity() folder = f"user_{user_id}" #/static/images/user_1 try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) return {"message": f"Image '{basename}' Uploaded."}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": f"Extension '{extension}' is not allowed."}, 400
def post(cls): """ Upload image file """ data = request.files # FileStorage obj # user_id = get_jwt_identity() folder = f"user_{user_id}" # static/images/user_1 try: image_path = image_helper.save_image(data['image'], folder=folder) basename = image_helper.get_image_basename(image_path) return {'message': f"File: {basename} Uploaded"}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return {"message": f"{extension} not allowed"}, 400
def post(cls): """ Used to upload an image file. It uses JWT to retrieve user information and then saves the image to the uses's folder. If there is a filename conflict, it appends a number at the end. """ data = image_schema.load(request.files) # {'image': FileStorage} user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(data['image'], folder=folder) # data['image'].filename + "_1" basename = image_helper.get_basename(image_path) return {'message': gettext("image_uploaded").format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data['image']) return {'message': gettext("image_illegal_extension").format(extension)}, 400
def post(self): """ Used to upload an image file. It uses JWT to retrieve user information and then saves the image to the users folder. If there is a filename conflict,it appends a number at the end """ data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(data["image"], folder=folder) basename = image_helper.get_basename(image_path) return {"message": IMAGE_UPLOADED.format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return {"message": IMAGE_ILLEGAL_EXTENSION.format(extension)}, 400
def post(cls): """Verify JWT and upload an image to the folder""" data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(data["image"]) basename = image_helper.get_basename( image_path ) # in case there is "_1" added at the end after save return {"message": gettext("image_uploaded").format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return { "message": gettext("image_illegal_extension").format(extension) }, 400
def post(self): data = image_schema.load(request.files) user_id = get_jwt_identity() folder = f"user_{user_id}" try: basename = image_helper.save_image(data["image"], folder=folder) return {"message": gettext("image_IMAGE_SAVED").format(basename)} except UploadNotAllowed: extension = image_helper.get_extension(data["image"]) return ( { "message": gettext("image_ILLEGAL_EXTENSION").format(extension) }, 400, )