Explorar el Código

Fix trash bin leftovers and audio viewer layout

Trash: only entries named <name>.<unix time> directly inside a .trash folder are now treated as trashed items. Thumbnail leftovers such as .metadata and .DS_Store no longer show up as undeletable nameless entries. A new helper removes a .trash folder once no trashed item remains, and it is used when restoring, clearing, expiring and deleting from the trash bin. Tests are added in file_system_trash_test.go.

Audio viewer: the cover art and controls now sit in a scrollable .ap-stage, so a short window no longer clips them. The art shrinks, and then hides, as the window gets shorter. On narrow screens the queue moves under the player and the whole page scrolls, so the queue toggle button and its JS handler are removed.
Toby Chui hace 4 semanas
padre
commit
e812e85264
Se han modificado 4 ficheros con 201 adiciones y 28 borrados
  1. 42 16
      src/file_system.go
  2. 79 0
      src/file_system_trash_test.go
  3. 77 5
      src/web/Drive/css/viewer.css
  4. 3 7
      src/web/Drive/js/viewer.audio.js

+ 42 - 16
src/file_system.go

@@ -1340,10 +1340,7 @@ func system_fs_clearExpiredTrash() {
 			fshAbs.RemoveAll(file)
 
 			//Drop the .trash folder too once nothing is left in it
-			remaining, _ := fshAbs.Glob(filepath.Dir(file) + "/*")
-			if len(remaining) == 0 {
-				fshAbs.Remove(filepath.Dir(file))
-			}
+			system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(file))
 			removed++
 		}
 
@@ -1392,8 +1389,7 @@ func system_fs_WebSocketScanTrashBin(w http.ResponseWriter, r *http.Request) {
 			continue
 		}
 		err = thisFshAbs.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
-			oneLevelUpper := filepath.Base(filepath.Dir(path))
-			if oneLevelUpper == ".trash" {
+			if system_fs_isTrashEntry(path) {
 				//This is a trashbin dir.
 				file := path
 
@@ -1540,10 +1536,7 @@ func system_fs_restoreFile(w http.ResponseWriter, r *http.Request) {
 	fshAbs.Rename(realpath, targetPath)
 
 	//Check if the parent dir has no more fileds. If yes, remove it
-	filescounter, _ := fshAbs.Glob(filepath.Dir(realpath) + "/*")
-	if len(filescounter) == 0 {
-		fshAbs.Remove(filepath.Dir(realpath))
-	}
+	system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(realpath))
 
 	utils.SendOK(w)
 }
@@ -1574,15 +1567,44 @@ func system_fs_clearTrashBin(w http.ResponseWriter, r *http.Request) {
 		fshAbs := fshs[c].FileSystemAbstraction
 		fshAbs.RemoveAll(file)
 		//Check if its parent directory have no files. If yes, remove the dir itself as well.
-		filesInThisTrashBin, _ := fshAbs.Glob(filepath.Dir(file) + "/*")
-		if len(filesInThisTrashBin) == 0 {
-			fshAbs.Remove(filepath.Dir(file))
-		}
+		system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(file))
 	}
 
 	utils.SendOK(w)
 }
 
