Browse Source

Fixed shortcut loading bug in File System

Toby Chui 3 years ago
parent
commit
07e2689e71

+ 11 - 4
file_system.go

@@ -2529,12 +2529,19 @@ func system_fs_handleList(w http.ResponseWriter, r *http.Request) {
 		}
 
 		//Check if it is shortcut file. If yes, render a shortcut data struct
-		var shortCutInfo *shortcut.ShortcutData = nil
+		var shortCutInfo *arozfs.ShortcutData = nil
 		if filepath.Ext(f.Name()) == ".shortcut" {
 			//This is a shortcut file
-			shorcutData, err := shortcut.ReadShortcut(f.Name())
-			if err == nil {
-				shortCutInfo = shorcutData
+			fcontent, err := fshAbs.ReadFile(arozfs.ToSlash(filepath.Join(realpath, f.Name())))
+			if err != nil {
+				shortCutInfo = nil
+			} else {
+				shorcutData, err := shortcut.ReadShortcut(fcontent)
+				if err != nil {
+					shortCutInfo = nil
+				} else {
+					shortCutInfo = shorcutData
+				}
 			}
 		}
 

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

@@ -33,6 +33,14 @@ type File interface {
 	WriteString(s string) (n int, err error)
 }
 
+//A shortcut representing struct
+type ShortcutData struct {
+	Type string //The type of shortcut
+	Name string //The name of the shortcut
+	Path string //The path of shortcut
+	Icon string //The icon of shortcut
+}
+
 var (
 
 	/*

+ 21 - 39
mod/filesystem/shortcut/shortcut.go

@@ -2,10 +2,9 @@ package shortcut
 
 import (
 	"errors"
-	"io/ioutil"
 	"strings"
 
-	"imuslab.com/arozos/mod/common"
+	"imuslab.com/arozos/mod/filesystem/arozfs"
 )
 
 /*
@@ -14,43 +13,26 @@ import (
 	Author: tobychui
 */
 
-//A shortcut representing struct
-type ShortcutData struct {
-	Type string //The type of shortcut
-	Name string //The name of the shortcut
-	Path string //The path of shortcut
-	Icon string //The icon of shortcut
-}
+func ReadShortcut(shortcutContent []byte) (*arozfs.ShortcutData, error) {
+	//Split the content of the shortcut files into lines
+	fileContent := strings.ReplaceAll(strings.TrimSpace(string(shortcutContent)), "\r\n", "\n")
+	lines := strings.Split(fileContent, "\n")
+
+	if len(lines) < 4 {
+		return nil, errors.New("Corrupted Shortcut File")
+	}
 
-func ReadShortcut(shortcutFile string) (*ShortcutData, error) {
-	if common.FileExists(shortcutFile) {
-		content, err := ioutil.ReadFile(shortcutFile)
-		if err != nil {
-			return nil, err
-		}
-
-		//Split the content of the shortcut files into lines
-		fileContent := strings.ReplaceAll(strings.TrimSpace(string(content)), "\r\n", "\n")
-		lines := strings.Split(fileContent, "\n")
-
-		if len(lines) < 4 {
-			return nil, errors.New("Corrupted Shortcut File")
-		}
-
-		for i := 0; i < len(lines); i++ {
-			lines[i] = strings.TrimSpace(lines[i])
-		}
-
-		//Render it as shortcut data
-		result := ShortcutData{
-			Type: lines[0],
-			Name: lines[1],
-			Path: lines[2],
-			Icon: lines[3],
-		}
-
-		return &result, nil
-	} else {
-		return nil, errors.New("File not exists.")
+	for i := 0; i < len(lines); i++ {
+		lines[i] = strings.TrimSpace(lines[i])
 	}
+
+	//Render it as shortcut data
+	result := arozfs.ShortcutData{
+		Type: lines[0],
+		Name: lines[1],
+		Path: lines[2],
+		Icon: lines[3],
+	}
+
+	return &result, nil
 }

+ 11 - 5
mod/filesystem/static.go

@@ -19,6 +19,7 @@ import (
 	"net/url"
 
 	mimetype "github.com/gabriel-vasile/mimetype"
+	"imuslab.com/arozos/mod/filesystem/arozfs"
 	"imuslab.com/arozos/mod/filesystem/shortcut"
 )
 
@@ -33,7 +34,7 @@ type FileData struct {
 	Displaysize string
 	ModTime     int64
 	IsShared    bool
-	Shortcut    *shortcut.ShortcutData //This will return nil or undefined if it is not a shortcut file
+	Shortcut    *arozfs.ShortcutData //This will return nil or undefined if it is not a shortcut file
 }
 
 type TrashedFile struct {
@@ -109,11 +110,16 @@ func GetFileDataFromPath(fsh *FileSystemHandler, vpath string, realpath string,
 	displaySize := GetFileDisplaySize(fileSize, sizeRounding)
 	modtime, _ := fsh.FileSystemAbstraction.GetModTime(realpath)
 
-	var shortcutInfo *shortcut.ShortcutData = nil
+	var shortcutInfo *arozfs.ShortcutData = nil
 	if filepath.Ext(realpath) == ".shortcut" {
-		scd, err := shortcut.ReadShortcut(realpath)
-		if err == nil {
-			shortcutInfo = scd
+		shortcutContent, err := fsh.FileSystemAbstraction.ReadFile(realpath)
+		if err != nil {
+			shortcutInfo = nil
+		} else {
+			shortcutInfo, err = shortcut.ReadShortcut(shortcutContent)
+			if err != nil {
+				shortcutInfo = nil
+			}
 		}
 	}
 

+ 1 - 1
web/SystemAO/file_system/file_explorer.html

@@ -1775,7 +1775,7 @@
                         icon = ao_module_utils.getIconFromExt(ext);
 
                         //Check if the shortcut object exists
-                        if (files[i].Shortcut != undefined){
+                        if (files[i].Shortcut != undefined && files[i].Shortcut != null){
                             icon = files[i].Shortcut.Icon;
                         }