Переглянути джерело

Show sharer as owner of files on share:/ drive

File properties showed no owner for items on the read-only share:/ drive, because ownership records only cover a user's own home. When no owner is found, fall back to the new sharedDriveOwnerOf helper, which resolves the item through the sharefs drive and returns the user who shared it.

The helper returns an empty string for other drives, the drive root, and paths not shared with the user. Added table-driven tests for these cases.
Toby Chui 3 тижнів тому
батько
коміт
a8ca1470b0
3 змінених файлів з 81 додано та 0 видалено
  1. 4 0
      src/file_system.go
  2. 14 0
      src/file_system.share.go
  3. 63 0
      src/file_system_share_test.go

+ 4 - 0
src/file_system.go

@@ -3238,6 +3238,10 @@ func system_fs_getFileProperties(w http.ResponseWriter, r *http.Request) {
 
 	//Get file owner
 	owner := userinfo.GetFileOwner(fsh, vpath)
+	if owner == "" {
+		//A file someone shared with this user belongs to whoever shared it
+		owner = sharedDriveOwnerOf(userinfo, fsh, vpath)
+	}
 
 	if owner == "" {
 		//Handle special virtual roots

+ 14 - 0
src/file_system.share.go

@@ -102,6 +102,20 @@ func sharedDriveAttach(handlers []*fs.FileSystemHandler) []*fs.FileSystemHandler
 	return append(handlers, newSharedDriveHandler())
 }
 
+// sharedDriveOwnerOf returns who shared a file on the share:/ drive with the
+// user, or "" for anything else (another drive, the drive root, a path that
+// is not shared). The file ownership records only know a user's own home.
+func sharedDriveOwnerOf(userinfo *user.User, fsh *fs.FileSystemHandler, vpath string) string {
+	if !share.IsSharedDrive(fsh) {
+		return ""
+	}
+	item, err := fsh.FileSystemAbstraction.(*sharefs.ShareFileSystem).ItemOf(vpath, userinfo.Username)
+	if err != nil || item == nil {
+		return ""
+	}
+	return item.Owner
+}
+
 // sharedDriveCopyDenied returns why a copy of one of the given files cannot be
 // taken, or nil: a file shared with the user whose owner does not allow
 // downloading it, or the drive root itself.

+ 63 - 0
src/file_system_share_test.go

@@ -0,0 +1,63 @@
+package main
+
+import (
+	"os"
+	"path/filepath"
+	"testing"
+
+	fs "imuslab.com/arozos/mod/filesystem"
+	"imuslab.com/arozos/mod/filesystem/abstractions/localfs"
+	"imuslab.com/arozos/mod/filesystem/abstractions/sharefs"
+	"imuslab.com/arozos/mod/user"
+)
+
+// testShareResolver hands out a fixed drive root per user
+type testShareResolver map[string][]*sharefs.Item
+
+func (r testShareResolver) Items(username string) []*sharefs.Item {
+	return r[username]
+}
+
+func TestSharedDriveOwnerOf(t *testing.T) {
+	root := t.TempDir()
+	if err := os.MkdirAll(filepath.Join(root, "users", "test", "Music"), 0755); err != nil {
+		t.Fatalf("mkdir: %v", err)
+	}
+	if err := os.WriteFile(filepath.Join(root, "users", "test", "Music", "song.mp3"), []byte("mp3"), 0644); err != nil {
+		t.Fatalf("write: %v", err)
+	}
+	ownerAbs := localfs.NewLocalFileSystemAbstraction("user", root, "user", false)
+	ownerFsh := &fs.FileSystemHandler{UUID: "user", Hierarchy: "user", Path: root, FileSystemAbstraction: ownerAbs}
+	musicRoot, _ := ownerAbs.VirtualPathToRealPath("/Music", "test")
+
+	drive := &fs.FileSystemHandler{
+		UUID:      "share",
+		Hierarchy: "public",
+		FileSystemAbstraction: sharefs.New("share", testShareResolver{
+			"TC": {{Name: "Music", Owner: "test", IsDir: true, CanBrowse: true, Fsh: ownerFsh, Root: musicRoot}},
+		}, sharefs.Options{}),
+	}
+
+	tests := []struct {
+		name     string
+		username string
+		fsh      *fs.FileSystemHandler
+		vpath    string
+		want     string
+	}{
+		{"shared folder", "TC", drive, "share:/Music", "test"},
+		{"file inside a shared folder", "TC", drive, "share:/Music/song.mp3", "test"},
+		{"drive root has no single owner", "TC", drive, "share:/", ""},
+		{"not shared with this user", "carol", drive, "share:/Music/song.mp3", ""},
+		{"not shared at all", "TC", drive, "share:/Other/x.txt", ""},
+		{"another drive", "TC", ownerFsh, "user:/Music/song.mp3", ""},
+	}
+	for _, tc := range tests {
+		t.Run(tc.name, func(t *testing.T) {
+			got := sharedDriveOwnerOf(&user.User{Username: tc.username}, tc.fsh, tc.vpath)
+			if got != tc.want {
+				t.Errorf("got %q, want %q", got, tc.want)
+			}
+		})
+	}
+}