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): """ 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): 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 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 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, 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(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): """ 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 post(cls): if "model_id" in request.form: id = request.form.get("model_id") else: return {"message": gettext("getfile_file_notfound")}, 404 print('model_id=' + id) design = DesignModel.find_by_id(id) if not design: return {"message": gettext("getfile_design_notfound")}, 404 if design: try: username = UserModel.find_by_id(get_jwt_identity()).username file1 = request.files['resultfile1'] image_path = image_helper.save_image( file1, folder=os.path.join(FOLDER_NAME)) result = ResultModel(name=file1.filename, model_id=design.id, objname=image_path, saved_by=username) try: result.save_to_db() except: traceback.print_exc() return {"message": gettext("saveresult_failed")}, 500 return {"message": gettext("saveresult_stored")}, 200 except: traceback.print_exc() return {"message": gettext("getfile_file_notfound")}, 404 return {"message": gettext("getfile_file_notfound")}, 404 return {"message": gettext("getfile_design_notfound")}, 404
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(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): """ 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): """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 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) 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 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): """ 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 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): """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, )
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