+/*
+Check if a path is a trashed item: an entry right inside a .trash folder whose
+name ends with the .<unix time> it was removed at. Anything else in there,
+like the .metadata folder a thumbnail of a trashed file leaves behind or a
+.DS_Store, is not something the user put in the trash.
+*/
+func system_fs_isTrashEntry(path string) bool {
+	if filepath.Base(filepath.Dir(path)) != ".trash" {
+		return false
+	}
+	ext := filepath.Ext(path)
+	if len(ext) < 2 {
+		return false
+	}
+	_, err := utils.StringToInt64(ext[1:])
+	return err == nil
+}
+
+// Remove a .trash folder (with the leftovers in it) once no trashed item is left inside
+func system_fs_removeTrashDirIfEmpty(fshAbs filesystem.FileSystemAbstraction, trashDir string) {
+	entries, err := fshAbs.Glob(filepath.ToSlash(trashDir) + "/*")
+	if err != nil {
+		return
+	}
+	for _, entry := range entries {
+		if system_fs_isTrashEntry(entry) {
+			return
+		}
+	}
+	fshAbs.RemoveAll(trashDir)
+}
+
 // Get all trash in a string list
 func system_fs_listTrash(username string) ([]string, []*filesystem.FileSystemHandler, error) {
 	userinfo, _ := userHandler.GetUserInfoFromUsername(username)
@@ -1603,8 +1625,7 @@ func system_fs_listTrash(username string) ([]string, []*filesystem.FileSystemHan
 		thisFshAbs := thisFsh.FileSystemAbstraction
 		rootPath, _ := thisFshAbs.VirtualPathToRealPath("", userinfo.Username)
 		err := thisFshAbs.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
-			oneLevelUpper := filepath.Base(filepath.Dir(path))
-			if oneLevelUpper == ".trash" {
+			if system_fs_isTrashEntry(path) {
 				//This is a trashbin dir.
 				files = append(files, path)
 				fshs = append(fshs, thisFsh)
@@ -2593,6 +2614,11 @@ func system_fs_handleOpr(w http.ResponseWriter, r *http.Request) {
 					return
 				}
 
+				//Deleted from the trash bin. Drop the .trash folder once it is empty
+				if filepath.Base(filepath.Dir(rsrcFile)) == ".trash" {
+					system_fs_removeTrashDirIfEmpty(srcFshAbs, filepath.Dir(rsrcFile))
+				}
+
 			} else if operation == "recycle" {
 				//Put it into a subfolder named trash and allow it to to be removed later
 				if !srcFshAbs.FileExists(rsrcFile) {

+ 79 - 0
src/file_system_trash_test.go

@@ -0,0 +1,79 @@
+package main
+
+import (
+	"os"
+	"path/filepath"
+	"testing"
+
+	"imuslab.com/arozos/mod/filesystem/abstractions/localfs"
+)
+
+/*
+Only entries named <name>.<unix time> right inside a .trash folder are trashed
+items. The .metadata folder a thumbnail of a trashed file leaves behind used
+to show up in the trash as a nameless folder that could not be deleted.
+*/
+func TestIsTrashEntry(t *testing.T) {
+	tests := []struct {
+		name string
+		path string
+		want bool
+	}{
+		{"trashed file", "/u/.metadata/.trash/report.pdf.1712345678", true},
+		{"trashed folder", "/u/.metadata/.trash/Photos.1712345678", true},
+		{"trashed hidden file", "/u/.metadata/.trash/.bashrc.1712345678", true},
+		{"thumbnail leftover", "/u/.metadata/.trash/.metadata", false},
+		{"finder leftover", "/u/.metadata/.trash/.DS_Store", false},
+		{"no timestamp", "/u/.metadata/.trash/report.pdf", false},
+		{"no extension", "/u/.metadata/.trash/report", false},
+		{"not in a trash folder", "/u/Desktop/report.pdf.1712345678", false},
+		{"inside a trashed folder", "/u/.metadata/.trash/Photos.1712345678/a.jpg", false},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if got := system_fs_isTrashEntry(tt.path); got != tt.want {
+				t.Errorf("system_fs_isTrashEntry(%q) = %v, want %v", tt.path, got, tt.want)
+			}
+		})
+	}
+}
+
+func TestRemoveTrashDirIfEmpty(t *testing.T) {
+	tests := []struct {
+		name     string
+		files    []string
+		wantKept bool
+	}{
+		{"empty", nil, false},
+		{"only thumbnail leftovers", []string{".metadata/.cache/a.png.1712345678.jpg", ".DS_Store"}, false},
+		{"trashed item left", []string{"a.png.1712345678", ".metadata/.cache/a.png.1712345678.jpg"}, true},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			root := t.TempDir()
+			trashDir := filepath.Join(root, ".metadata", ".trash")
+			if err := os.MkdirAll(trashDir, 0755); err != nil {
+				t.Fatalf("mkdir: %v", err)
+			}
+			for _, f := range tt.files {
+				p := filepath.Join(trashDir, f)
+				if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
+					t.Fatalf("mkdir: %v", err)
+				}
+				if err := os.WriteFile(p, []byte("x"), 0644); err != nil {
+					t.Fatalf("write: %v", err)
+				}
+			}
+
+			fshAbs := localfs.NewLocalFileSystemAbstraction("test", root, "user", false)
+			system_fs_removeTrashDirIfEmpty(fshAbs, trashDir)
+
+			_, err := os.Stat(trashDir)
+			if kept := err == nil; kept != tt.wantKept {
+				t.Errorf("trash folder kept = %v, want %v", kept, tt.wantKept)
+			}
+		})
+	}
+}

+ 77 - 5
src/web/Drive/css/viewer.css

@@ -992,13 +992,29 @@ button.vw-pill-label:hover {
     position: relative;
     overflow: hidden;
     display: flex;
+}
+
+/* Scrolls instead of clipping the art and the controls when the window is short */
+.ap-stage {
+    flex: 1;
+    min-width: 0;
+    overflow-y: auto;
+    display: flex;
     flex-direction: column;
     align-items: center;
-    justify-content: center;
     gap: 22px;
     padding: 30px 28px 66px;
 }
 
