|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:collection/collection.dart'; |
| 4 | +import 'package:deepfacelab_client/class/folder_property.dart'; |
| 5 | +import 'package:deepfacelab_client/service/workspace_service.dart'; |
| 6 | + |
| 7 | +import '../class/workspace.dart'; |
| 8 | + |
| 9 | +class FileManagerService { |
| 10 | + Future<FolderProperty> _updateFolderProperty( |
| 11 | + {required FolderProperty folderProperty, |
| 12 | + required List<FileSystemEntity> fileSystemEntities}) async { |
| 13 | + folderProperty.nbChildren = fileSystemEntities.length; |
| 14 | + var size = 0; |
| 15 | + List<String> foldersFound = []; |
| 16 | + for (var fileSystemEntity in fileSystemEntities) { |
| 17 | + if (fileSystemEntity is File) { |
| 18 | + size += await fileSystemEntity.length(); |
| 19 | + } |
| 20 | + if (fileSystemEntity is Directory) { |
| 21 | + var thisFileSystemEntities = |
| 22 | + await Directory(fileSystemEntity.path).list().toList(); |
| 23 | + foldersFound.add(fileSystemEntity.path); |
| 24 | + var thisFolderProperty = folderProperty.folderProperties |
| 25 | + .firstWhereOrNull( |
| 26 | + (FolderProperty f) => f.path == fileSystemEntity.path); |
| 27 | + if (thisFolderProperty == null) { |
| 28 | + thisFolderProperty = |
| 29 | + FolderProperty(path: fileSystemEntity.path, folderProperties: []); |
| 30 | + folderProperty.folderProperties.add(thisFolderProperty); |
| 31 | + } |
| 32 | + await _updateFolderProperty( |
| 33 | + folderProperty: thisFolderProperty, |
| 34 | + fileSystemEntities: thisFileSystemEntities); |
| 35 | + size += thisFolderProperty.size!; |
| 36 | + } |
| 37 | + } |
| 38 | + folderProperty.folderProperties = folderProperty.folderProperties |
| 39 | + .where((f) => foldersFound.contains(f.path)) |
| 40 | + .toList(); |
| 41 | + folderProperty.size = size; |
| 42 | + return folderProperty; |
| 43 | + } |
| 44 | + |
| 45 | + Future<FolderProperty> updateFolderProperty( |
| 46 | + {required String path, |
| 47 | + required Workspace workspace, |
| 48 | + List<FileSystemEntity>? fileSystemEntities, |
| 49 | + bool force = false}) async { |
| 50 | + // region get thisFolderProperty |
| 51 | + workspace.folderProperty ??= |
| 52 | + FolderProperty(path: workspace.path, folderProperties: []); |
| 53 | + FolderProperty? thisFolderProperty = workspace.folderProperty; |
| 54 | + var pathArray = path |
| 55 | + .replaceAll(workspace.path, '') |
| 56 | + .split(Platform.pathSeparator) |
| 57 | + .where((element) => element != '') |
| 58 | + .toList(); |
| 59 | + var i = 0; |
| 60 | + while (thisFolderProperty != null && thisFolderProperty.path != path) { |
| 61 | + thisFolderProperty = thisFolderProperty.folderProperties.firstWhereOrNull( |
| 62 | + (f) => f.path.endsWith(Platform.pathSeparator + pathArray[i])); |
| 63 | + i++; |
| 64 | + } |
| 65 | + // endregion |
| 66 | + if (thisFolderProperty == null) { |
| 67 | + return await updateFolderProperty( |
| 68 | + path: workspace.path, workspace: workspace, force: true); |
| 69 | + } |
| 70 | + fileSystemEntities ??= await Directory(path).list().toList(); |
| 71 | + if (!force && thisFolderProperty.nbChildren == fileSystemEntities.length) { |
| 72 | + return thisFolderProperty; |
| 73 | + } |
| 74 | + if (thisFolderProperty.path != workspace.path) { |
| 75 | + return await updateFolderProperty( |
| 76 | + path: workspace.path, workspace: workspace, force: true); |
| 77 | + } |
| 78 | + thisFolderProperty = await _updateFolderProperty( |
| 79 | + folderProperty: thisFolderProperty, |
| 80 | + fileSystemEntities: fileSystemEntities); |
| 81 | + workspace.folderProperty = thisFolderProperty; |
| 82 | + WorkspaceService().createUpdateWorkspace( |
| 83 | + oldWorkspace: workspace, newWorkspace: workspace); |
| 84 | + return thisFolderProperty; |
| 85 | + } |
| 86 | +} |
0 commit comments