Ver Fonte

Fixed readdir sorting issue in Music Embedded playe

Toby Chui há 3 anos atrás
pai
commit
12b7b91f6f

+ 134 - 88
mod/agi/agi.file.go

@@ -1,8 +1,11 @@
 package agi
 
 import (
+	"crypto/md5"
+	"encoding/hex"
 	"encoding/json"
 	"errors"
+	"io"
 	"io/fs"
 	"log"
 	"os"
@@ -38,8 +41,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -50,24 +52,21 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		content, err := call.Argument(1).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check if there is quota for the given length
 		if !u.StorageQuota.HaveSpace(int64(len(content))) {
 			//User have no remaining storage quota
 			g.raiseError(errors.New("Storage Quota Fulled"))
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Translate the virtual path to realpath
 		fsh, rpath, err := virtualPathToRealPath(vpath, u)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check if file already exists.
@@ -84,8 +83,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		err = fsh.FileSystemAbstraction.WriteFile(rpath, []byte(content), 0755)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Add the filesize to user quota
@@ -99,8 +97,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -112,8 +109,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, rpath, err := virtualPathToRealPath(vpath, u)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check if file already exists.
@@ -126,8 +122,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			}
 		} else {
 			g.raiseError(errors.New("File not exists"))
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Remove the file
@@ -142,8 +137,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -155,16 +149,14 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, rpath, err := virtualPathToRealPath(vpath, u)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Create and write to file using ioUtil
 		content, err := fsh.FileSystemAbstraction.ReadFile(rpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		reply, _ := vm.ToValue(string(content))
 		return reply
@@ -177,16 +169,14 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			vpath, err := call.Argument(0).ToString()
 			if err != nil {
 				g.raiseError(err)
-				reply, _ := vm.ToValue(false)
-				return reply
+				return otto.FalseValue()
 			}
 
 			//Translate the virtual path to realpath
 			fsh, rpath, err := virtualPathToRealPath(vpath, u)
 			if err != nil {
 				g.raiseError(err)
-				reply, _ := vm.ToValue(false)
-				return reply
+				return otto.FalseValue()
 			}
 			fshAbs := fsh.FileSystemAbstraction
 
@@ -194,8 +184,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			fileList, err := fshAbs.Glob(rpath)
 			if err != nil {
 				g.raiseError(err)
-				reply, _ := vm.ToValue(false)
-				return reply
+				return otto.FalseValue()
 			}
 
 			//Translate all paths to virtual paths
@@ -221,8 +210,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		mode, err := call.Argument(1).ToString()
 		if err != nil {
@@ -232,8 +220,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, rpath, err := virtualPathToRealPath(vpath, u)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		results := []string{}
 		fsh.FileSystemAbstraction.Walk(rpath, func(path string, info os.FileInfo, err error) error {
@@ -273,8 +260,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		regex, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		userSortMode, err := call.Argument(1).ToString()
@@ -309,8 +295,8 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			//Rewrite and validate the sort mode
 			if userSortMode == "user" {
 				//Use user sorting mode.
-				if g.Option.UserHandler.GetDatabase().KeyExists("fs-sortpref", u.Username+"/"+filepath.ToSlash(vrootPath)) {
-					g.Option.UserHandler.GetDatabase().Read("fs-sortpref", u.Username+"/"+filepath.ToSlash(vrootPath), &userSortMode)
+				if g.Option.UserHandler.GetDatabase().KeyExists("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vrootPath))) {
+					g.Option.UserHandler.GetDatabase().Read("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vrootPath)), &userSortMode)
 				} else {
 					userSortMode = "default"
 				}
@@ -325,15 +311,13 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			fsh, rrootPath, err := virtualPathToRealPath(vrootPath, u)
 			if err != nil {
 				g.raiseError(err)
-				reply, _ := vm.ToValue(false)
-				return reply
+				return otto.FalseValue()
 			}
 
 			suitableFiles, err := fsh.FileSystemAbstraction.Glob(filepath.Join(rrootPath, regexFilename))
 			if err != nil {
 				g.raiseError(err)
-				reply, _ := vm.ToValue(false)
-				return reply
+				return otto.FalseValue()
 			}
 
 			fileList := []string{}
@@ -370,8 +354,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		regex, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		userSortMode, err := call.Argument(1).ToString()
@@ -390,8 +373,8 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		//Rewrite and validate the sort mode
 		if userSortMode == "user" {
 			//Use user sorting mode.
-			if g.Option.UserHandler.GetDatabase().KeyExists("fs-sortpref", u.Username+"/"+filepath.ToSlash(vrootPath)) {
-				g.Option.UserHandler.GetDatabase().Read("fs-sortpref", u.Username+"/"+filepath.ToSlash(vrootPath), &userSortMode)
+			if g.Option.UserHandler.GetDatabase().KeyExists("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vrootPath))) {
+				g.Option.UserHandler.GetDatabase().Read("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vrootPath)), &userSortMode)
 			} else {
 				userSortMode = "default"
 			}
@@ -406,16 +389,14 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, err := u.GetFileSystemHandlerFromVirtualPath(vrootPath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		fshAbs := fsh.FileSystemAbstraction
 		rrootPath, _ := fshAbs.VirtualPathToRealPath(vrootPath, u.Username)
 		suitableFiles, err := fshAbs.Glob(filepath.Join(rrootPath, regexFilename))
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		fileList := []string{}
@@ -450,8 +431,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -459,25 +439,42 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
 		}
 
+		userSortMode, err := call.Argument(1).ToString()
+		if err != nil || userSortMode == "" || userSortMode == "undefined" {
+			userSortMode = "default"
+		}
+
+		//Rewrite and validate the sort mode
+		if userSortMode == "user" {
+			//Use user sorting mode.
+			if g.Option.UserHandler.GetDatabase().KeyExists("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vpath))) {
+				g.Option.UserHandler.GetDatabase().Read("fs-sortpref", u.Username+"/"+filepath.ToSlash(filepath.Clean(vpath)), &userSortMode)
+			} else {
+				userSortMode = "default"
+			}
+		}
+
+		if !fssort.SortModeIsSupported(userSortMode) {
+			log.Println("[AGI] Sort mode: " + userSortMode + " not supported. Using default")
+			userSortMode = "default"
+		}
+
 		fsh, err := u.GetFileSystemHandlerFromVirtualPath(vpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		fshAbs := fsh.FileSystemAbstraction
 		rpath, err := fshAbs.VirtualPathToRealPath(vpath, u.Username)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		dirEntry, err := fshAbs.ReadDir(rpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		type fileInfo struct {
@@ -489,6 +486,32 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 			IsDir    bool
 		}
 
+		//Sort the dirEntry by file info, a bit slow :(
+		entries := map[string]fs.DirEntry{}
+		fnames := []string{}
+		fis := []fs.FileInfo{}
+		if userSortMode != "default" {
+			//Prepare the data structure for sorting
+			for _, de := range dirEntry {
+				fnames = append(fnames, de.Name())
+				fstat, _ := de.Info()
+				fis = append(fis, fstat)
+				thisFsDirEntry := de
+				entries[de.Name()] = thisFsDirEntry
+			}
+
+			//Sort it
+			sortedNameList := fssort.SortFileList(fnames, fis, userSortMode)
+
+			//Update dirEntry sequence
+			newDirEntry := []fs.DirEntry{}
+			for _, key := range sortedNameList {
+				newDirEntry = append(newDirEntry, entries[key])
+			}
+
+			dirEntry = newDirEntry
+		}
+
 		results := []fileInfo{}
 		for _, de := range dirEntry {
 			isHidden, _ := hidden.IsHidden(de.Name(), false)
@@ -519,8 +542,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -531,23 +553,20 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, err := u.GetFileSystemHandlerFromVirtualPath(vpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		fshAbs := fsh.FileSystemAbstraction
 		rpath, err := fshAbs.VirtualPathToRealPath(vpath, u.Username)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Get filesize of file
 		rawsize := fshAbs.GetFileSize(rpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		reply, _ := vm.ToValue(rawsize)
@@ -559,8 +578,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -571,23 +589,19 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, err := u.GetFileSystemHandlerFromVirtualPath(vpath)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 		fshAbs := fsh.FileSystemAbstraction
 		rpath, err := fshAbs.VirtualPathToRealPath(vpath, u.Username)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		if fshAbs.FileExists(rpath) {
-			reply, _ := vm.ToValue(true)
-			return reply
+			return otto.TrueValue()
 		} else {
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 	})
 
@@ -596,8 +610,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -609,8 +622,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		fsh, rpath, err := virtualPathToRealPath(vpath, u)
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		if _, err := fsh.FileSystemAbstraction.Stat(rpath); os.IsNotExist(err) {
@@ -619,11 +631,9 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		}
 
 		if fsh.FileSystemAbstraction.IsDir(rpath) {
-			reply, _ := vm.ToValue(true)
-			return reply
+			return otto.TrueValue()
 		} else {
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 	})
 
@@ -658,8 +668,45 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 
 	//Get MD5 of the given filepath, not implemented
 	vm.Set("_filelib_md5", func(call otto.FunctionCall) otto.Value {
-		log.Println("Call to MD5 Functions!")
-		return otto.FalseValue()
+		vpath, err := call.Argument(0).ToString()
+		if err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+
+		//Check for permission
+		if !u.CanRead(vpath) {
+			panic(vm.MakeCustomError("PermissionDenied", "Path access denied"))
+		}
+
+		fsh, err := u.GetFileSystemHandlerFromVirtualPath(vpath)
+		if err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+		fshAbs := fsh.FileSystemAbstraction
+		rpath, err := fshAbs.VirtualPathToRealPath(vpath, u.Username)
+		if err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+
+		f, err := fshAbs.ReadStream(rpath)
+		if err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+
+		defer f.Close()
+		h := md5.New()
+		if _, err := io.Copy(h, f); err != nil {
+			g.raiseError(err)
+			return otto.FalseValue()
+		}
+
+		md5Sum := hex.EncodeToString(h.Sum(nil))
+		result, _ := vm.ToValue(md5Sum)
+		return result
 	})
 
 	//Get the root name of the given virtual path root
@@ -688,8 +735,7 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		vpath, err := call.Argument(0).ToString()
 		if err != nil {
 			g.raiseError(err)
-			reply, _ := vm.ToValue(false)
-			return reply
+			return otto.FalseValue()
 		}
 
 		//Check for permission
@@ -743,8 +789,8 @@ func (g *Gateway) injectFileLibFunctions(vm *otto.Otto, u *user.User) {
 		filelib.mtime = _filelib_mtime;
 		filelib.rootName = _filelib_rname;
 
-		filelib.readdir = function(path){
-			var s = _filelib_readdir(path);
+		filelib.readdir = function(path, sortmode){
+			var s = _filelib_readdir(path, sortmode);
 			return JSON.parse(s);
 		};
 	`)

+ 1 - 1
web/Music/functions/getMeta.js

@@ -53,7 +53,7 @@ if (requirelib("filelib") == true){
         }
     }
     */
-    var nearbyFiles = filelib.readdir(dirname);
+    var nearbyFiles = filelib.readdir(dirname, "user");
     var audioFiles = [];
     var supportedFormats = [".mp3",".flac",".wav",".ogg",".aac",".webm",".mp4"];
     //For each nearby files

Diff do ficheiro suprimidas por serem muito extensas
+ 27 - 0
web/img/system/commenting.ai


+ 13 - 0
web/img/system/commenting.svg

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="256px" height="128px" viewBox="0 0 256 128" enable-background="new 0 0 256 128" xml:space="preserve">
+<path fill="#E8E6EC" d="M170.5,12h-72c-22.644,0-41,19.176-41,42.831v2.089c0,5.953,1.165,11.621,3.266,16.772
+	c-4.925,2.695-8.266,7.924-8.266,13.934c0,8.768,7.107,15.875,15.875,15.875c5.672,0,10.635-2.983,13.442-7.458
+	c5.098,2.376,10.74,3.708,16.683,3.708h72c22.644,0,41-19.176,41-42.831v-2.089C211.5,31.176,193.144,12,170.5,12z"/>
+<circle fill="#E8E6EC" cx="46.5" cy="110.75" r="9.5"/>
+<circle fill="#AAA8AE" cx="105.25" cy="55.083" r="11.583"/>
+<circle fill="#AAA8AE" cx="163.583" cy="55.084" r="11.583"/>
+<circle fill="#C5C4CA" cx="134.417" cy="55.083" r="11.583"/>
+</svg>

Alguns ficheiros não foram mostrados porque muitos ficheiros mudaram neste diff