Przeglądaj źródła

Fix File Manager when current folder is deleted

When a folder was removed by another window or client, the directory hash endpoint did not report an error, so the File Manager stayed on a missing path.

Backend: system_fs_handleDirHash now returns "Directory not found" if the path no longer exists or is not a directory.

Frontend: getDirHash handles this error by walking up to the nearest existing parent directory and listing it, using the new returnToExistingParentDir and getParentDir helpers. It falls back to user:/ and reloads the side bar roots if the whole root is gone. This replaces the previous page reload.
Toby Chui 3 tygodni temu
rodzic
commit
e2390ba331

+ 6 - 0
src/file_system.go

@@ -3329,6 +3329,12 @@ func system_fs_handleDirHash(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
+	//The folder might have been removed by another window or client
+	if !fshAbs.FileExists(rpath) || !fshAbs.IsDir(rpath) {
+		utils.SendErrorResponse(w, "Directory not found")
+		return
+	}
+
 	//Get a list of files in this directory
 	/*
 		currentDir = filepath.ToSlash(filepath.Clean(rpath)) + "/"

+ 73 - 16
src/web/SystemAO/file_system/js/explorer/listing.js

@@ -293,23 +293,12 @@ function getDirHash(callback){
         data: {dir: currentPath},
         success: function(data){
             if (data.error !== undefined){
-                if (data.error == "Invalid dir given"){
-                    //Storage has been unmounted. Back to parent
-                    var pdir = currentPath.split("/");
-                    pdir.pop();pdir.pop();
-                    pdir = pdir.join("/");
-                    currentPath = pdir;
-                    listDirectory(currentPath, function(){
-                        window.location.reload();
-                    });
-                    return;
+                if (data.error == "Invalid dir given" || data.error == "Directory not found"){
+                    //Folder removed (e.g. deleted in another window). Back to the nearest existing parent
+                    returnToExistingParentDir(currentPath);
                 }else if (data.error == "Unable to resolve target directory"){
-                    //Resolve failed. Back to user:/
-                    var pdir = currentPath.split("/");
-                    pdir = pdir.shift() + "/";
-                    listDirectory(pdir, function(){
-                        window.location.reload();
-                    });
+                    //The whole root is gone (e.g. disk hot-swapped). Back to user:/
+                    returnToExistingParentDir("user:/");
                 }
             }
             callback(data);
@@ -317,6 +306,74 @@ function getDirHash(callback){
     })
 }
 
+//Walk up from the given path until a directory that still exists is found,
+//then list it. Falls back to user:/ when even the root is gone.
+var returningToParentDir = false;
+function returnToExistingParentDir(path){
+    if (returningToParentDir){
+        return;
+    }
+    returningToParentDir = true;
+    let startPath = currentPath;
+    let rootGone = false;
+
+    let finish = function(targetPath){
+        returningToParentDir = false;
+        if (currentPath != startPath){
+            //User already navigated somewhere else
+            return;
+        }
+        listDirectory(targetPath, function(){
+            if (rootGone){
+                //Reload the root list in the side bar
+                initRootDirs();
+            }
+        });
+    }
+
+    let probe = function(dir){
+        $.ajax({
+            url: "../../system/file_system/listDirHash",
+            data: {dir: dir},
+            success: function(data){
+                if (data.error === undefined){
+                    finish(dir);
+                    return;
+                }
+                if (data.error == "Unable to resolve target directory" || /^[^\/]+:\/$/.test(dir)){
+                    //Root no longer mounted, or reached the root and it is still not readable
+                    rootGone = true;
+                    if (dir == "user:/"){
+                        returningToParentDir = false;
+                        return;
+                    }
+                    finish("user:/");
+                    return;
+                }
+                probe(getParentDir(dir));
+            },
+            error: function(){
+                //Network or server issue. Try again on next refresh tick
+                returningToParentDir = false;
+            }
+        });
+    }
+
+    probe(getParentDir(path));
+}
+
+//Return the parent of a virtual path, e.g. user:/Document/Myfolder/ => user:/Document/
+function getParentDir(path){
+    if (/^[^\/]+:\/$/.test(path)){
+        //Already a root
+        return path;
+    }
+    let segments = path.replace(/\/+$/, "").split("/");
+    segments.pop();
+    let parent = segments.join("/") + "/";
+    return parent;
+}
+
 //=================================== FILE OPERATIONS ========================
 
 function refreshList(callback = undefined, keepSelectedItems = false){