Пример #1
0
def cacheCheck(file_name, server_name):
    #Check file timestamp on dfs
    body = {
        "file_name": cipher.encode_string(file_name).decode(),
        "server_name": cipher.encode_string(server_name).decode()
    }
    response = requests.post(full_serv_addr + "/file/info",
                             data=json.dumps(body),
                             headers=headers)

    time_stamp = response.json().get("file_timestamp")

    #time stamp of file on client?
    user = mongo_db.users.find_one({"id": userId})
    cached_file = user.get(getFormattedFileName(file_name))
    if cached_file is None:
        #download
        return False

    cached_time_stamp = cached_file.get("last_modified")

    print(float(time_stamp))
    print(float(cached_time_stamp))
    if not math.isclose(float(time_stamp), float(cached_time_stamp),
                        abs_tol=1):
        #download newer
        return False
    else:
        #use cached version
        print("accessing cached version")
        return True
Пример #2
0
def addToCache(file, server_name):
    user = mongo_db.users.find_one({"id": userId})
    #Update to mention what server it's from
    file.update({"server_source": server_name})
    #Add file to user
    mongo_db.users.update_one(
        user, {"$set": {
            getFormattedFileName(file.get("file_name")): file
        }})
Пример #3
0
def getFromCache(file_name, server_name):
    print("getting!")

    user = mongo_db.users.find_one({"id": userId})
    cached_file = user.get(getFormattedFileName(file_name))

    new_file_name = input("Save as: ")
    file_contents = cached_file.get("file_contents")

    file = open(new_file_name, "wb")
    file.write(file_contents)
    file.close()
Пример #4
0
def download():
    data_in = request.get_json(force=True)
    print("hitting download")

    server_name = cipher.decode_string(data_in.get("server_name")).decode()
    server = mongo_db.servers.find_one({"server_name": server_name})
    server_info = {
        "server_id": server.get("id"),
        "server_name": server.get("server_name"),
        "server_address": server.get("port")
    }
    print(server_info)

    file_name = cipher.decode_string(data_in.get("file_name")).decode()

    file = mongo_db.files.find_one({
        "file_name": getFileName(file_name),
        "file_type": getFileExtension(file_name),
        "server": server_info
    })
    if file is None:
        jsonify({"response_code": 404})

    #Find where file is
    file_requested = mongo_db.files.find_one({
        "file_name":
        getFileName(file_name),
        "file_type":
        getFileExtension(file_name),
        "server":
        server_info
    })
    if file_requested.get("locked") is True:
        return jsonify({"response_code": 423})

    #Grab contents
    print(server)
    file_out = server.get(getFormattedFileName(file_name))

    file_contents = cipher.encode_string(
        file_out.get("file_contents").decode()).decode()
    print(file_contents)

    file_timestamp = cipher.encode_string(
        str(file_requested.get("last_modified"))).decode()

    response = {
        "file_name": cipher.encode_string(file_name).decode(),
        "file_contents": file_contents,
        "last_modified": file_timestamp
    }
    return jsonify(response)
Пример #5
0
def addToQueue():
    data_in = request.get_json(force=True)

    #File requested
    file_name = cipher.decode_string(data_in.get("file_name")).decode()
    #and where it is

    #Requester details
    user_addr = cipher.decode_string(data_in.get("user_addr")).decode()

    data = {"file_name": file_name, user_addr: user_addr}

    #File exist in table?
    file_existing = mongo_db.locks.find_one(
        {"file_name": getFormattedFileName(file_name)})
    if file_existing is None:
        mongo_db.locks.insert(data)
    else:
        mongo_db.locks.update_one(file_existing, {"$set": data})
Пример #6
0
def upload():
    data_in = request.get_json(force=True)
    print("hitting upload")

    temp = data_in.get("file_name")
    file_name = cipher.decode_string(temp.encode()).decode()
    file_contents = cipher.decode_string(
        data_in.get("file_contents").encode().decode())

    #Find a free server
    server = mongo_db.servers.find_one({
        "server_name":
        cipher.decode_string(data_in.get("server_name")).decode()
    })
    server_info = {
        "server_id": server.get("id"),
        "server_name": server.get("server_name"),
        "server_address": server.get("port")
    }

    #Store
    data = {
        "file_name": getFileName(file_name),
        "file_type": getFileExtension(file_name),
        "server": server_info,
        "locked": False,
        "last_modified": time.time()
    }

    #does it already exist on this server?
    file_existing = mongo_db.files.find_one({
        "file_name":
        getFileName(file_name),
        "file_type":
        getFileExtension(file_name),
        "server":
        server_info
    })

    if file_existing is not None:
        #Exists? Is it locked (ie: don't overwrite)
        if file_existing.get("locked") is True:
            print("ba;;s")
            return jsonify({"response_code": 423})
        mongo_db.files.update_one(
            {
                "file_name": getFileName(file_name),
                "file_type": getFileExtension(file_name),
                "server": server_info
            }, {"$set": data})
    else:
        mongo_db.files.insert(
            data)  #into files db (ie: where the file contents are)

    server_data = {"file_name": file_name, "file_contents": file_contents}
    mongo_db.servers.update_one(
        server, {"$set": {
            getFormattedFileName(file_name): server_data
        }})

    return jsonify({"response_code": 200})