def store_file(self, file_id, file_size): socket = self.request send_confirmation_message_to_upload_request(file_id, file_size, socket) file_contents = download_file(file_id, file_size, socket) logging.info("Storing file: " + file_id) abs_file_path = os.path.join(abs_directory_path, file_id) f = open(abs_file_path, 'w+') f.write(file_contents) bytes_size = os.path.getsize(abs_file_path) local_file = LocalFile(f, file_id, abs_file_path, bytes_size) local_file.close() logging.info("Storing file finished: " + file_id)
def open(file_id, abs_directory_path, directory_socket): """ Read file from file server :param abs_directory_path: Path to where file will be stored :param file_id: Unique string for file :param directory_socket: Socket connection with file server :return: a handle to the file """ file_server_message = establish_file_directory(file_id, directory_socket) host = file_server_message.host port = file_server_message.port file_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) file_socket.connect((host, port)) send_file_download_message(file_id, file_socket) response = read_upload_response_message(file_id, file_socket) file_name = response.file_name assert file_id == file_name file_size = response.file_size send_confirmation_message_to_upload_request(file_name, file_size, file_socket) file_contents = download_file(file_name, file_size, file_socket) f = string_to_file(file_name, abs_directory_path, file_contents) return DistributedFile(f, file_id, f.name, file_size, file_socket)