Normalize folder names to prevent leading/trailing slashes in folder creation

This commit is contained in:
Leon Bösche
2026-01-10 03:40:46 +01:00
parent 687c7a5a61
commit f3656fdbd0
2 changed files with 18 additions and 7 deletions

View File

@@ -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(

View File

@@ -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,