From daef5c69edc5f3ee91663b86a3270d83db34012a Mon Sep 17 00:00:00 2001 From: Thiago Carvalho Date: Wed, 4 Oct 2023 09:09:33 -0300 Subject: [PATCH] remove folder tree generation of files --- lib/src/io_file.dart | 6 --- lib/src/io_folder.dart | 102 ++++++++++------------------------------ lib/src/web/web_io.dart | 30 +++++------- 3 files changed, 37 insertions(+), 101 deletions(-) diff --git a/lib/src/io_file.dart b/lib/src/io_file.dart index a94dbae..b9bc28d 100644 --- a/lib/src/io_file.dart +++ b/lib/src/io_file.dart @@ -39,12 +39,6 @@ abstract class IOFile implements IOEntity { ); } -/// A mutable version of `IOFile` that can be used to change the file's path. -/// The `mutablePath` is not the same as `path`, which the latter is given by the system. -abstract class MutableIOFilePath implements IOFile { - abstract String virtualPath; -} - /// Adapts the `IOFile` from different I/O sources. /// /// Those are: diff --git a/lib/src/io_folder.dart b/lib/src/io_folder.dart index d133708..4eb769a 100644 --- a/lib/src/io_folder.dart +++ b/lib/src/io_folder.dart @@ -95,28 +95,38 @@ class _FileSystemFolder extends IOFolder { return ioFile; } + /// recursively get all entities from this folder filtering by the `IOEntity` `T` type + Future> _getAllEntitiesFromType( + IOFolder ioFolder) async { + final content = await ioFolder.listContent(); + final subFolders = content.whereType(); + final entities = []; + + for (IOFolder iof in subFolders) { + entities.addAll(await _getAllEntitiesFromType(iof)); + } + + entities.addAll(content.whereType()); + + return entities; + } + @override List get props => [name, path]; } class _WebFolder extends IOFolder { _WebFolder( - List files, - this.name, { - String? path, - }) : path = path ?? name, - _files = files { - _initChildren(); - } - final List _files; - final Map _children = {}; - + List files, + ) : _files = files; @override final DateTime lastModifiedDate = DateTime.now(); + final List _files; + @override Future> listContent() { - return Future.value(_children.values.toList()); + return Future.value(_files); } @override @@ -126,69 +136,17 @@ class _WebFolder extends IOFolder { @override Future> listSubfolders() { - return _getAllEntitiesFromType(this); + throw UnimplementedError('IOFolder doesnt support list subfolders on Web'); } @override - final String name; + final String name = ''; @override - final String path; + final String path = ''; @override List get props => [name, path]; - - void _initChildren() { - final Set processedFolders = {}; - - // Add immediate files first - for (var file in _files) { - final segments = file.virtualPath.split('/'); - if (segments.length == 1) { - _children[segments.first] = file; - } - } - - // Add immediate folders next - for (var file in _files) { - final segments = file.virtualPath.split('/'); - if (segments.length > 1) { - final immediateFolderName = segments.first; - if (!processedFolders.contains(immediateFolderName)) { - processedFolders.add(immediateFolderName); - List immediateChildFiles = _files - .where( - (f) => f.virtualPath.split('/').first == immediateFolderName) - .map((f) { - f.virtualPath = - f.virtualPath.substring("$immediateFolderName/".length); - return f; - }).toList(); - _children[immediateFolderName] = _WebFolder( - immediateChildFiles, - immediateFolderName, - path: file.path.split(file.name).first, - ); - } - } - } - } -} - -/// recursively get all entities from this folder filtering by the `IOEntity` `T` type -Future> _getAllEntitiesFromType( - IOFolder ioFolder) async { - final content = await ioFolder.listContent(); - final subFolders = content.whereType(); - final entities = []; - - for (IOFolder iof in subFolders) { - entities.addAll(await _getAllEntitiesFromType(iof)); - } - - entities.addAll(content.whereType()); - - return entities; } /// Adapts the `IOFolder` from different I/O sources @@ -216,19 +174,9 @@ class IOFolderAdapter { return folder; } - IOFolder fromIOFiles(List files, - {bool useVirtualPath = true}) { - final path = useVirtualPath - ? files.first.virtualPath.split('/').first - : files.first.path.split('/').first; + IOFolder fromIOFiles(List files) { return _WebFolder( files, - path.split('/').first, - // it uses the very first file path as the root path as all files inside the - // folder should be in the same path. - // Even if the first file is in a subfolder it will return the correct behavior. - // e.g.: folder/subfolder1/file2.txt -> returns /folder - path: path, ); } } diff --git a/lib/src/web/web_io.dart b/lib/src/web/web_io.dart index 027899a..452a8bc 100644 --- a/lib/src/web/web_io.dart +++ b/lib/src/web/web_io.dart @@ -77,9 +77,9 @@ class WebFileSystemProvider implements MultiFileProvider { @override Future getFolder() async { - final files = []; + final files = []; - late Stream> folderStream; + late Stream> folderStream; _folderPicker.pickFolderFiles((stream) => folderStream = stream); @@ -144,9 +144,9 @@ class WebFileSystemProvider implements MultiFileProvider { /// When the file picker window is closed it will return a list of `IOFile` class FolderPicker { Future pickFolderFiles( - Function(Stream> stream) getFiles) async { - StreamController> _folderController = - StreamController>(); + Function(Stream> stream) getFiles) async { + StreamController> _folderController = + StreamController>(); /// Set the stream to get the files getFiles(_folderController.stream); @@ -191,25 +191,22 @@ class FolderPicker { lastModifiedDate = DateTime.now(); } - return WebFile( - e, - name: e.name, - lastModifiedDate: lastModifiedDate, - path: path, - contentType: lookupMimeTypeWithDefaultType(path), - ); + return WebFile(e, + name: e.name, + lastModifiedDate: lastModifiedDate, + path: path, + contentType: lookupMimeTypeWithDefaultType(path)); } } -class WebFile implements MutableIOFilePath { +class WebFile implements IOFile { WebFile( File file, { required this.name, required this.lastModifiedDate, required this.path, required this.contentType, - }) : virtualPath = path, - _file = file; + }) : _file = file; final File _file; @@ -278,7 +275,4 @@ class WebFile implements MutableIOFilePath { String toString() { return 'file name: $name\nfile path: $path\nlast modified date: ${lastModifiedDate.toIso8601String()}\nlength: $length'; } - - @override - String virtualPath; }