Ejemplo n.º 1
0
def accept_incoming_image():
    try:
        token = flask.request.form['token']
    except:
        return flask.jsonify({"message": "Auth Required"}), 401
    response = jw.validate_token(token)
    if response[0]:
        if response[1]['decodedToken']['role'] == 'device':
            # set a storage directory for the image. built using the username.
            storage_directory = f"./static/images/{response[1]['decodedToken']['username']}/"
            if os.path.isdir(storage_directory):
                pass
            else:
                os.mkdir(storage_directory)
            # uploaded
            f = flask.request.files['image']
            file_uuid = shortuuid.uuid()
            filename = storage_directory + file_uuid + ".png"
            f.save(filename)
            ilog.log(
                response[1]['decodedToken']['username'], file_uuid + ".png",
                f"40.76.37.214:80/static/images/{response[1]['decodedToken']['username']}/{file_uuid}.png",
                str(datetime.now()))
            return (flask.jsonify({
                "message":
                f"Accepted the Image from user {response[1]['decodedToken']['username']}"
            })), 200
    else:
        return flask.jsonify({"message": "Malformed JWT."}), 403
Ejemplo n.º 2
0
def delete_image():
    """Deletes an image in a path supplied

    Returns:
        JSON: a json object with the response
    """
    # check if the token is supplied with the getRequest:
    token = None
    if 'token' in flask.request.args:
        token = flask.request.args['token']
    else:
        return {
            "message":"Token Not Supplied"
        }, 403

    # check if the image path is supplied with the request
    imgpath = None
    if 'path' in flask.request.args:
        imgpath = flask.request.args['path']
    else:
        return {
            "message":"Path not supplied"
        }, 400
    
    # if we reach here, the image path is supplied
    # now we try to delete the image
    
    if imgpath and token:
        # we have a image path and a token
        response = jw.validate_token(token)
        if response[0]:
            if response[1]['decodedToken']['role'] == 'device':
                # means that the response is valid and has a device username
                username = response[1]['decodedToken']['username']
                deletion_path = f'/static/images/{username}/{imgpath}'
                try:
                    os.remove(deletion_path)
                    return flask.jsonify({
                        "message": "Deleted Image"
                    }), 200
                except:
                    return flask.jsonify({
                        "message": "Could not delete"
                    }), 500
        else:
            return flask.jsonify({
                "message": "Malformed JWT."
            }), 403