Browse Source

Added experimental fix for absolute path on Windows

TC pushbot 5 2 years ago
parent
commit
2ce0cee375
4 changed files with 36 additions and 87 deletions
  1. 13 3
      desktop.go
  2. 1 0
      mod/agi/agi.file.go
  3. 13 76
      mod/filesystem/abstractions/localfs/localfs.go
  4. 9 8
      mod/filesystem/arozfs/arozfs.go

+ 13 - 3
desktop.go

@@ -3,6 +3,7 @@ package main
 import (
 	"encoding/json"
 	"errors"
+	"fmt"
 	"log"
 	"net/http"
 	"os"
@@ -189,7 +190,11 @@ func desktop_handleShortcutRename(w http.ResponseWriter, r *http.Request) {
 
 func desktop_listFiles(w http.ResponseWriter, r *http.Request) {
 	//Check if the user directory already exists
-	userinfo, _ := userHandler.GetUserInfoFromRequest(w, r)
+	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
+	if err != nil {
+		utils.SendErrorResponse(w, "user not logged in!")
+		return
+	}
 	username := userinfo.Username
 
 	//Initiate the user folder structure. Do nothing if the structure already exists.
@@ -202,8 +207,13 @@ func desktop_listFiles(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	fshAbs := fsh.FileSystemAbstraction
-	userDesktopRealpath, _ := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
+	userDesktopRealpath, err := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
+	if err != nil {
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
 
+	fmt.Println("GLOBING PATH", userDesktopRealpath)
 	files, err := fshAbs.Glob(userDesktopRealpath + "/*")
 	if err != nil {
 		utils.SendErrorResponse(w, "Desktop file load failed")
@@ -227,7 +237,7 @@ func desktop_listFiles(w http.ResponseWriter, r *http.Request) {
 		IconY         int
 	}
 
-	var desktopFiles []desktopObject
+	desktopFiles := []desktopObject{}
 	for _, this := range files {
 		//Always use linux convension for directory seperator
 		if filepath.Base(this)[:1] == "." {

+ 1 - 0
mod/agi/agi.file.go

@@ -515,6 +515,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User, scriptFsh
 			}
 			fstat, _ := de.Info()
 			vpath, _ := realpathToVirtualpath(fsh, filepath.ToSlash(filepath.Join(rpath, de.Name())), u)
+
 			thisInfo := fileInfo{
 				Filename: de.Name(),
 				Filepath: vpath,

+ 13 - 76
mod/filesystem/abstractions/localfs/localfs.go

@@ -83,89 +83,26 @@ func (l LocalFileSystemAbstraction) Close() error {
 */
 
 func (l LocalFileSystemAbstraction) VirtualPathToRealPath(subpath string, username string) (string, error) {
-	subpath = filepath.ToSlash(subpath)
-	if strings.HasPrefix(subpath, l.UUID+":") {
-		//This is full virtual path. Trim the uuid and correct the subpath
-		subpath = subpath[len(l.UUID+":"):]
+	rpath, err := arozfs.GenericVirtualPathToRealPathTranslator(l.UUID, l.Hierarchy, subpath, username)
+	if err != nil {
+		return "", err
 	}
 
-	if l.Hierarchy == "user" {
-		return filepath.ToSlash(filepath.Join(l.Rootpath, "users", username, subpath)), nil
-	} else if l.Hierarchy == "public" {
-		return filepath.ToSlash(filepath.Join(l.Rootpath, subpath)), nil
-	}
-	return "", arozfs.ErrVpathResolveFailed
+	rpath = filepath.Join(l.Rootpath, rpath)
+
+	//fmt.Println("VIRTUAL TO REAL", l.Rootpath, subpath, rpath)
+	return rpath, nil
 }
 
 func (l LocalFileSystemAbstraction) RealPathToVirtualPath(fullpath string, username string) (string, error) {
-	/*
-		thisStorageRootAbs, err := filepath.Abs(l.Rootpath)
-		if err != nil {
-			//Fail to abs this path. Maybe this is a emulated file system?
-			thisStorageRootAbs = l.Rootpath
-		}
-		thisStorageRootAbs = filepath.ToSlash(filepath.Clean(thisStorageRootAbs))
-
-		subPath := ""
-		if len(fullpath) > len(l.Rootpath) && filepath.ToSlash(fullpath[:len(l.Rootpath)]) == filepath.ToSlash(l.Rootpath) {
-			//This realpath is in contained inside this storage root
-			subtractionPath := l.Rootpath
-			if l.Hierarchy == "user" {
-				//Check if this file is belongs to this user
-				startOffset := len(filepath.Clean(l.Rootpath) + "/users/")
-				if len(fullpath) < startOffset+len(username) {
-					//This file is not owned by this user
-					return "", errors.New("File not owned by this user")
-				} else {
-					userNameMatch := fullpath[startOffset : startOffset+len(username)]
-					if userNameMatch != username {
-						//This file is not owned by this user
-						return "", errors.New("File not owned by this user")
-					}
-				}
-
-				//Generate subtraction path
-				subtractionPath = filepath.ToSlash(filepath.Clean(filepath.Join(l.Rootpath, "users", username)))
-			}
-
-			if len(subtractionPath) < len(fullpath) {
-				subPath = fullpath[len(subtractionPath):]
-			}
-
-		} else if len(fullpath) > len(thisStorageRootAbs) && filepath.ToSlash(fullpath[:len(thisStorageRootAbs)]) == filepath.ToSlash(thisStorageRootAbs) {
-			//The realpath contains the absolute path of this storage root
-			subtractionPath := thisStorageRootAbs
-			if l.Hierarchy == "user" {
-				subtractionPath = thisStorageRootAbs + "/users/" + username + "/"
-			}
-
-			if len(subtractionPath) < len(fullpath) {
-				subPath = fullpath[len(subtractionPath):]
-			}
-		} else if filepath.ToSlash(fullpath) == filepath.ToSlash(l.Rootpath) {
-			//Storage Root's root
-			subPath = ""
-		}
-
-		if len(subPath) > 1 && subPath[:1] == "/" {
-			subPath = subPath[1:]
-		}
-	*/
-	fullpath = filepath.ToSlash(fullpath)
-	if strings.HasPrefix(fullpath, l.UUID+":") && !utils.FileExists(fullpath) {
-		return "", arozfs.ErrRpathResolveFailed
-	}
-	prefix := filepath.ToSlash(filepath.Join(l.Rootpath, "users", username))
-	if l.Hierarchy == "public" {
-		prefix = filepath.ToSlash(l.Rootpath)
-	}
-	fullpath = filepath.ToSlash(filepath.Clean(fullpath))
-	subPath := strings.TrimPrefix(fullpath, prefix)
-	if !strings.HasPrefix(subPath, "/") {
-		subPath = "/" + subPath
+	fullpath = arozfs.ToSlash(fullpath)
+	if strings.HasPrefix(fullpath, arozfs.ToSlash(filepath.Clean(l.Rootpath))) {
+		fullpath = strings.TrimPrefix(fullpath, arozfs.ToSlash(filepath.Clean(l.Rootpath)))
 	}
 
-	return l.UUID + ":" + filepath.ToSlash(subPath), nil
+	vpath, err := arozfs.GenericRealPathToVirtualPathTranslator(l.UUID, l.Hierarchy, arozfs.ToSlash(fullpath), username)
+	//fmt.Println("REAL TO VIRTUAL", arozfs.ToSlash(filepath.Clean(l.Rootpath)), fullpath, vpath)
+	return vpath, err
 }
 
 func (l LocalFileSystemAbstraction) FileExists(realpath string) bool {

+ 9 - 8
mod/filesystem/arozfs/arozfs.go

@@ -35,7 +35,7 @@ type File interface {
 	WriteString(s string) (n int, err error)
 }
 
-//A shortcut representing struct
+// A shortcut representing struct
 type ShortcutData struct {
 	Type string //The type of shortcut
 	Name string //The name of the shortcut
@@ -71,12 +71,12 @@ var (
 	ErrNullOperation         = errors.New("FS_NULL_OPR")
 )
 
-//Generate a File Manager redirection error message
+// Generate a File Manager redirection error message
 func NewRedirectionError(targetVpath string) error {
 	return errors.New("Redirect:" + targetVpath)
 }
 
-//Check if a file system is network drive
+// Check if a file system is network drive
 func IsNetworkDrive(fstype string) bool {
 	if fstype == "webdav" || fstype == "ftp" || fstype == "smb" || fstype == "sftp" {
 		return true
@@ -85,7 +85,7 @@ func IsNetworkDrive(fstype string) bool {
 	return false
 }
 
-//Get a list of supported file system types for mounting via arozos
+// Get a list of supported file system types for mounting via arozos
 func GetSupportedFileSystemTypes() []string {
 	return []string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs", "webdav", "ftp", "smb", "sftp"}
 }
@@ -94,7 +94,7 @@ func GetSupportedFileSystemTypes() []string {
 	Standard file system abstraction translate function
 */
 
-//Generic virtual path to real path translator
+// Generic virtual path to real path translator
 func GenericVirtualPathToRealPathTranslator(uuid string, hierarchy string, subpath string, username string) (string, error) {
 	subpath = ToSlash(filepath.Clean(subpath))
 	subpath = ToSlash(filepath.Clean(strings.TrimSpace(subpath)))
@@ -118,7 +118,7 @@ func GenericVirtualPathToRealPathTranslator(uuid string, hierarchy string, subpa
 	return "", errors.New("unsupported filesystem hierarchy")
 }
 
-//Generic real path to virtual path translator
+// Generic real path to virtual path translator
 func GenericRealPathToVirtualPathTranslator(uuid string, hierarchy string, rpath string, username string) (string, error) {
 	rpath = ToSlash(filepath.Clean(strings.TrimSpace(rpath)))
 	if strings.HasPrefix(rpath, "./") {
@@ -137,10 +137,11 @@ func GenericRealPathToVirtualPathTranslator(uuid string, hierarchy string, rpath
 	if !strings.HasPrefix(rpath, "/") {
 		rpath = "/" + rpath
 	}
+
 	return uuid + ":" + rpath, nil
 }
 
-//Generic function for abstraction driver to filter incoming paths
+// Generic function for abstraction driver to filter incoming paths
 func GenericPathFilter(filename string) string {
 	filename = ToSlash(filepath.Clean(filename))
 	rawpath := strings.TrimSpace(filename)
@@ -152,7 +153,7 @@ func GenericPathFilter(filename string) string {
 	return rawpath
 }
 
-//Filter illegal characters in filename
+// Filter illegal characters in filename
 func FilterIllegalCharInFilename(filename string, replacement string) string {
 	re, _ := regexp.Compile(`[\\\[\]$?#<>+%!"'|{}:@]`)
 	return re.ReplaceAllString(filename, replacement)