Kaynağa Gözat

Updated File Manager listDir to use fssort

Toby Chui 3 yıl önce
ebeveyn
işleme
f838dbee67

+ 24 - 76
file_system.go

@@ -29,6 +29,7 @@ import (
 	"imuslab.com/arozos/mod/filesystem"
 	"imuslab.com/arozos/mod/filesystem/arozfs"
 	fsp "imuslab.com/arozos/mod/filesystem/fspermission"
+	"imuslab.com/arozos/mod/filesystem/fssort"
 	"imuslab.com/arozos/mod/filesystem/fuzzy"
 	hidden "imuslab.com/arozos/mod/filesystem/hidden"
 	"imuslab.com/arozos/mod/filesystem/localversion"
@@ -2472,8 +2473,6 @@ func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
 
 	fshAbs := fsh.FileSystemAbstraction
 
-	var parsedFilelist []filesystem.FileData
-
 	//Normal file systems
 	realpath, err := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
 	if err != nil {
@@ -2502,65 +2501,19 @@ func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
 		sortMode = "default"
 	}
 
-	//Check for really special exception in where the path contains [ or ] which cannot be handled via Golang Glob function
-	/*
-		files, err := fshAbs.Glob(realpath + "/*")
-		if err != nil {
-			systemWideLogger.PrintAndLog("File System", "Unable to list dir: "+err.Error(), err)
-			return
-		}
-
-		var shortCutInfo *shortcut.ShortcutData = nil
-		for _, v := range files {
-			//Check if it is hidden file
-			isHidden, _ := hidden.IsHidden(v, false)
-			if showHidden != "true" && isHidden {
-				//Skipping hidden files
-				continue
-			}
-
-			//Check if this is an aodb file
-			if filepath.Base(v) == "aofs.db" || filepath.Base(v) == "aofs.db.lock" {
-				//Database file (reserved)
-				continue
-			}
-
-			//Check if it is shortcut file. If yes, render a shortcut data struct
-			if filepath.Ext(v) == ".shortcut" {
-				//This is a shortcut file
-				shorcutData, err := shortcut.ReadShortcut(v)
-				if err == nil {
-					shortCutInfo = shorcutData
-				}
-			}
-
-			fstat, _ := fshAbs.Stat(v)
-
-			rawsize := fstat.Size()
-			modtime := fstat.ModTime().Unix()
-			thisvPath, _ := fshAbs.RealPathToVirtualPath(v, userinfo.Username)
-			thisFile := filesystem.FileData{
-				Filename:    filepath.Base(v),
-				Filepath:    currentDir + filepath.Base(v),
-				Realpath:    v,
-				IsDir:       fstat.IsDir(),
-				Filesize:    rawsize,
-				Displaysize: filesystem.GetFileDisplaySize(rawsize, 2),
-				ModTime:     modtime,
-				IsShared:    shareManager.FileIsShared(userinfo, thisvPath),
-				Shortcut:    shortCutInfo,
-			}
-
-			parsedFilelist = append(parsedFilelist, thisFile)
-		}
-	*/
-
 	files, err := fshAbs.ReadDir(realpath)
 	if err != nil {
 		systemWideLogger.PrintAndLog("File System", "Unable to read dir: "+err.Error(), err)
 		return
 	}
 
+	//Remapping use parsed list
+	parsedFilelist := map[string]filesystem.FileData{}
+
+	//Sorting use list
+	realpathList := []string{}
+	fileInfoList := []fs.FileInfo{}
+
 	for _, f := range files {
 		//Check if it is hidden file
 		isHidden, _ := hidden.IsHidden(f.Name(), false)
@@ -2585,7 +2538,10 @@ func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
 			}
 		}
 
-		statInfo, _ := f.Info()
+		statInfo, err := f.Info()
+		if err != nil {
+			continue
+		}
 		thisvPath, _ := fshAbs.RealPathToVirtualPath(filepath.Join(realpath, f.Name()), userinfo.Username)
 		thisFile := filesystem.FileData{
 			Filename:    f.Name(),
@@ -2599,31 +2555,23 @@ func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
 			Shortcut:    shortCutInfo,
 		}
 
-		parsedFilelist = append(parsedFilelist, thisFile)
+		parsedFilelist[currentDir+f.Name()] = thisFile
+		realpathList = append(realpathList, currentDir+f.Name())
+		fileInfoList = append(fileInfoList, statInfo)
 	}
 
 	//Sort the filelist
-	if sortMode == "default" {
-		//Sort by name, convert filename to window sorting methods
-		sort.Slice(parsedFilelist, func(i, j int) bool {
-			return strings.ToLower(parsedFilelist[i].Filename) < strings.ToLower(parsedFilelist[j].Filename)
-		})
-	} else if sortMode == "reverse" {
-		//Sort by reverse name
-		sort.Slice(parsedFilelist, func(i, j int) bool {
-			return strings.ToLower(parsedFilelist[i].Filename) > strings.ToLower(parsedFilelist[j].Filename)
-		})
-	} else if sortMode == "smallToLarge" {
-		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize < parsedFilelist[j].Filesize })
-	} else if sortMode == "largeToSmall" {
-		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].Filesize > parsedFilelist[j].Filesize })
-	} else if sortMode == "mostRecent" {
-		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime > parsedFilelist[j].ModTime })
-	} else if sortMode == "leastRecent" {
-		sort.Slice(parsedFilelist, func(i, j int) bool { return parsedFilelist[i].ModTime < parsedFilelist[j].ModTime })
+	sortedRealpathList := fssort.SortFileList(realpathList, fileInfoList, sortMode)
+	results := []filesystem.FileData{}
+
+	for _, thisRpath := range sortedRealpathList {
+		val, ok := parsedFilelist[thisRpath]
+		if ok {
+			results = append(results, val)
+		}
 	}
 
-	jsonString, _ := json.Marshal(parsedFilelist)
+	jsonString, _ := json.Marshal(results)
 	common.SendJSONResponse(w, string(jsonString))
 
 }

