Implement folder move restrictions in file explorer and backend; prevent moving a folder into itself or its own subfolder.
This commit is contained in:
@@ -1456,11 +1456,39 @@ class _FileExplorerState extends State<FileExplorer> {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
onAcceptWithDetails: (draggedFile) {
|
onAcceptWithDetails: (draggedFile) {
|
||||||
|
final sourcePath = draggedFile.data.path;
|
||||||
|
final targetPath = file.path;
|
||||||
|
|
||||||
|
// Prevent dragging a folder into itself
|
||||||
|
if (sourcePath == targetPath) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text(
|
||||||
|
'Cannot move a folder into itself',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent dragging a folder into its own subfolder
|
||||||
|
if (targetPath.startsWith('$sourcePath/') ||
|
||||||
|
targetPath.startsWith('$sourcePath\\')) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text(
|
||||||
|
'Cannot move a folder into its own subfolder',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
context.read<FileBrowserBloc>().add(
|
context.read<FileBrowserBloc>().add(
|
||||||
MoveFile(
|
MoveFile(
|
||||||
orgId: widget.orgId,
|
orgId: widget.orgId,
|
||||||
sourcePath: draggedFile.data.path,
|
sourcePath: sourcePath,
|
||||||
targetPath: file.path,
|
targetPath: targetPath,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1464,6 +1464,20 @@ func moveOrgFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevent moving a folder into itself or its own subfolders
|
||||||
|
if sourceFile.Type == "folder" {
|
||||||
|
// Check if trying to move into itself
|
||||||
|
if req.SourcePath == req.TargetPath {
|
||||||
|
errors.WriteError(w, errors.CodeInvalidArgument, "Cannot move a folder into itself", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Check if target is a subfolder of source
|
||||||
|
if strings.HasPrefix(req.TargetPath, req.SourcePath+"/") {
|
||||||
|
errors.WriteError(w, errors.CodeInvalidArgument, "Cannot move a folder into its own subfolder", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Determine new file name - check if target is a folder
|
// Determine new file name - check if target is a folder
|
||||||
var newPath string
|
var newPath string
|
||||||
var targetFile *database.File
|
var targetFile *database.File
|
||||||
@@ -1670,6 +1684,20 @@ func moveUserFileHandler(w http.ResponseWriter, r *http.Request, db *database.DB
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevent moving a folder into itself or its own subfolders
|
||||||
|
if sourceFile.Type == "folder" {
|
||||||
|
// Check if trying to move into itself
|
||||||
|
if req.SourcePath == req.TargetPath {
|
||||||
|
errors.WriteError(w, errors.CodeInvalidArgument, "Cannot move a folder into itself", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Check if target is a subfolder of source
|
||||||
|
if strings.HasPrefix(req.TargetPath, req.SourcePath+"/") {
|
||||||
|
errors.WriteError(w, errors.CodeInvalidArgument, "Cannot move a folder into its own subfolder", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Determine new file name - check if target is a folder
|
// Determine new file name - check if target is a folder
|
||||||
var newPath string
|
var newPath string
|
||||||
var targetFile *database.File
|
var targetFile *database.File
|
||||||
|
|||||||
Reference in New Issue
Block a user