def test_list_dirs(self): for i in range(0, 5): fsutil.create_dir(self.temp_path('a/b/c/d-{}'.format(i))) fsutil.create_file(self.temp_path('a/b/c/f-{}'.format(i)), content='{}'.format(i)) dirpaths = fsutil.list_dirs(self.temp_path('a/b/c')) dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths] self.assertEqual(len(dirpaths), 5) self.assertEqual(dirnames, ['d-0', 'd-1', 'd-2', 'd-3', 'd-4'])
def test_list_dirs(self): for i in range(0, 5): fsutil.create_dir(self.temp_path("a/b/c/d-{}".format(i))) fsutil.create_file( self.temp_path("a/b/c/f-{}".format(i)), content="{}".format(i) ) dirpaths = fsutil.list_dirs(self.temp_path("a/b/c")) dirnames = [fsutil.split_path(dirpath)[-1] for dirpath in dirpaths] self.assertEqual(len(dirpaths), 5) self.assertEqual(dirnames, ["d-0", "d-1", "d-2", "d-3", "d-4"])
def ls(self): if self.__content is not None: return self.__content self.__content = [] if self.is_dir: folders = fsutil.list_dirs(self.absolute) for folder in folders: self.__content.append(Node.parse(absolute=folder)) files = fsutil.list_files(self.absolute) for file in files: self.__content.append(Node.parse(absolute=file)) return self.__content
def list_dir(request): path = request.GET.get('path', '') with_dirs = request.GET.get('dirs', 1) with_files = request.GET.get('files', 0) node = Node.parse(relative=path) result = [] if not node.is_exist or node.is_file: raise Http404 if with_dirs: try: folders = fsutil.list_dirs(node.absolute) except FileNotFoundError: raise Http404 for i, folder in enumerate(folders): relative_path = folder.replace(base_files_dir, '') id_path = '/'.join(fsutil.split_path(relative_path)) folders[i] = { 'id': hashlib.sha1(folder.encode('utf-8')).hexdigest(), 'load': reverse('bloom:file_manager:list_dir', params={'path': id_path}), 'class': 'folder', 'children': len(fsutil.list_dirs(folder)) > 0, 'text': fsutil.get_filename(folder), 'url': static(relative_path), 'list': reverse('bloom:file_manager:list_dir', params={ 'path': id_path, 'files': 1 }), } result.extend(folders) if with_files: try: files = fsutil.list_files(node.absolute) except FileNotFoundError: raise Http404 for i, file in enumerate(files): files[i] = { 'id': fsutil.get_file_hash(file, 'sha1'), 'class': 'file ex-' + fsutil.get_file_extension(file), 'text': fsutil.get_filename(file), 'url': static(file.replace(static_dir, '')), } result.extend(files) if not path and not with_files: result = { 'id': 'root', 'text': 'Root Folder', 'children': result, 'state': { 'opened': True, 'selected': True }, 'list': reverse('bloom:file_manager:list_dir', params={'files': 1}), } return JsonResponse(result, safe=False)