Normalize folder names to prevent leading/trailing slashes in folder creation
This commit is contained in:
@@ -130,10 +130,13 @@ class _FileExplorerState extends State<FileExplorer> {
|
||||
onPressed: () {
|
||||
final folderName = controller.text.trim();
|
||||
if (folderName.isNotEmpty) {
|
||||
final nameWithSlash = folderName.startsWith('/')
|
||||
? folderName
|
||||
: '/$folderName';
|
||||
Navigator.of(context).pop(nameWithSlash);
|
||||
// Strip any leading/trailing slashes so we only send the bare folder name
|
||||
final sanitized = folderName
|
||||
.replaceAll(RegExp(r'^/+'), '')
|
||||
.replaceAll(RegExp(r'/+$'), '');
|
||||
if (sanitized.isNotEmpty) {
|
||||
Navigator.of(context).pop(sanitized);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: const Text(
|
||||
|
||||
@@ -119,20 +119,28 @@ class FileService {
|
||||
String parentPath,
|
||||
String folderName,
|
||||
) async {
|
||||
// Normalize folder name to avoid accidental leading slashes creating double-slash paths
|
||||
final normalizedName = folderName
|
||||
.replaceAll(RegExp(r'^/+'), '')
|
||||
.replaceAll(RegExp(r'/+$'), '');
|
||||
if (normalizedName.isEmpty) {
|
||||
throw Exception('Folder name cannot be empty');
|
||||
}
|
||||
|
||||
// Construct proper path: /parent/folder or /folder for root
|
||||
String path;
|
||||
if (parentPath == '/') {
|
||||
path = '/$folderName';
|
||||
path = '/$normalizedName';
|
||||
} else {
|
||||
// Ensure parentPath doesn't end with / before appending
|
||||
final cleanParent = parentPath.endsWith('/')
|
||||
? parentPath.substring(0, parentPath.length - 1)
|
||||
: parentPath;
|
||||
path = '$cleanParent/$folderName';
|
||||
path = '$cleanParent/$normalizedName';
|
||||
}
|
||||
|
||||
final data = {
|
||||
'name': folderName,
|
||||
'name': normalizedName,
|
||||
'path': path,
|
||||
'type': 'folder',
|
||||
'size': 0,
|
||||
|
||||
Reference in New Issue
Block a user