def show_registered_servers(self): servers = self.unionpath.edit_mount_list("get", property="servers") conn_IP = self.unionpath.client.IP for server in servers: if conn_IP == server: print( color.cyan(" (*) " + server + " -> " + servers.get(server))) else: print( color.cyan(" " + server + " -> " + servers.get(server)))
def add_file_to_filesystem(self, source, destination=None): if ".uniondir" in source: print(color.red("Cannot add object from within the filesystem.")) return if not destination: destination = self.unionpath.current_folder else: curr = self.unionpath.current_folder dirs = destination.split(os.sep) for dir in dirs: dir_hash = self.unionpath.translate_to_hash(dir, curr) if not dir_hash: print("{} does not exist".format(destination)) return curr = os.path.join(curr, dir_hash) destination = curr filename, extension = self.unionpath.get_filename(source) hashname = self.unionpath.create_hash(source) path = shutil.copy2(source, destination) os.rename(path, path.replace(filename + extension, hashname)) fs_loc = self.unionpath.hashpath_to_fspath(destination) print(color.cyan("{} {}".format(filename, extension))) self.unionpath.add_to_dictionary(hashname, filename, "file", destination, fs_loc, timestamp=None, extension=extension, mount=self.unionpath.current_mount) return "add {}".format(hashname)
def get_mount_from_client(self, dirhash, name, empty): contents = [] dirpath = os.path.join(self.unionpath.filesystem_root_dir, dirhash) os.mkdir(dirpath) if empty == "True": self.client.send("DONE") else: self.client.send("GIVE") while (True): message = self.client.get() print(color.cyan(message)) if message == "DONE": break contents.append(message.split()[1]) self.filehandler.get_file(message, dir=dirhash) mount = { dirhash: { "name": name, "owner": self.client.hash, "content": contents } } self.unionpath.edit_mountlist(op="add", mount=mount)
def help(): ''' USE: help.help() Description: returns the '--help' text Notes: if you add extra operators, make sure to add them here ad well as in help.spfc_opr() and the error.get() dictionary and the help.dct() dictionary. ''' css_1 = '\n\n ' css_2 = '\n ' commands = [ 'reg', 'con', 'cd', 'open', 'ls', 'srv', 'mk', 'add', 'rm', 'mt', 'mv', 'cp', 'rn', 'f', '--help', 'quit', 'clear', 'exp' ] help = color.green('this is the --help section:') for cmd in commands: help += css_1 + '» ' + color.bold(str( spfc_opr(cmd, True))) + color.cyan(css_2 + 'aliases: ' + str( spfc_opr(cmd))) + css_2 + '' + color.yellow( dct(cmd)[0]) + css_2 + '' + color.grey(dct(cmd)[1]) return help
def list_folder_content(self, paths, arg=None, additional=False): dir_icon = "📁" file_icon = "📄" mount_icon = "💾" img_icon = "🖼" music_icon = "🎵" movie_icon = "🎥" name, time, type, location, hash, extension, fs_path, mount = 0, 1, 2, 3, 4, 5, 6, 7 f_list = [] d_list = [] mounts = self.unionpath.edit_mount_list(op='get', property='mounts') for p in paths: if p[type] == 'file': f_list.append(p) elif p[type] == 'directory': d_list.append(p) paths = f_list + d_list if arg: print('\n-------------------') if not additional: for p in paths: if p[type] == 'file': icon = file_icon if p[extension] in [ ".png", ".jpg", ".jpeg", ".bmp", ".gif", ".eps" ]: icon = img_icon elif p[extension] in [".mp3", ".wav", ".flac", ".aac"]: icon = music_icon elif p[extension] in [".mp4", ".mov", ".avi", ".wmv"]: icon = movie_icon if extension != "": print("{} ".format(icon) + color.purple(p[name] + p[extension])) else: print("{} ".format(icon) + color.purple(p[name])) elif p[type] == 'directory': icon = dir_icon if p[hash] in mounts: icon = mount_icon print("{} ".format(icon) + color.blue(p[name])) else: for p in paths: date = datetime.datetime.fromtimestamp(int( p[time])).strftime('%m/%d/%Y %H:%M:%S') if p[type] == 'file': if p[type] == 'file': icon = file_icon if p[extension] in [ ".png", ".jpg", ".jpeg", ".bmp", ".gif", ".eps" ]: icon = img_icon elif p[extension] in [".mp3", ".wav", ".flac", ".aac"]: icon = music_icon elif p[extension] in [".mp4", ".mov", ".avi", ".wmv"]: icon = movie_icon if not str(p[extension]): add_info = color.cyan( " \tDate: {} \tFingerprint: {}".format( date, p[time])) else: add_info = color.cyan( " \tType: {} \tDate: {} \tFingerprint: {}". format(p[extension], date, p[time])) if extension != "": print("{} ".format(icon) + color.purple(p[name] + p[extension]) + add_info) else: print("{} ".format(icon) + color.purple(p[name]) + add_info) elif p[type] == 'directory': icon = dir_icon if p[hash] in mounts: icon = mount_icon add_info = color.cyan( " \tDate: {} \tFingerprint: {}".format(date, p[time])) print("{} ".format(icon) + color.blue(p[name]) + add_info) if arg: print('\n-------------------')
def welcome(): msg = color.cyan("############################################################################\n") msg += color.cyan("# ~ {}{}\n").format( color.purple("Redecentralised Filesystem"), color.cyan(" ~ #")) msg += color.cyan("# #\n") msg += color.cyan("# - Establish connection with to file server with {} {}\n").format( color.yellow("register <IP> <Name>"), color.cyan("#")) msg += color.cyan("# - Enter {} {}\n").format(color.yellow("name"), color.cyan( "of known server #")) msg += color.cyan("# - List names of all known servers with {} {}\n").format( color.yellow("serverlist"), color.cyan("#")) msg += color.cyan("# - Exit program with {} {}\n").format( color.yellow("quit"), color.cyan("#")) msg += color.cyan("############################################################################\n") return msg
def get(self): msg = self.socket.recv(2048).decode('utf-8') print(color.cyan("<- {}".format(msg))) return msg