示例#1
0
 def get(cls, filename: str):
     user_id = get_jwt_identity()
     folder = "user_{}".format(user_id)
     if not image_helper.is_filename_safe(filename):
         return {"message": IMAGE_ILLEGAL_FILENAME}, 400
     try:
         return send_file(image_helper.get_path(filename, folder=folder))
     except FileNotFoundError:
         return {"message": IMAGE_NOT_FOUND}, 404
 def get(cls, filename: str):
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe(filename):
         return {"message": gettext("image_illegal_file_name").format(filename)}, 400
     try:
         return send_file(image_helper.get_path(filename, folder=folder))
     except FileNotFoundError:
         return {"message": gettext("image_not_found").format(filename)}, 404
示例#3
0
 def get(cls, filename: str):
     """Returns the requseted image if it exists. Looks up inside the logged user's folder"""
     user_id = get_jwt_identity()
     folder = f'user_{user_id}'
     if not image_helper.is_filename_safe(filename):
         return {'message': gettext("image_illegal_file_name").format(filename)}, 400
     
     try:
         return send_file(image_helper.get_path(filename, folder=folder))
     except FileNotFoundError:
         return {'message': gettext("image_not_found").format(filename)}, 404
示例#4
0
 def get(self, filename: str):
     """
     Returns the requested image if it exists, looks up in the loggedin users folder
     """
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe(filename):
         return {"message": IMAGE_ILLEGAL_FILE_NAME.format(filename)}, 400
     try:
         return send_file(image_helper.get_path(filename, folder=folder))
     except FileNotFoundError:
         return {"message": IMAGE_NOT_FOUND.format(filename)}, 404
示例#5
0
    def get(cls, filename: str):
        """Returns the requested image if it exists."""
        folder = f"user_{get_jwt_identity()}"
        if not image_helper.is_filename_safe(filename):
            return {
                "message": gettext("image_illegal_file_name").format(filename)
            }, 400

        try:
            return send_file(image_helper.get_path(filename, folder))
        except FileNotFoundError:
            return {"message": gettext("image_not_found")}, 404
示例#6
0
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')
示例#7
0
    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
示例#8
0
    def get(self, filename: str):
        user_id = get_jwt_identity()
        folder = f"user_{user_id}"

        if not image_helper.is_filename_safe(filename):
            return {
                "message": gettext("image_FILENAME_NOT_SAFE").format(filename)
            }, 400

        try:
            return send_file(image_helper.get_path(filename, folder=folder))
        except FileNotFoundError:
            return {"message": gettext("image_FILE_NOT_FOUND")}, 404
示例#9
0
 def delete(cls, filename: str):
     user_id = get_jwt_identity()
     folder = f'user_{user_id}'
     if not image_helper.is_filename_safe(filename):
         return {'message': gettext("image_illegal_file_name").format(filename)}, 400
     
     try:
         os.remove(image_helper.get_path(filename, folder=folder))
         return {'message': gettext('image_deleted').format(filename)}, 200
     except FileNotFoundError:
         return {'message': gettext("image_not_found").format(filename)}, 404
     except:
         return {'message': gettext("image_delete_failed")}, 500
示例#10
0
 def delete(cls, filename: str):
     user_id = get_jwt_identity()
     folder = "user_{}".format(user_id)
     if not image_helper.is_filename_safe(filename):
         return {"message": IMAGE_ILLEGAL_FILENAME}
     try:
         os.remove(image_helper.get_path(filename, folder=folder))
         return {"message": IMAGE_DELETED}, 200
     except FileNotFoundError:
         return {"message": IMAGE_NOT_FOUND}, 404
     except:
         traceback.print_exc()
         return {"message": IMAGE_DELETED_FAILED}, 500
示例#11
0
 def get(cls, filename: str):
     """
     Return requested image for user if exists
     """
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe():
         return {"message": f"Image illegal filename"}, 400
     try:
         return send_file(image_helper.get_path(filename,
                                                folder=folder)), 200
     except FileNotFoundError:
         return {'message': 'Image not found'}, 404
示例#12
0
 def delete(cls, filename):
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe(filename):
         return {"message": gettext("image_illegal_file_name").format(filename)}, 400
     try:
         os.remove(image_helper.get_path(filename, folder=folder))
         return {"message": gettext("image_deleted").format(filename)}, 200
     except FileNotFoundError:
         return {"message": gettext("image_not_found").format(filename)}, 400
     except:
         traceback.print_exc()
         return {"message": gettext("image_delete_failed").format(filename)}, 500
示例#13
0
    def delete(self, filename: str):
        user_id = get_jwt_identity()
        folder = f"user_{user_id}"

        if not image_helper.is_filename_safe(filename):
            return {"message": IMAGE_ILLEGAL_FILE_NAME.format(filename)}, 400

        try:
            os.remove(image_helper.get_path(filename, folder=folder))
            return {"message": IMAGE_DELETED.format(filename)}, 200
        except FileNotFoundError:
            return {"message": IMAGE_NOT_FOUND.format(filename)}, 404
        except:
            return {"message": IMAGE_DELETE_FAILED.format(filename)}, 500
 def get(self, filename: str):
     """
     reeturns the requested image if it exists. look only for user pictures
     """
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe(filename):
         return {
             "message": gettext("image_illegal_file_name").format(filename)
         }, 400
     try:
         return send_file(image_helper.get_path(filename, folder=folder))
     except:
         return {"message": gettext("image_not_found")}
