Browse Source

Fixed localversion tool

tobychui 3 years ago
parent
commit
bfeba567c8

+ 8 - 7
file_system.go

@@ -2742,7 +2742,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 	if opr == "" {
 		//List file history
 
-		fileVersionData, err := localversion.GetFileVersionData(rpath)
+		fileVersionData, err := localversion.GetFileVersionData(fsh, rpath)
 		if err != nil {
 			common.SendErrorResponse(w, "Unable to load version information: "+err.Error())
 			return
@@ -2759,7 +2759,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
-		err = localversion.RemoveFileHistory(rpath, historyID)
+		err = localversion.RemoveFileHistory(fsh, rpath, historyID)
 		if err != nil {
 			common.SendErrorResponse(w, err.Error())
 			return
@@ -2768,7 +2768,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 		common.SendOK(w)
 	} else if opr == "deleteAll" {
 		//Delete all file history of given vpath
-		err = localversion.RemoveAllRelatedFileHistory(rpath)
+		err = localversion.RemoveAllRelatedFileHistory(fsh, rpath)
 		if err != nil {
 			common.SendErrorResponse(w, err.Error())
 			return
@@ -2783,7 +2783,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 			common.SendErrorResponse(w, "Invalid history id given")
 			return
 		}
-		err = localversion.RestoreFileHistory(rpath, historyID)
+		err = localversion.RestoreFileHistory(fsh, rpath, historyID)
 		if err != nil {
 			common.SendErrorResponse(w, err.Error())
 			return
@@ -2792,7 +2792,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 		common.SendOK(w)
 	} else if opr == "new" {
 		//Create a new snapshot of this file
-		err = localversion.CreateFileSnapshot(rpath)
+		err = localversion.CreateFileSnapshot(fsh, rpath)
 		if err != nil {
 			common.SendErrorResponse(w, err.Error())
 			return
@@ -2808,7 +2808,7 @@ func system_fs_FileVersionHistory(w http.ResponseWriter, r *http.Request) {
 func system_fs_clearVersionHistories() {
 	for _, fsh := range fsHandlers {
 		if !fsh.ReadOnly {
-			localversion.CleanExpiredVersionBackups(fsh.Path, 30*86400)
+			localversion.CleanExpiredVersionBackups(fsh, fsh.Path, 30*86400)
 		}
 
 	}
@@ -3170,6 +3170,7 @@ func bufferRemoteFileToLocal(targetFsh *filesystem.FileSystemHandler, rpath stri
 		log.Println("[File System] Buffer from remote to local failed: ", err.Error())
 		return "", err
 	}
+	defer src.Close()
 
 	dest, err := os.OpenFile(newBufferFilename, os.O_CREATE|os.O_WRONLY, 0775)
 	if err != nil {
@@ -3178,7 +3179,7 @@ func bufferRemoteFileToLocal(targetFsh *filesystem.FileSystemHandler, rpath stri
 	}
 	io.Copy(dest, src)
 	dest.Close()
-	src.Close()
+
 	return newBufferFilename, nil
 }
 

+ 2 - 0
mediaServer.go

@@ -193,6 +193,7 @@ func serverMedia(w http.ResponseWriter, r *http.Request) {
 				return
 			}
 			io.Copy(w, remoteStream)
+			remoteStream.Close()
 			return
 		} else {
 			http.ServeFile(w, r, escapedRealFilepath)
@@ -207,6 +208,7 @@ func serverMedia(w http.ResponseWriter, r *http.Request) {
 				return
 			}
 			io.Copy(w, remoteStream)
+			remoteStream.Close()
 			return
 		}
 		http.ServeFile(w, r, realFilepath)

+ 1 - 1
mod/agi/agi.go

@@ -421,12 +421,12 @@ func (g *Gateway) bufferRemoteResourcesToLocal(fsh *filesystem.FileSystemHandler
 	if err != nil {
 		return "", nil, err
 	}
+	defer f.Close()
 	dest, err := os.OpenFile(buffFile, os.O_CREATE|os.O_RDWR, 0775)
 	if err != nil {
 		return "", nil, err
 	}
 	io.Copy(dest, f)
-	f.Close()
 	dest.Close()
 	return buffFile, func() {
 		closerFunc()

+ 2 - 35
mod/agi/userFunc.go

@@ -43,49 +43,16 @@ func (g *Gateway) injectUserFunctions(vm *otto.Otto, scriptFile string, scriptSc
 	//File system and path related
 	vm.Set("decodeVirtualPath", func(call otto.FunctionCall) otto.Value {
 		log.Println("Call to deprecated function decodeVirtualPath")
-		path, _ := call.Argument(0).ToString()
-		_, realpath, err := virtualPathToRealPath(path, u)
-		if err != nil {
-			reply, _ := vm.ToValue(false)
-			return reply
-		} else {
-			reply, _ := vm.ToValue(realpath)
-			return reply
-		}
+		return otto.FalseValue()
 	})
 
 	vm.Set("decodeAbsoluteVirtualPath", func(call otto.FunctionCall) otto.Value {
 		log.Println("Call to deprecated function decodeAbsoluteVirtualPath")
-		path, _ := call.Argument(0).ToString()
-		_, realpath, err := virtualPathToRealPath(path, u)
-		if err != nil {
-			reply, _ := vm.ToValue(false)
-			return reply
-		} else {
-			//Convert the real path to absolute path
-			abspath, err := filepath.Abs(realpath)
-			if err != nil {
-				reply, _ := vm.ToValue(false)
-				return reply
-			}
-			reply, _ := vm.ToValue(abspath)
-			return reply
-		}
+		return otto.FalseValue()
 	})
 
 	vm.Set("encodeRealPath", func(call otto.FunctionCall) otto.Value {
 		log.Println("Call to deprecated function encodeRealPath")
-		/*
-			path, _ := call.Argument(0).ToString()
-			_, realpath, err := realpathToVirtualpath(path, u)
-			if err != nil {
-				reply, _ := vm.ToValue(false)
-				return reply
-			} else {
-				reply, _ := vm.ToValue(realpath)
-				return reply
-			}
-		*/
 		return otto.FalseValue()
 	})
 

+ 4 - 1
mod/filesystem/fileOpr.go

@@ -442,7 +442,7 @@ func FileCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 		if err != nil {
 			return err
 		}
-
+		defer f.Close()
 		err = destFshAbs.WriteStream(realDest, f, 0775)
 		if err != nil {
 			return err
@@ -558,6 +558,7 @@ func FileMove(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 			fmt.Println(err)
 			return err
 		}
+		defer f.Close()
 
 		err = destAbst.WriteStream(realDest, f, 0755)
 		if err != nil {
@@ -640,12 +641,14 @@ func dirCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 				fmt.Println(err)
 				return err
 			}
+			defer f.Close()
 
 			err = destFshAbs.WriteStream(fileDest, f, 0755)
 			if err != nil {
 				fmt.Println(err)
 				return err
 			}
+
 		}
 		return nil
 	})

+ 61 - 34
mod/filesystem/localversion/localversion.go

@@ -34,8 +34,9 @@ type VersionList struct {
 	Versions      []*FileSnapshot
 }
 
-func GetFileVersionData(realFilepath string) (*VersionList, error) {
-	mtime, _ := filesystem.GetModTime(realFilepath)
+func GetFileVersionData(fsh *filesystem.FileSystemHandler, realFilepath string) (*VersionList, error) {
+	fshAbs := fsh.FileSystemAbstraction
+	mtime, _ := fshAbs.GetModTime(realFilepath)
 	versionList := VersionList{
 		CurrentFile:   filepath.Base(realFilepath),
 		LatestModtime: mtime,
@@ -43,7 +44,7 @@ func GetFileVersionData(realFilepath string) (*VersionList, error) {
 	}
 	//Example folder structure: ./.localver/{date_time}/{file}
 	expectedVersionFiles := filepath.Join(filepath.Dir(realFilepath), ".metadata/.localver", "*", filepath.Base(realFilepath))
-	versions, err := filepath.Glob(filepath.ToSlash(expectedVersionFiles))
+	versions, err := fshAbs.Glob(filepath.ToSlash(expectedVersionFiles))
 	if err != nil {
 		return &versionList, err
 	}
@@ -53,14 +54,14 @@ func GetFileVersionData(realFilepath string) (*VersionList, error) {
 
 	for _, version := range versions {
 		historyID := filepath.Base(filepath.Dir(version))
-		mtime, _ := filesystem.GetModTime(version)
+		mtime, _ := fshAbs.GetModTime(version)
 		overwriteDisplayTime := strings.ReplaceAll(strings.Replace(strings.Replace(historyID, "-", "/", 2), "-", ":", 2), "_", " ")
 		versionList.Versions = append(versionList.Versions, &FileSnapshot{
 			HistoryID:     historyID,
 			Filename:      filepath.Base(version),
 			ModTime:       mtime,
 			OverwriteTime: overwriteDisplayTime,
-			Filesize:      filesystem.GetFileSize(version),
+			Filesize:      fshAbs.GetFileSize(version),
 			Relpath:       ".metadata/.localver/" + historyID + "/" + filepath.Base(version),
 		})
 	}
@@ -68,31 +69,43 @@ func GetFileVersionData(realFilepath string) (*VersionList, error) {
 	return &versionList, nil
 }
 
-func RestoreFileHistory(originalFilepath string, histroyID string) error {
+func RestoreFileHistory(fsh *filesystem.FileSystemHandler, originalFilepath string, histroyID string) error {
+	fshAbs := fsh.FileSystemAbstraction
 	expectedVersionFile := filepath.Join(filepath.Dir(originalFilepath), ".metadata/.localver", filepath.Base(histroyID), filepath.Base(originalFilepath))
-	if !filesystem.FileExists(expectedVersionFile) {
+	if !fshAbs.FileExists(expectedVersionFile) {
 		return errors.New("File version not exists")
 	}
 
 	//Restore it
-	os.Rename(originalFilepath, originalFilepath+".backup")
-	filesystem.BasicFileCopy(expectedVersionFile, originalFilepath)
+	fshAbs.Rename(originalFilepath, originalFilepath+".backup")
+	srcf, err := fshAbs.ReadStream(expectedVersionFile)
+	if err != nil {
+		return err
+	}
+
+	err = fshAbs.WriteStream(originalFilepath, srcf, 0775)
+	if err != nil {
+		srcf.Close()
+		return err
+	}
+
+	srcf.Close()
 
 	//Check if it has been restored correctly
-	versionFileHash, _ := filesystem.GetFileMD5Sum(expectedVersionFile)
-	copiedFileHash, _ := filesystem.GetFileMD5Sum(expectedVersionFile)
+	versionFileHash, _ := filesystem.GetFileMD5Sum(fsh, expectedVersionFile)
+	copiedFileHash, _ := filesystem.GetFileMD5Sum(fsh, expectedVersionFile)
 	if versionFileHash != copiedFileHash {
 		//Rollback failed. Restore backup file
-		os.Rename(originalFilepath+".backup", originalFilepath)
+		fshAbs.Rename(originalFilepath+".backup", originalFilepath)
 		return errors.New("Unable to restore file: file hash mismatch after restore")
 	}
 
 	//OK! Delete the backup file
-	os.Remove(originalFilepath + ".backup")
+	fshAbs.Remove(originalFilepath + ".backup")
 
 	//Delete all history versions that is after the restored versions
 	expectedVersionFiles := filepath.Join(filepath.Dir(originalFilepath), ".metadata/.localver", "*", filepath.Base(originalFilepath))
-	versions, err := filepath.Glob(filepath.ToSlash(expectedVersionFiles))
+	versions, err := fshAbs.Glob(filepath.ToSlash(expectedVersionFiles))
 	if err != nil {
 		return err
 	}
@@ -101,15 +114,22 @@ func RestoreFileHistory(originalFilepath string, histroyID string) error {
 	for _, version := range versions {
 		if enableRemoval {
 			//Remove this version as this is after the restored version
-			os.Remove(version)
+			fshAbs.Remove(version)
+			fileInVersion, _ := fshAbs.Glob(filepath.ToSlash(filepath.Dir(version) + "/*"))
+			if len(fileInVersion) == 0 {
+				fshAbs.RemoveAll(filepath.Dir(version))
+			}
 		} else {
 			thisHistoryId := filepath.Base(filepath.Dir(version))
 			if thisHistoryId == histroyID {
 				//Match. Tag enable Removal
 				enableRemoval = true
-
 				//Remove this version
-				os.Remove(version)
+				fshAbs.Remove(version)
+				fileInVersion, _ := fshAbs.Glob(filepath.ToSlash(filepath.Dir(version) + "/*"))
+				if len(fileInVersion) == 0 {
+					fshAbs.RemoveAll(filepath.Dir(version))
+				}
 			}
 		}
 
@@ -118,17 +138,17 @@ func RestoreFileHistory(originalFilepath string, histroyID string) error {
 	return nil
 }
 
-func RemoveFileHistory(originalFilepath string, histroyID string) error {
+func RemoveFileHistory(fsh *filesystem.FileSystemHandler, originalFilepath string, histroyID string) error {
 	expectedVersionFile := filepath.Join(filepath.Dir(originalFilepath), ".metadata/.localver", filepath.Base(histroyID), filepath.Base(originalFilepath))
-	if !filesystem.FileExists(expectedVersionFile) {
+	if !fsh.FileSystemAbstraction.FileExists(expectedVersionFile) {
 		return errors.New("File version not exists")
 	}
 
-	return os.Remove(expectedVersionFile)
+	return fsh.FileSystemAbstraction.Remove(expectedVersionFile)
 }
 
-func RemoveAllRelatedFileHistory(originalFilepath string) error {
-	expectedVersionFiles, err := filepath.Glob(filepath.Join(filepath.Dir(originalFilepath), ".metadata/.localver", "*", filepath.Base(originalFilepath)))
+func RemoveAllRelatedFileHistory(fsh *filesystem.FileSystemHandler, originalFilepath string) error {
+	expectedVersionFiles, err := fsh.FileSystemAbstraction.Glob(filepath.Join(filepath.Dir(originalFilepath), ".metadata/.localver", "*", filepath.Base(originalFilepath)))
 	if err != nil {
 		return err
 	}
@@ -138,26 +158,33 @@ func RemoveAllRelatedFileHistory(originalFilepath string) error {
 	return nil
 }
 
-func CreateFileSnapshot(realFilepath string) error {
-	if !filesystem.FileExists(realFilepath) {
+func CreateFileSnapshot(fsh *filesystem.FileSystemHandler, realFilepath string) error {
+	fshAbs := fsh.FileSystemAbstraction
+	if !fshAbs.FileExists(realFilepath) {
 		return errors.New("Source file not exists")
 	}
 	//Create the snapshot folder for this file
 	snapshotID := time.Now().Format("2006-01-02_15-04-05")
 	expectedSnapshotFolder := filepath.Join(filepath.Dir(realFilepath), ".metadata/.localver", snapshotID)
-	err := os.MkdirAll(expectedSnapshotFolder, 0775)
+	err := fshAbs.MkdirAll(expectedSnapshotFolder, 0775)
 	if err != nil {
 		return err
 	}
 	//Copy the target file to snapshot dir
 	targetVersionFilepath := filepath.Join(expectedSnapshotFolder, filepath.Base(realFilepath))
-	return filesystem.BasicFileCopy(realFilepath, targetVersionFilepath)
+	srcf, err := fshAbs.ReadStream(realFilepath)
+	if err != nil {
+		return err
+	}
+	defer srcf.Close()
+	return fshAbs.WriteStream(targetVersionFilepath, srcf, 0775)
 }
 
 //Clearn expired version backups that is older than maxReserveTime
-func CleanExpiredVersionBackups(walkRoot string, maxReserveTime int64) {
+func CleanExpiredVersionBackups(fsh *filesystem.FileSystemHandler, walkRoot string, maxReserveTime int64) {
+	fshAbs := fsh.FileSystemAbstraction
 	localVerFolders := []string{}
-	filepath.Walk(walkRoot,
+	fshAbs.Walk(walkRoot,
 		func(path string, info os.FileInfo, err error) error {
 			if err != nil {
 				//Skip this file
@@ -165,16 +192,16 @@ func CleanExpiredVersionBackups(walkRoot string, maxReserveTime int64) {
 			}
 			if !info.IsDir() && inLocalVersionFolder(path) {
 				//This is a file inside the localver folder. Check its modtime
-				mtime, _ := filesystem.GetModTime(path)
+				mtime, _ := fshAbs.GetModTime(path)
 				if time.Now().Unix()-mtime > maxReserveTime {
 					//Too old! Remove this version history
-					os.Remove(path)
+					fshAbs.Remove(path)
 				}
 
 				//Check if the folder still contains files. If not, remove it
-				files, _ := filepath.Glob(filepath.ToSlash(filepath.Dir(path)) + "/*")
+				files, _ := fshAbs.Glob(filepath.ToSlash(filepath.Dir(path)) + "/*")
 				if len(files) == 0 {
-					os.RemoveAll(filepath.Dir(path))
+					fshAbs.RemoveAll(filepath.Dir(path))
 				}
 			} else if info.IsDir() && filepath.Base(path) == ".localver" {
 				localVerFolders = append(localVerFolders, path)
@@ -185,9 +212,9 @@ func CleanExpiredVersionBackups(walkRoot string, maxReserveTime int64) {
 
 	for _, path := range localVerFolders {
 		//Check if a localver folder still contains folder. If not, delete this
-		files, _ := filepath.Glob(filepath.ToSlash(path) + "/*")
+		files, _ := fshAbs.Glob(filepath.ToSlash(path) + "/*")
 		if len(files) == 0 {
-			os.RemoveAll(path)
+			fshAbs.RemoveAll(path)
 		}
 	}
 }

+ 4 - 3
mod/filesystem/static.go

@@ -406,16 +406,17 @@ func GetFileSHA256Sum(filename string) (string, error) {
 	return hex.EncodeToString(h.Sum(nil)), nil
 }
 
-func GetFileMD5Sum(filename string) (string, error) {
-	file, err := os.Open(filename)
+func GetFileMD5Sum(fsh *FileSystemHandler, rpath string) (string, error) {
+	file, err := fsh.FileSystemAbstraction.ReadStream(rpath)
 	if err != nil {
 		return "", err
 	}
-	defer file.Close()
 
 	h := md5.New()
 	if _, err := io.Copy(h, file); err != nil {
 		return "", err
 	}
+
+	file.Close()
 	return hex.EncodeToString(h.Sum(nil)), nil
 }

+ 3 - 1
mod/share/share.go

@@ -554,15 +554,17 @@ func (s *Manager) HandleShareAccess(w http.ResponseWriter, r *http.Request) {
 								if err != nil {
 									log.Println("[Share] Buffer and zip download operation failed: ", err)
 								}
+								defer f.Close()
 								dest, err := os.OpenFile(localPath, os.O_CREATE|os.O_WRONLY, 0775)
 								if err != nil {
 									log.Println("[Share] Buffer and zip download operation failed: ", err)
 								}
+								defer dest.Close()
 								_, err = io.Copy(dest, f)
 								if err != nil {
 									log.Println("[Share] Buffer and zip download operation failed: ", err)
 								}
-								f.Close()
+
 							}
 							return nil
 						})

+ 1 - 1
web/script/applocale.js

@@ -38,7 +38,7 @@ var applocale = {
 
                 if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fontFamily != undefined){
                     //This language has a prefered font family. Inject it
-                    $("h1, h2, h3, p, span, div, a").css({
+                    $("h1, h2, h3, p, span, div, a, button").css({
                         "font-family":data.keys[applocale.lang].fontFamily
                     });
                     console.log("[Applocale] Updating font family to: ", data.keys[applocale.lang].fontFamily)