+/* Centered while it fits; justify-content: center would cut off the top */
+.ap-stage>:first-child {
+    margin-top: auto;
+}
+
+.ap-stage>:last-child {
+    margin-bottom: auto;
+}
+
 .ap-bg {
     position: absolute;
     inset: -60px;
@@ -1132,6 +1148,7 @@ button.vw-pill-label:hover {
     align-items: center;
     gap: 6px;
     color: var(--vw-text-2);
+    flex: none;
 }
 
 .ap-extra .vw-range {
@@ -1804,13 +1821,11 @@ button.vw-pill-label:hover {
 /* ---------------------------------------------------------------- */
 @media (max-width: 900px) {
 
-    .ap-side,
     .iv-info,
     .dv-toc {
         display: none;
     }
 
-    .ap-side.open,
     .iv-info.open,
     .dv-toc.open {
         display: flex;
@@ -1845,6 +1860,63 @@ button.vw-pill-label:hover {
         margin: 16px;
         padding: 28px 22px;
     }
+
+    /* The queue goes under the player and the whole page scrolls */
+    .ap {
+        flex-direction: column;
+        overflow-y: auto;
+    }
+
+    .ap-main {
+        flex: none;
+    }
+
+    .ap-stage {
+        overflow: visible;
+    }
+
+    .ap-side {
+        width: auto;
+        flex: 1 0 auto;
+        border-left: 0;
+        border-top: 1px solid var(--vw-line);
+    }
+
+    .ap-list {
+        flex: none;
+        overflow: visible;
+    }
+}
+
+/* Short windows: a smaller cover, then none, then no visualiser */
+@media (max-height: 760px) {
+    .ap-art {
+        width: min(200px, 28vh);
+        border-radius: 16px;
+    }
+
+    .ap-stage {
+        gap: 14px;
+        padding-top: 20px;
+        padding-bottom: 44px;
+    }
+}
+
+@media (max-height: 560px) {
+    .ap-art {
+        display: none;
+    }
+}
+
+@media (max-height: 380px) {
+    .ap-vis {
+        display: none;
+    }
+
+    .ap-stage {
+        padding-top: 12px;
+        padding-bottom: 16px;
+    }
 }
 
 @media (max-width: 640px) {
@@ -1889,9 +1961,9 @@ button.vw-pill-label:hover {
         height: 36px;
     }
 
-    .ap-main {
+    .ap-stage {
         gap: 16px;
-        padding: 18px 16px 62px;
+        padding: 18px 16px 24px;
     }
 
     .ap-meta h2 {

+ 3 - 7
src/web/Drive/js/viewer.audio.js

@@ -218,7 +218,7 @@
         var st = { mode: BROWSER_FORMATS[ext] ? "direct" : "transcode", offset: 0, duration: 0, speed: mem.speed || 1, destroyed: false, tried: false, artURL: "" };
 
         ctx.body.innerHTML =
-            '<div class="ap"><div class="ap-main"><div class="ap-bg"></div>' +
+            '<div class="ap"><div class="ap-main"><div class="ap-bg"></div><div class="ap-stage">' +
             '<div class="ap-art">' + I("music") + '</div>' +
             '<div class="ap-meta"><h2></h2><p></p></div>' +
             '<canvas class="ap-vis"></canvas>' +
@@ -231,9 +231,8 @@
             '<button class="vp-btn' + (mem.repeat != "off" ? " on" : "") + '" data-c="repeat" title="' + esc(T("vw/repeat", "Repeat")) + ' (R)">' + repeatIcon(mem.repeat) + '</button>' +
             '</div>' +
             '<div class="ap-extra"><button class="vp-btn" data-c="mute"></button><input class="vw-range" type="range" min="0" max="1" step="0.02" data-c="vol" aria-label="' + esc(T("vw/volume", "Volume")) + '">' +
-            '<button class="vp-btn" data-c="speed" title="' + esc(T("vw/speed", "Speed")) + '" style="width:auto;padding:0 10px;font-size:13px;font-weight:600">' + st.speed + 'x</button>' +
-            '<button class="vp-btn" data-c="queue" title="' + esc(T("vw/queue", "Queue")) + '">' + I("list") + '</button></div>' +
-            '</div><aside class="vw-panel ap-side"><div class="ap-side-head"><span>' + esc(T("vw/queue", "Queue")) + '</span><small>' +
+            '<button class="vp-btn" data-c="speed" title="' + esc(T("vw/speed", "Speed")) + '" style="width:auto;padding:0 10px;font-size:13px;font-weight:600">' + st.speed + 'x</button></div>' +
+            '</div></div><aside class="vw-panel ap-side"><div class="ap-side-head"><span>' + esc(T("vw/queue", "Queue")) + '</span><small>' +
             esc(T("drag/items", "{n} items", { n: ctx.list.length })) + '</small></div><div class="ap-list vw-scroll"></div></aside></div><audio preload="metadata"></audio>';
 
         var main = ctx.body.querySelector(".ap-main");
@@ -244,7 +243,6 @@
         var playBtn = main.querySelector('[data-c="play"]');
         var volInput = main.querySelector('[data-c="vol"]');
         var muteBtn = main.querySelector('[data-c="mute"]');
-        var side = ctx.body.querySelector(".ap-side");
         var listEl = ctx.body.querySelector(".ap-list");
 
         main.querySelector("h2").textContent = title(item, null);
@@ -593,8 +591,6 @@
                             }
                         };
                     }));
-                } else if (c == "queue") {
-                    side.classList.toggle("open");
                 }
             });
         });