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)
def file_info(): #For getting info about a file data_in = request.get_json(force=True) server_name = cipher.decode_string(data_in.get("server_name")).decode() file_name = cipher.decode_string(data_in.get("file_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") } 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}) file_timestamp = file.get("last_modified") body = {"file_timestamp": file_timestamp} return jsonify(body)
def checkLock(): data_in = request.get_json(force=True) file_name = cipher.decode_string(data_in.get("file_name")).decode() 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") } file_existing = mongo_db.files.find_one({ "file_name": getFileName(file_name), "file_type": getFileExtension(file_name), "server": server_info }) if (file_existing.get("locked") is True): return jsonify({"response_code": 423}) else: return jsonify({"response_code": 200})
def lockFile(): data_in = request.get_json(force=True) file_name = cipher.decode_string(data_in.get("file_name")).decode() file_existing = mongo_db.files.find_one({ "file_name": getFileName(file_name), "file_type": getFileExtension(file_name) }) mongo_db.files.update_one(file_existing, {"$set": {"locked": True}}) return jsonify({"response_code": 200})
def unlockFile(): data_in = request.get_json(force=True) file_name = cipher.decode_string(data_in.get("file_name")).decode() 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") } file_existing = mongo_db.files.find_one({ "file_name": getFileName(file_name), "file_type": getFileExtension(file_name), "server": server_info }) mongo_db.files.update_one(file_existing, {"$set": {"locked": False}}) return jsonify({"response_code": 200})
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})