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: () {
|
onPressed: () {
|
||||||
final folderName = controller.text.trim();
|
final folderName = controller.text.trim();
|
||||||
if (folderName.isNotEmpty) {
|
if (folderName.isNotEmpty) {
|
||||||
final nameWithSlash = folderName.startsWith('/')
|
// Strip any leading/trailing slashes so we only send the bare folder name
|
||||||
? folderName
|
final sanitized = folderName
|
||||||
: '/$folderName';
|
.replaceAll(RegExp(r'^/+'), '')
|
||||||
Navigator.of(context).pop(nameWithSlash);
|
.replaceAll(RegExp(r'/+$'), '');
|
||||||
|
if (sanitized.isNotEmpty) {
|
||||||
|
Navigator.of(context).pop(sanitized);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: const Text(
|
child: const Text(
|
||||||
|
|||||||
@@ -119,20 +119,28 @@ class FileService {
|
|||||||
String parentPath,
|
String parentPath,
|
||||||
String folderName,
|
String folderName,
|
||||||
) async {
|
) 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
|
// Construct proper path: /parent/folder or /folder for root
|
||||||
String path;
|
String path;
|
||||||
if (parentPath == '/') {
|
if (parentPath == '/') {
|
||||||
path = '/$folderName';
|
path = '/$normalizedName';
|
||||||
} else {
|
} else {
|
||||||
// Ensure parentPath doesn't end with / before appending
|
// Ensure parentPath doesn't end with / before appending
|
||||||
final cleanParent = parentPath.endsWith('/')
|
final cleanParent = parentPath.endsWith('/')
|
||||||
? parentPath.substring(0, parentPath.length - 1)
|
? parentPath.substring(0, parentPath.length - 1)
|
||||||
: parentPath;
|
: parentPath;
|
||||||
path = '$cleanParent/$folderName';
|
path = '$cleanParent/$normalizedName';
|
||||||
}
|
}
|
||||||
|
|
||||||
final data = {
|
final data = {
|
||||||
'name': folderName,
|
'name': normalizedName,
|
||||||
'path': path,
|
'path': path,
|
||||||
'type': 'folder',
|
'type': 'folder',
|
||||||
'size': 0,
|
'size': 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user