def register_user(self, msg_spl): hashk = hash_.get_hash_from_string(self.addr[0] + msg_spl[1]) folder = os.path.join(hashk, msg_spl[2]) file = os.path.join(folder, "{}.json".format(hashk)) controller.add_user(self.client_list_path, self.filesystem_list_path, msg_spl[1], self.addr[0], hashk) res = controller.add_filesystem(self.filesystem_list_path, msg_spl[2], hashk, folder) create.folder(hashk) create.folder(folder) create.json_file(file) self.root_dir = os.path.join(self.filesystem_path, folder) self.fs_content_file = os.path.join(self.filesystem_path, file) self.hash = hashk return "{} {}".format(res, hashk)
def make_folder(self, msg_spl): curr = msg_spl[1] dir = msg_spl[2] if curr.__eq__("root"): loc = self.root_dir else: loc = self.root_dir + os.sep + curr if os.sep in dir: dirs = dir.split(os.sep) for i in range(len(dirs) - 1): loc += os.sep + dirs[i] dir = dirs[-1] timestamp = math.trunc(datetime.timestamp(datetime.now())) hashname = hash_.get_hash_from_string(str(timestamp) + dir) loc = os.path.join(loc, hashname) os.mkdir(loc) self.add_to_dict(hashname, dir, "directory", timestamp=timestamp) return "CDIR {} {}".format(hashname, timestamp)
def get_file(self, msg_spl): flag = False src = msg_spl[1] dst = msg_spl[2] print(color.purple("ADD {} {}".format(src, dst))) dir = self._get_dir(dst) filename = self._get_filename(src) extension = os.path.splitext(filename)[1] path = os.path.join(self.hash, dir) filepath = create.file_path_in_filesystem(os.path.join(path, filename)) print(color.green(filename)) with open(filepath, "wb") as file: while True: bytes = self.conn.recv(BUFFER_SIZE) file.write(bytes) if bytes.strip()[-3:] == b'EOF': break elif bytes.strip()[-3:] == b'INT': flag = True break file.close() if flag: os.remove(filepath) return "NCADD" timestamp = math.trunc(datetime.timestamp(datetime.now())) hashname = hash_.get_hash_from_string(filepath + str(timestamp)) new_filepath = filepath.replace(filename, "{}{}".format(hashname, extension)) timestamp = self.add_to_dict(hashname, filename, "file", extension=extension, timestamp=timestamp) os.rename(filepath, new_filepath) try: os.remove(filepath) except: None return "CADD {} {}".format(hashname, timestamp)