浏览代码

Fixed file operation progress bug

TC pushbot 5 4 年之前
父节点
当前提交
b4305004da
共有 2 个文件被更改,包括 40 次插入23 次删除
  1. 28 2
      file_system.go
  2. 12 21
      mod/filesystem/fileOpr.go

+ 28 - 2
file_system.go

@@ -1236,7 +1236,7 @@ func system_fs_handleWebSocketOpr(w http.ResponseWriter, r *http.Request) {
 			}
 
 			if operation == "move" {
-				fs.FileMove(rsrcFile, rdestFile, existsOpr, false, func(progress int, currentFile string) {
+				err := fs.FileMove(rsrcFile, rdestFile, existsOpr, false, func(progress int, currentFile string) {
 					//Multply child progress to parent progress
 					blockRatio := float64(100) / float64(len(sourceFiles))
 					overallRatio := blockRatio*float64(i) + blockRatio*(float64(progress)/float64(100))
@@ -1251,8 +1251,21 @@ func system_fs_handleWebSocketOpr(w http.ResponseWriter, r *http.Request) {
 					js, _ := json.Marshal(currentStatus)
 					c.WriteMessage(1, js)
 				})
+
+				//Handle move starting error
+				if err != nil {
+					stopStatus := ProgressUpdate{
+						LatestFile: filepath.Base(rsrcFile),
+						Progress:   -1,
+						Error:      err.Error(),
+					}
+					js, _ := json.Marshal(stopStatus)
+					c.WriteMessage(1, js)
+					c.Close()
+					return
+				}
 			} else if operation == "copy" {
-				fs.FileCopy(rsrcFile, rdestFile, existsOpr, func(progress int, currentFile string) {
+				err := fs.FileCopy(rsrcFile, rdestFile, existsOpr, func(progress int, currentFile string) {
 					//Multply child progress to parent progress
 					blockRatio := float64(100) / float64(len(sourceFiles))
 					overallRatio := blockRatio*float64(i) + blockRatio*(float64(progress)/float64(100))
@@ -1267,6 +1280,19 @@ func system_fs_handleWebSocketOpr(w http.ResponseWriter, r *http.Request) {
 					js, _ := json.Marshal(currentStatus)
 					c.WriteMessage(1, js)
 				})
+
+				//Handle Copy starting error
+				if err != nil {
+					stopStatus := ProgressUpdate{
+						LatestFile: filepath.Base(rsrcFile),
+						Progress:   -1,
+						Error:      err.Error(),
+					}
+					js, _ := json.Marshal(stopStatus)
+					c.WriteMessage(1, js)
+					c.Close()
+					return
+				}
 			}
 		}
 	}

+ 12 - 21
mod/filesystem/fileOpr.go

@@ -371,10 +371,9 @@ func ViewZipFile(filepath string) ([]string, error) {
 func FileCopy(src string, dest string, mode string, progressUpdate func(int, string)) error {
 	srcRealpath, _ := filepath.Abs(src)
 	destRealpath, _ := filepath.Abs(dest)
-	if IsDir(src) && strings.Contains(destRealpath, srcRealpath) {
+	if IsDir(src) && strings.Contains(filepath.ToSlash(destRealpath)+"/", filepath.ToSlash(srcRealpath)+"/") {
 		//Recursive operation. Reject
 		return errors.New("Recursive copy operation.")
-
 	}
 
 	//Check if the copy destination file already have an identical file
@@ -472,7 +471,7 @@ func FileCopy(src string, dest string, mode string, progressUpdate func(int, str
 func FileMove(src string, dest string, mode string, fastMove bool, progressUpdate func(int, string)) error {
 	srcRealpath, _ := filepath.Abs(src)
 	destRealpath, _ := filepath.Abs(dest)
-	if IsDir(src) && strings.Contains(destRealpath, srcRealpath) {
+	if IsDir(src) && strings.Contains(filepath.ToSlash(destRealpath)+"/", filepath.ToSlash(srcRealpath)+"/") {
 		//Recursive operation. Reject
 		return errors.New("Recursive move operation.")
 	}
@@ -557,20 +556,8 @@ func FileMove(src string, dest string, mode string, fastMove bool, progressUpdat
 			//Source is file only. Copy file.
 			realDest := dest + movedFilename
 			/*
-				//Updates 20-10-2020, replaced io.Copy to BufferedLargeFileCopy
-				source, err := os.Open(src)
-				if err != nil {
-					return err
-				}
-
-				destination, err := os.Create(realDest)
-				if err != nil {
-					return err
-				}
-
-				io.Copy(destination, source)
-				source.Close()
-				destination.Close()
+				Updates 20-10-2020, replaced io.Copy to BufferedLargeFileCopy
+				Legacy code removed.
 			*/
 
 			//Update the progress
@@ -611,11 +598,14 @@ func CopyDir(src string, dest string) error {
 
 //Replacment of the legacy dirCopy plugin with filepath.Walk function. Allowing real time progress update to front end
 func dirCopy(src string, realDest string, progressUpdate func(int, string)) error {
+
 	//Get the total file counts
-	totalFileCounts := 0
+	totalFileCounts := int64(0)
 	filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
 		if !info.IsDir() {
-			totalFileCounts++
+			//Updates 22 April 2021, chnaged from file count to file size for progress update
+			//totalFileCounts++
+			totalFileCounts += info.Size()
 		}
 		return nil
 	})
@@ -626,7 +616,7 @@ func dirCopy(src string, realDest string, progressUpdate func(int, string)) erro
 	}
 
 	//Start moving
-	fileCounter := 0
+	fileCounter := int64(0)
 
 	err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
 		srcAbs, _ := filepath.Abs(src)
@@ -641,7 +631,8 @@ func dirCopy(src string, realDest string, progressUpdate func(int, string)) erro
 			//Mkdir base on relative path
 			return os.MkdirAll(filepath.Join(realDest, folderRootRelative), 0755)
 		} else {
-			fileCounter++
+			//fileCounter++
+			fileCounter += info.Size()
 			//Move file base on relative path
 			fileSrc := filepath.ToSlash(filepath.Join(filepath.Clean(src), folderRootRelative))
 			fileDest := filepath.ToSlash(filepath.Join(filepath.Clean(realDest), folderRootRelative))