+ 23 - 2
mod/agi/agi.file.go

@@ -3,6 +3,7 @@ package agi
 import (
 	"encoding/json"
 	"errors"
+	"io/fs"
 	"log"
 	"os"
 	"path/filepath"
@@ -335,8 +336,18 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 				return reply
 			}
 
+			fileList := []string{}
+			fis := []fs.FileInfo{}
+			for _, thisFile := range suitableFiles {
+				fi, err := fsh.FileSystemAbstraction.Stat(thisFile)
+				if err == nil {
+					fileList = append(fileList, thisFile)
+					fis = append(fis, fi)
+				}
+			}
+
 			//Sort the files
-			newFilelist := fssort.SortFileList(suitableFiles, userSortMode)
+			newFilelist := fssort.SortFileList(fileList, fis, userSortMode)
 
 			//Return the results in virtual paths
 			results := []string{}
@@ -407,8 +418,18 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			return reply
 		}
 
+		fileList := []string{}
+		fis := []fs.FileInfo{}
+		for _, thisFile := range suitableFiles {
+			fi, err := fsh.FileSystemAbstraction.Stat(thisFile)
+			if err == nil {
+				fileList = append(fileList, thisFile)
+				fis = append(fis, fi)
+			}
+		}
+
 		//Sort the files
-		newFilelist := fssort.SortFileList(suitableFiles, userSortMode)
+		newFilelist := fssort.SortFileList(fileList, fis, userSortMode)
 
 		//Parse the results (Only extract the filepath)
 		results := []string{}

+ 13 - 14
mod/filesystem/fssort/fssort.go

@@ -1,7 +1,8 @@
 package fssort
 
 import (
-	"os"
+	"fmt"
+	"io/fs"
 	"path/filepath"
 	"sort"
 	"strings"
@@ -17,23 +18,24 @@ type sortBufferedStructure struct {
 /*
 	Quick utilties to sort file list according to different modes
 */
-func SortFileList(filelistRealpath []string, sortMode string) []string {
+func SortFileList(filelistRealpath []string, fileInfos []fs.FileInfo, sortMode string) []string {
 	//Build a filelist with information based on the given filelist
 	parsedFilelist := []*sortBufferedStructure{}
-	for _, file := range filelistRealpath {
+	if len(filelistRealpath) != len(fileInfos) {
+		//Invalid usage
+		fmt.Println("[fssort] Invalid Usage!")
+		return filelistRealpath
+	}
+	for i, file := range filelistRealpath {
 		thisFileInfo := sortBufferedStructure{
 			Filename: filepath.Base(file),
 			Filepath: file,
 		}
 
 		//Get Filesize
-		fi, err := os.Stat(file)
-		if err != nil {
-			thisFileInfo.Filesize = 0
-		} else {
-			thisFileInfo.Filesize = fi.Size()
-			thisFileInfo.ModTime = fi.ModTime().Unix()
-		}
+		fi := fileInfos[i]
+		thisFileInfo.Filesize = fi.Size()
+		thisFileInfo.ModTime = fi.ModTime().Unix()
 
 		parsedFilelist = append(parsedFilelist, &thisFileInfo)
 
@@ -71,10 +73,7 @@ func SortFileList(filelistRealpath []string, sortMode string) []string {
 }
 
 func SortModeIsSupported(sortMode string) bool {
-	if !contains(sortMode, []string{"default", "reverse", "smallToLarge", "largeToSmall", "mostRecent", "leastRecent", "smart"}) {
-		return false
-	}
-	return true
+	return contains(sortMode, []string{"default", "reverse", "smallToLarge", "largeToSmall", "mostRecent", "leastRecent", "smart"})
 }
 
 func contains(item string, slice []string) bool {

+ 6 - 2
mod/filesystem/metadata/metadata.go

@@ -4,6 +4,7 @@ import (
 	"encoding/base64"
 	"encoding/json"
 	"errors"
+	"io/fs"
 	"log"
 	"net/http"
 	"os"
@@ -254,17 +255,20 @@ func (rh *RenderHandler) HandleLoadCache(w http.ResponseWriter, r *http.Request,
 	}
 
 	pendingFiles := []string{}
+	sortingFileInfos := []fs.FileInfo{}
 	pendingFolders := []string{}
 	for _, fileInfo := range fis {
-		if fileInfo.IsDir() {
+		if !fileInfo.IsDir() {
 			pendingFiles = append(pendingFiles, filepath.Join(targetPath, fileInfo.Name()))
+			info, _ := fileInfo.Info()
+			sortingFileInfos = append(sortingFileInfos, info)
 		} else {
 			pendingFolders = append(pendingFolders, filepath.Join(targetPath, fileInfo.Name()))
 		}
 	}
 	pendingFiles = append(pendingFiles, pendingFolders...)
 
-	files := fssort.SortFileList(pendingFiles, sortmode)
+	files := fssort.SortFileList(pendingFiles, sortingFileInfos, sortmode)
 
 	//Updated implementation 24/12/2020: Load image with cache first before rendering those without
 	for _, file := range files {