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): """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(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, 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 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): """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): """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): """ 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 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(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): 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 post(self): data = request.files["image"] user_id = get_jwt_identity() folder = "user_%s" % (user_id) try: image_path = image_helper.save_image(data, folder, None) basename = image_helper.get_basename(image_path) return {"message": "image uploaded"}, 201 except UploadNotAllowed: return "illegal extension"
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_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) # {'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): """ Used to upload an image file. It uses JWT to retrieve user info and then saves the image to the user's folder. If there is a filename conflict, it appends a number at the end. """ data = image_schema.load(request.files) # request.files is a dictionary in request that has a key of the name of the filename to the data of the file. The data is always going to be a FileStorage object from Werkzeug because Werkzeug is going to take all the incoming files and is going to wrap it with this FileStorage object. So this is going to be {'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': gettext('image_uploaded').format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extensions(data['image']) return {'message': gettext('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 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): """ Used to upload an image ile it uses jwt tp retriewe user informaion and then saves it to the sers folder If the is a filename conflict it appends a number to the end """ data = image_schema.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": 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): """ Uses to upload an image file. It uses JWT to retrieve user information and saves the image to the user's folder. If file is a filename conflict, it appends a number as 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": 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): """ 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}" try: # save(self, storage, folder=None, name=None) image_path = image_helper.save_image(data["image"], folder=folder) # 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": gettext("image_uploaded").format(basename)}, 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): """ Used to upload an image file. It uses JWT to retrieve user info and then saves the image to the user's folder. Whether there is a filename conflict, it appends a number at the end of the filename. """ data = image_schema.load(request.files) # {"image": FileStorage} image = data["image"] user_id = get_jwt_identity() folder = f"user_{user_id}" try: image_path = image_helper.save_image(image, folder) basename = image_helper.get_basename(image_path) return {"message": gettext("image_uploaded").format(basename)}, 201 except UploadNotAllowed: extension = image_helper.get_extension(image) return { "message": gettext("image_extension_not_allowed").format(extension) }, 400
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). """ # request.files is a dict inside request that has key of filename to data(FileStorage obj) # request.files is {"image": FileStorage} # it may raises a `ValidationError` "or" we will know data["image"] is FileStorage data = image_schema.load(request.files) ########################################### print( "request.files", request.files ) # ImmutableMultiDict([('image', <FileStorage: 'Screen Shot 2018-11-26 at 12.30.36 AM.png' ('image/png')>)]) print( "data", data ) # {'image': <FileStorage: 'Screen Shot 2018-11-26 at 12.30.36 AM.png' ('image/png')>} ########################################### user_id = get_jwt_identity() folder = f"user_{user_id}" # static/images/user_1 try: # save(cls, storage, folder=None, name=None) image_path = image_helper.save_image( data["image"], folder=folder) # return whole image path ########################################### print( "image_path", image_path) # user_1/Screen_Shot_2018-11-26_at_12.30.36_AM.png ########################################### # 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": gettext("image_uploaded").format(basename)}, 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): """ Used to upload an image file uses wt to retrive user info saves the image to user folder appemds number in the end of conflict exists :return: """ data = image_schema.load(request.files) #{" image:fileStorage} #loaded to the schema to make sure its a FileStorage Object 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 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_model(): if request.method == "POST": if 'file1' not in request.files: return "Missing file part" file1 = request.files['file1'] name = file1.filename if DesignModel.find_by_designname_and_username(name, session['username']): return "Model already exists with name. Try another name" try: username = session['username'] folder = f"user_{username}" image_path = image_helper.save_image(file1, folder=os.path.join(folder)) 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}" design = DesignModel(name=name, username=session['username'], objname=image_path) try: design.save_to_db() except: traceback.print_exc() return "Error creating model" return redirect(url_for(".index")) return render_template("models/new_model.html")