示例#15
0
 def get(cls, filename: str):
     """
     This endpoint returns the requested image if exists. It will use JWT to
     retrieve user information and look for the image inside the user's folder.
     """
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     # check if filename is URL secure
     if not image_helper.is_filename_safe(filename):
         return {"message": gettext("image_illegal_file_name").format(filename)}, 400
     try:
         # try to send the requested file to the user with status code 200
         return send_file(image_helper.get_path(filename, folder=folder))
     except FileNotFoundError:
         return {"message": gettext("image_not_found").format(filename)}, 404
示例#16
0
    def get(cls, filename: str):
        """
            Returns the requested image if exists. Looks up into logged in user folder.
        """

        user_id = get_jwt_identity()
        folder = f"user_{user_id}"
        if not image_helper.is_filename_safe(filename):
            return {
                'message': getext('image_illegal_filename').format(filename)
            }, 400

        try:
            return send_file(image_helper.get_path(filename, folder))
        except FileNotFoundError:
            return {'message': getext('image_not_found').format(filename)}, 404
示例#17
0
 def delete(cls, filename: str):
     """
     Allow an user to delete an image 
     """
     user_id = get_jwt_identity()
     folder = f"user_{user_id}"
     if not image_helper.is_filename_safe():
         return {"message": f"Image illegal filename"}, 400
     try:
         os.remove(image_helper.get_path(filename, folder=folder))
         return {"message": "Image Deleted"}, 200
     except FileNotFoundError:
         return {"message": "Image not found"}, 404
     except:
         traceback.print_exc()
         return {"message": " Failed to delete image"}, 500
示例#18
0
    def delete(cls, filename: str):
        """Delete the image based on the file name."""
        folder = f"user_{get_jwt_identity()}"
        if not image_helper.is_filename_safe(filename):
            return {
                "message": gettext("image_illegal_file_name").format(filename)
            }, 400

        try:
            os.remove(image_helper.get_path(filename, folder))
            return {
                "message": gettext("generic_deleted").format(filename)
            }, 200
        except FileNotFoundError:
            return {"message": gettext("image_not_found")}, 404
        except:
            traceback.print_exc()
            return {"message": gettext("image_delete_failed")}, 500
示例#19
0
    def delete(cls, filename: str):
        """
        This endpoint is used to delete the requested image under the user's folder.
        It uses the JWT to retrieve user information.
        """
        user_id = get_jwt_identity()
        folder = f"user_{user_id}"

        # check if filename is URL secure
        if not image_helper.is_filename_safe(filename):
            return {"message": gettext("image_illegal_file_name").format(filename)}, 400

        try:
            os.remove(image_helper.get_path(filename, folder=folder))
            return {"message": gettext("image_deleted").format(filename)}, 200
        except FileNotFoundError:
            return {"message": gettext("image_not_found").format(filename)}, 404
        except:
            traceback.print_exc()
            return {"message": gettext("image_delete_failed")}, 500
示例#20
0
    def delete(self, filename: str):
        user_id = get_jwt_identity()
        folder = f"user_{user_id}"

        if not image_helper.is_filename_safe(filename):
            return {
                "message": gettext("image_FILENAME_NOT_SAFE").format(filename)
            }, 400

        try:
            image_helper.remove(filename, folder=folder)
            return {
                "message":
                gettext("image_FILE_SUCCESSFULLY_DELETED").format(filename)
            }
        except FileNotFoundError:
            return {"message": gettext("image_FILE_NOT_FOUND")}, 404
        except:
            traceback.print_exc()
            return {"message": gettext("image_DELETE_FAILED")}, 500
 def get(self, label_id: str, image_id: str):
     """
     This endpoint returns the requested i   mage if exists. It will use
     JWT to retrieve user information and look for the image
     inside the label's folder.
     """
     image = ImageModel.find_by_id(image_id)
     filename = image.name
     # folder = label_name
     # check if filename is URL secure
     if not image_helper.is_filename_safe(filename):
         return {"message": IMAGE_ILLEGAL_FILENAME.format(filename)}, 400
     try:
         # try to send the requested file to the user with status code 200
         # abs_path = image_helper.get_path(filename, folder=folder)
         # abs_path_list = abs_path.split("\\")
         # abs_path_list.pop()
         # path = "\\".join(abs_path_list)
         path = "..\static\images"
         return send_from_directory(path, filename)
     except FileNotFoundError:
         return {"message": IMAGE_NOT_FOUND.format(filename)}, 404
    def delete(self, label_id: str, image_id: str):
        """
        This endpoint is used to delete the requested image under the user's
        folder. It uses the JWT to retrieve user information.
        """
        image = ImageModel.find_by_id(image_id)
        filename = image.name

        # check if filename is URL secure
        if not image_helper.is_filename_safe(filename):
            return {"message": IMAGE_ILLEGAL_FILENAME.format(filename)}, 400

        try:
            image_path = os.path.abspath(f'static/images/{filename}')
            image.delete_from_db()
            os.remove(image_path)
            return {"message": IMAGE_DELETED.format(filename)}, 200

        except FileNotFoundError:
            return {"message": IMAGE_NOT_FOUND.format(filename)}, 404
        except Exception:
            traceback.print_exc()
            return {"message": IMAGE_DELETE_FAILED}, 500