Browse Source

auto-push

Toby Chui 6 years ago
parent
commit
12e778f5e4
2 changed files with 160 additions and 37 deletions
  1. BIN
      fsconv.exe
  2. 160 37
      main.go

BIN
fsconv.exe


+ 160 - 37
main.go

@@ -143,13 +143,10 @@ func (a ByLen) Less(i, j int) bool {
 func (a ByLen) Swap(i, j int) {
    a[i], a[j] = a[j], a[i]
 }
- 
 
-func main() {
-	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
-	if len(os.Args) < 2 {
-		//Default options, usually double click will start this process
-		folders, files := scan_recursive(dir, []string{os.Args[0]})
+func recursiveUTF8Conv(dir string, rf bool, rd bool){
+	folders, files := scan_recursive(dir, []string{os.Args[0]})
+	if (rf){
 		for i := 0; i < len(files); i++ {
 			thisFilepath := strings.ReplaceAll(files[i], "\\", "/")
 			filename := path.Base(thisFilepath)
@@ -168,7 +165,9 @@ func main() {
 			}
 			
 		}
-		
+	}
+
+	if (rd){
 		//Sort the array of folders by string length, hence deeper folder will be renamed first
 		sort.Sort(ByLen(folders))
 		applicationStartupPath, _ := Realpath(path.Dir(os.Args[0]))
@@ -186,19 +185,13 @@ func main() {
 			}
 			
 		}
+	}
 
+}
 
-	}else if (len(os.Args) == 2 && os.Args[1] == "help"){
-		//Show help message
-		fmt.Println("ArOZ Online UM File Naming Method Converter")
-		fmt.Println("Usage: \n./fsconv						Convert all files under current directory and its sub-directories into UTF-8 file naming method.")
-		fmt.Println("./fsconv -um 						Convert all files under current directory and its sub-directories into UM File Naming Method representation.")
-		fmt.Println("Use ./fsconv -h for showing all usable flags for defined file / folder conversion")
-
-
-	}else if (len(os.Args) == 2 && os.Args[1] == "-um"){
-		//Convert all files and folder into um filenaming methods
-		folders, files := scan_recursive(dir, []string{os.Args[0]})
+func recursiveUMFNConv(dir string,rf bool, rd bool){
+	folders, files := scan_recursive(dir, []string{os.Args[0]})
+	if (rf){
 		for i := 0; i < len(files); i++ {
 			thisFilepath := strings.ReplaceAll(files[i], "\\", "/")
 			filename := path.Base(thisFilepath)
@@ -214,9 +207,10 @@ func main() {
 					panic(err)
 				}
 			}
-			
 		}
-		
+	}
+	
+	if (rd){
 		//Sort the array of folders by string length, hence deeper folder will be renamed first
 		sort.Sort(ByLen(folders))
 		applicationStartupPath, _ := Realpath(path.Dir(os.Args[0]))
@@ -233,26 +227,26 @@ func main() {
 			}
 			
 		}
-	} else {
-		//Given target directory with flags
-		var recursive = flag.Bool("r",false,"Enable recursive filename translation.")
-		var inputFile = flag.String("i","","Input filename.")
-		var defaultFormat = flag.Bool("utf",false,"Convert to standard UTF-8 filename format")
-		var umFormat = flag.Bool("um",false,"Convert to UMfilename format")
-		var includeFiles = flag.Bool("f",false,"Include all files within input directory, default false")
-		var includeFolders = flag.Bool("d",false,"Include all directories within input directory, default false")
-		flag.Parse()
-		fmt.Println("r:", *recursive)
-		fmt.Println("i:", *inputFile)
-		fmt.Println("utf:", *defaultFormat)
-		fmt.Println("um:", *umFormat)
-		fmt.Println("f:", *includeFiles)
-		fmt.Println("d:", *includeFolders)
-
 	}
+	
 }
 
-
+func isDir(path string) bool{
+    fi, err := os.Stat(path)
+    if err != nil {
+        fmt.Println(err)
+        return false
+    }
+    switch mode := fi.Mode(); {
+    case mode.IsDir():
+        // do directory stuff
+        return true
+    case mode.IsRegular():
+        // do file stuff
+        return false
+	}
+	return false
+}
 
 func getSafeFilename(s string) string{
 	replacer := strings.NewReplacer("/", "", "\\","", "@","-", "&","-", "*","-", "<","-", ">","-", "|","-", "?","-", ":","-")
@@ -373,3 +367,132 @@ func nextComponent(path []byte, start int) []byte {
 	}
 	return path[0 : start+v]
 }
