Exemple #1
0
 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 Exception:
             traceback.print_exc()
             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,
         )
Exemple #2
0
 def post(cls):
     data = image_schema.load(request.files)  # type(request.files) => dict
     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 {'msg': "Image name '{}' is uploaded".format(basename)}, 201
     except UploadNotAllowed:
         ext = image_helper.get_extension(data["image"])
         return {'msg': "Invalid file format '{}'.".format(ext)}, 400
 def post(self):
     """Used to upload an image.
         Each user will have their own folder. If there is a name conflict
         append 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(image=data['image'], folder=folder)
         basename = image_helper.get_basename(image_path)
         return {'image': f'{request.url_root[:-1]}/static/images/{image_path}', 'message': f'Image {basename} has been uploaded.' }, 201
     except UploadNotAllowed:
         extension = image_helper.get_extension(data['image'])
         return {'message': f'The extension "{extension}" is not allowed.'}, 400
Exemple #4
0
 def post(cls):
     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,
         )
Exemple #5
0
 def put(cls):
     data = image_schema.load(request.files)
     user_id = get_jwt_identity()
     filename = f"user_{user_id}"   # !!! eg: user_{user_id}
     folder = "avatars"
     avatar_path = image_helper.find_image_any_format(filename, folder)
     # Check if the file exists
     if avatar_path:
         try:
             # Delete the old profile avatar so that a new avatar can be saved with same name
             os.remove(avatar_path)
         except:
             return {'msg': "Internal Server Error. Request Failed!"}, 500
     # !!! get the file extention from the requests
     ext = image_helper.get_extension(data["image"].filename)
     try:
         avatar = filename+ext  # user_{id}.ext
         # !!! save the new avatar
         avatar_path = image_helper.save_image(data["image"], folder=folder, name=avatar)
         basename = image_helper.get_basename(avatar_path)
         return {'msg': "Avatar, '{}' Uploaded Successfully!".format(basename)}, 201
     except UploadNotAllowed:
         return {'msg': "Invalid File Format '{}' is not allowed!".format(ext)}, 400