Browse Source

Added custom ToSlash and Base function for testing

Toby Chui 3 years ago
parent
commit
9824953def
2 changed files with 23 additions and 5 deletions
  1. 18 0
      mod/filesystem/arozfs/arozfs.go
  2. 5 5
      mod/filesystem/fileOpr.go

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

@@ -10,6 +10,7 @@ import (
 	"errors"
 	"errors"
 	"io"
 	"io"
 	"io/fs"
 	"io/fs"
+	"strings"
 )
 )
 
 
 type File interface {
 type File interface {
@@ -78,3 +79,20 @@ func IsNetworkDrive(fstype string) bool {
 func GetSupportedFileSystemTypes() []string {
 func GetSupportedFileSystemTypes() []string {
 	return []string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs", "webdav", "ftp", "smb"}
 	return []string{"ext4", "ext2", "ext3", "fat", "vfat", "ntfs", "webdav", "ftp", "smb"}
 }
 }
+
+func ToSlash(filename string) string {
+	return strings.ReplaceAll(filename, "\\", "/")
+}
+
+func Base(filename string) string {
+	filename = ToSlash(filename)
+	if filename == "" || filename == "/" {
+		return filename
+	}
+	c := strings.Split(filename, "/")
+	if len(c) == 1 {
+		return c[0]
+	} else {
+		return c[len(c)-1]
+	}
+}

+ 5 - 5
mod/filesystem/fileOpr.go

@@ -494,9 +494,9 @@ func FileCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 	}
 	}
 
 
 	//Check if the copy destination file already have an identical file
 	//Check if the copy destination file already have an identical file
-	copiedFilename := filepath.Base(src)
+	copiedFilename := arozfs.Base(src)
 
 
-	if destFshAbs.FileExists(filepath.Join(dest, filepath.Base(src))) {
+	if destFshAbs.FileExists(filepath.Join(dest, arozfs.Base(src))) {
 		if mode == "" {
 		if mode == "" {
 			//Do not specific file exists principle
 			//Do not specific file exists principle
 			return errors.New("Destination file already exists.")
 			return errors.New("Destination file already exists.")
@@ -507,7 +507,7 @@ func FileCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 		} else if mode == "overwrite" {
 		} else if mode == "overwrite" {
 			//Continue with the following code
 			//Continue with the following code
 			//Check if the copy and paste dest are identical
 			//Check if the copy and paste dest are identical
-			if filepath.ToSlash(filepath.Clean(src)) == filepath.ToSlash(filepath.Clean(filepath.Join(dest, filepath.Base(src)))) {
+			if filepath.ToSlash(filepath.Clean(src)) == filepath.ToSlash(filepath.Clean(filepath.Join(dest, arozfs.Base(src)))) {
 				//Source and target identical. Cannot overwrite.
 				//Source and target identical. Cannot overwrite.
 				return errors.New("Source and destination paths are identical.")
 				return errors.New("Source and destination paths are identical.")
 
 
@@ -515,12 +515,12 @@ func FileCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 
 
 		} else if mode == "keep" {
 		} else if mode == "keep" {
 			//Keep the file but saved with 'Copy' suffix
 			//Keep the file but saved with 'Copy' suffix
-			newFilename := strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + " - Copy" + filepath.Ext(src)
+			newFilename := strings.TrimSuffix(arozfs.Base(src), filepath.Ext(src)) + " - Copy" + filepath.Ext(src)
 			//Check if the newFilename already exists. If yes, continue adding suffix
 			//Check if the newFilename already exists. If yes, continue adding suffix
 			duplicateCounter := 0
 			duplicateCounter := 0
 			for destFshAbs.FileExists(filepath.Join(dest, newFilename)) {
 			for destFshAbs.FileExists(filepath.Join(dest, newFilename)) {
 				duplicateCounter++
 				duplicateCounter++
-				newFilename = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + " - Copy(" + strconv.Itoa(duplicateCounter) + ")" + filepath.Ext(src)
+				newFilename = strings.TrimSuffix(arozfs.Base(src), filepath.Ext(src)) + " - Copy(" + strconv.Itoa(duplicateCounter) + ")" + filepath.Ext(src)
 				if duplicateCounter > 1024 {
 				if duplicateCounter > 1024 {
 					//Maxmium loop encountered. For thread safty, terminate here
 					//Maxmium loop encountered. For thread safty, terminate here
 					return errors.New("Too many copies of identical files.")
 					return errors.New("Too many copies of identical files.")