+
+func main() {
+	dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
+	if len(os.Args) < 2 {
+		//Default options, usually double click will start this process
+		recursiveUTF8Conv(dir,true,true)
+
+	}else if (len(os.Args) == 2 && os.Args[1] == "help"){
+		//Show help message
+		fmt.Println("ArOZ Online UM File Naming Method Converter")
+		fmt.Println("Usage: \n./fsconv						Convert all files under current directory and its sub-directories into UTF-8 file naming method.")
+		fmt.Println("./fsconv -um 						Convert all files under current directory and its sub-directories into UM File Naming Method representation.")
+		fmt.Println("Use ./fsconv -h for showing all usable flags for defined file / folder conversion")
+
+	}else if (len(os.Args) == 2 && os.Args[1] == "-um"){
+		//Convert all files and folder into um filenaming methods
+		recursiveUMFNConv(dir,true,true);
+
+	} else {
+		//Given target directory with flags
+		var recursive = flag.Bool("r",false,"Enable recursive filename translation.")
+		var inputFile = flag.String("i","","Input filename.")
+		var defaultFormat = flag.Bool("utf",false,"Convert to standard UTF-8 filename format")
+		var umFormat = flag.Bool("um",false,"Convert to UMfilename format")
+		var recursiveFileOnly = flag.Bool("rf",false,"Recursive rename file only.")
+		var recursiveDirOnly = flag.Bool("rd",false,"Recursive rename directory only.")
+		flag.Parse()
+
+		//Check if the input file exists.
+		if !file_exists(*inputFile){
+			fmt.Println("ERROR. Input file not exists. Given: ", *inputFile);
+			os.Exit(0);
+		}
+
+		//Check if setting logic correct
+		if (*defaultFormat == *umFormat){
+			//If not defined defaultFormat or umFormat, assume default Format
+			*defaultFormat = true
+		}
+
+
+		fmt.Println("r:", *recursive)
+		fmt.Println("i:", *inputFile)
+		fmt.Println("utf:", *defaultFormat)
+		fmt.Println("um:", *umFormat)
+		fmt.Println("rf:", *recursiveFileOnly)
+		fmt.Println("rd:", *recursiveDirOnly)
+
+		//Perform translation process
+		if (*recursive|| *recursiveFileOnly || *recursiveDirOnly){
+			//Recursive mode
+			if (*defaultFormat == true){
+				//Convert everything in given directory into standard filename
+				if (*recursiveFileOnly){
+					recursiveUTF8Conv(*inputFile,true,false)
+				}else if (*recursiveDirOnly){
+					recursiveUTF8Conv(*inputFile,false,true)
+				}else{
+					recursiveUTF8Conv(*inputFile,true,true)
+				}
+				
+			}else{
+				//Convert everything in give ndirectory in umfilename
+				if (*recursiveFileOnly){
+					recursiveUMFNConv(*inputFile,true,false)
+				}else if (*recursiveDirOnly){
+					recursiveUMFNConv(*inputFile,false,true)
+				}else{
+					recursiveUMFNConv(*inputFile,true,true)
+				}
+			}
+		}else{
+			if (isDir(*inputFile)){
+				//A folder
+				if (*defaultFormat == true){
+					if (isHex(filepath.Base(*inputFile))){
+						//This folder use hex foldername. Convert it to UTF8 filename
+						convFoldername, _ := hex2bin(filepath.Base(*inputFile));
+						safeName := getSafeFilename(string(convFoldername))
+						fmt.Println(filepath.Base(*inputFile) + " -> " + string(safeName))
+						err := os.Rename(*inputFile,filepath.Dir(*inputFile) + "/" + string(safeName))
+						if (err != nil){
+							panic(err)
+						}
+					}
+				}else{
+					convFoldername := bin2hex(filepath.Base(*inputFile));
+					fmt.Println(filepath.Base(*inputFile) + " -> " + string(convFoldername))
+					os.Rename(*inputFile,filepath.Dir(*inputFile) + "/" + string(convFoldername))
+				}
+			}else{
+				//A file
+				if (*defaultFormat == true){
+					thisFilepath := strings.ReplaceAll(*inputFile, "\\", "/")
+					filename := path.Base(thisFilepath)
+					extension := filepath.Ext(filename)
+					name := filename[0 : len(filename)-len(extension)]
+					//Check if it has the inith prefix
+					if len(name) > 5 && name[0:5] == "inith"{
+						//This is hex filename. Translate its name to normal filename
+						originalName,_ := hex2bin(name[5:])
+						safeName := getSafeFilename(string(originalName))
+						fmt.Println(filename + " -> " + safeName + extension)
+						err := os.Rename(*inputFile, path.Dir(thisFilepath) + "/" + safeName + extension)
+						if (err != nil){
+							panic(err)
+						}
+					}
+				}else{
+					thisFilepath := strings.ReplaceAll(*inputFile, "\\", "/")
+					filename := path.Base(thisFilepath)
+					extension := filepath.Ext(filename)
+					name := filename[0 : len(filename)-len(extension)]
+					//Check if it has the inith prefix
+					if len(name) > 5 && name[0:5] != "inith"{
+						//This is not hex filename. Translate its name to um-filename method
+						umfilename := "inith" + bin2hex(name)
+						fmt.Println(filename + " -> " + umfilename + extension)
+						err := os.Rename(*inputFile, path.Dir(thisFilepath) + "/" + umfilename + extension)
+						if (err != nil){
+							panic(err)
+						}
+					}
+				}
+			}
+		}
+
+	}
+}