Skip to content

Commit

Permalink
remove folder tree generation of files
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagocarvalhodev committed Oct 4, 2023
1 parent 2359255 commit daef5c6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 101 deletions.
6 changes: 0 additions & 6 deletions lib/src/io_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
102 changes: 25 additions & 77 deletions lib/src/io_folder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,38 @@ class _FileSystemFolder extends IOFolder {
return ioFile;
}

/// recursively get all entities from this folder filtering by the `IOEntity` `T` type
Future<List<T>> _getAllEntitiesFromType<T extends IOEntity>(
IOFolder ioFolder) async {
final content = await ioFolder.listContent();
final subFolders = content.whereType<IOFolder>();
final entities = <T>[];

for (IOFolder iof in subFolders) {
entities.addAll(await _getAllEntitiesFromType(iof));
}

entities.addAll(content.whereType<T>());

return entities;
}

@override
List<Object?> get props => [name, path];
}

class _WebFolder extends IOFolder {
_WebFolder(
List<MutableIOFilePath> files,
this.name, {
String? path,
}) : path = path ?? name,
_files = files {
_initChildren();
}
final List<MutableIOFilePath> _files;
final Map<String, IOEntity> _children = {};

List<IOFile> files,
) : _files = files;
@override
final DateTime lastModifiedDate = DateTime.now();

final List<IOFile> _files;

@override
Future<List<IOEntity>> listContent() {
return Future.value(_children.values.toList());
return Future.value(_files);
}

@override
Expand All @@ -126,69 +136,17 @@ class _WebFolder extends IOFolder {

@override
Future<List<IOFolder>> listSubfolders() {
return _getAllEntitiesFromType<IOFolder>(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<Object?> get props => [name, path];

void _initChildren() {
final Set<String> 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<MutableIOFilePath> 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<List<T>> _getAllEntitiesFromType<T extends IOEntity>(
IOFolder ioFolder) async {
final content = await ioFolder.listContent();
final subFolders = content.whereType<IOFolder>();
final entities = <T>[];

for (IOFolder iof in subFolders) {
entities.addAll(await _getAllEntitiesFromType(iof));
}

entities.addAll(content.whereType<T>());

return entities;
}

/// Adapts the `IOFolder` from different I/O sources
Expand Down Expand Up @@ -216,19 +174,9 @@ class IOFolderAdapter {
return folder;
}

IOFolder fromIOFiles(List<MutableIOFilePath> files,
{bool useVirtualPath = true}) {
final path = useVirtualPath
? files.first.virtualPath.split('/').first
: files.first.path.split('/').first;
IOFolder fromIOFiles(List<IOFile> 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,
);
}
}
30 changes: 12 additions & 18 deletions lib/src/web/web_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class WebFileSystemProvider implements MultiFileProvider {

@override
Future<IOFolder> getFolder() async {
final files = <MutableIOFilePath>[];
final files = <IOFile>[];

late Stream<List<MutableIOFilePath>> folderStream;
late Stream<List<IOFile>> folderStream;

_folderPicker.pickFolderFiles((stream) => folderStream = stream);

Expand Down Expand Up @@ -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<void> pickFolderFiles(
Function(Stream<List<MutableIOFilePath>> stream) getFiles) async {
StreamController<List<MutableIOFilePath>> _folderController =
StreamController<List<MutableIOFilePath>>();
Function(Stream<List<IOFile>> stream) getFiles) async {
StreamController<List<IOFile>> _folderController =
StreamController<List<IOFile>>();

/// Set the stream to get the files
getFiles(_folderController.stream);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

0 comments on commit daef5c6

Please sign in to comment.