main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. ArOZ Online System - fscov File Naming Format Conversion Tool
  3. This micro-servie is designed to convert filename from and to umfilename (ArOZ Online System File Explorer Custom Format)
  4. and standard UTF-8 File System filename.
  5. Usage:
  6. ./fsconv
  7. (Convert all the files, folder and sub-directories from and to umfilename to UTF8 filename.)
  8. ./fsconv {target} {mode (um / utf)}
  9. (Convert all the files, folder and sub-directories to filename in given mode, target can be file or directory.)
  10. */
  11. package main
  12. import (
  13. "fmt"
  14. "io/ioutil"
  15. "os"
  16. "path"
  17. "path/filepath"
  18. "reflect"
  19. "strconv"
  20. "sort"
  21. "regexp"
  22. "strings"
  23. "encoding/hex"
  24. )
  25. //Commonly used functions
  26. func in_array(val interface{}, array interface{}) (exists bool, index int) {
  27. exists = false
  28. index = -1
  29. switch reflect.TypeOf(array).Kind() {
  30. case reflect.Slice:
  31. s := reflect.ValueOf(array)
  32. for i := 0; i < s.Len(); i++ {
  33. if reflect.DeepEqual(val, s.Index(i).Interface()) == true {
  34. index = i
  35. exists = true
  36. return
  37. }
  38. }
  39. }
  40. return
  41. }
  42. func file_exists(path string) bool {
  43. if _, err := os.Stat(path); os.IsNotExist(err) {
  44. return false
  45. }
  46. return true
  47. }
  48. func file_get_contents(path string) string {
  49. dat, err := ioutil.ReadFile(path)
  50. if err != nil {
  51. panic("Unable to read file: " + path)
  52. }
  53. return (string(dat))
  54. }
  55. func scan_recursive(dir_path string, ignore []string) ([]string, []string) {
  56. folders := []string{}
  57. files := []string{}
  58. // Scan
  59. filepath.Walk(dir_path, func(path string, f os.FileInfo, err error) error {
  60. _continue := false
  61. // Loop : Ignore Files & Folders
  62. for _, i := range ignore {
  63. // If ignored path
  64. if strings.Index(path, i) != -1 {
  65. // Continue
  66. _continue = true
  67. }
  68. }
  69. if _continue == false {
  70. f, err = os.Stat(path)
  71. // If no error
  72. if err != nil {
  73. panic("ERROR. " + err.Error())
  74. }
  75. // File & Folder Mode
  76. f_mode := f.Mode()
  77. // Is folder
  78. if f_mode.IsDir() {
  79. // Append to Folders Array
  80. folders = append(folders, path)
  81. // Is file
  82. } else if f_mode.IsRegular() {
  83. // Append to Files Array
  84. files = append(files, path)
  85. }
  86. }
  87. return nil
  88. })
  89. return folders, files
  90. }
  91. func hex2bin(s string) []byte {
  92. ret, _ := hex.DecodeString(s)
  93. return ret
  94. }
  95. func Bin2hex(str string) (string, error) {
  96. i, err := strconv.ParseInt(str, 2, 0)
  97. if err != nil {
  98. return "", err
  99. }
  100. return strconv.FormatInt(i, 16), nil
  101. }
  102. type ByLen []string
  103. func (a ByLen) Len() int {
  104. return len(a)
  105. }
  106. func (a ByLen) Less(i, j int) bool {
  107. return len(a[i]) > len(a[j])
  108. }
  109. func (a ByLen) Swap(i, j int) {
  110. a[i], a[j] = a[j], a[i]
  111. }
  112. func main() {
  113. dir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
  114. if len(os.Args) < 2 {
  115. //Default options, usually double click will start this process
  116. folders, files := scan_recursive(dir, []string{os.Args[0]})
  117. for i := 0; i < len(files); i++ {
  118. thisFilepath := strings.ReplaceAll(files[i], "\\", "/")
  119. filename := path.Base(thisFilepath)
  120. extension := filepath.Ext(filename)
  121. name := filename[0 : len(filename)-len(extension)]
  122. //Check if it has the inith prefix
  123. if len(name) > 5 && name[0:5] == "inith"{
  124. //This is hex filename. Translate its name to normal filename
  125. originalName := string(hex2bin(name[5:]))
  126. var re = regexp.MustCompile(`/[/\\?%*:|"<>]/g`)
  127. safeName := re.ReplaceAllString(originalName, `-`)
  128. fmt.Println(filename + " -> " + safeName + extension)
  129. err := os.Rename(files[i], path.Dir(thisFilepath) + "/" + safeName + extension)
  130. if (err != nil){
  131. panic(err)
  132. }
  133. }
  134. }
  135. //Sort the array of folders by string length, hence deeper folder will be renamed first
  136. sort.Sort(ByLen(folders))
  137. for j := 0; j < len(folders); j++{
  138. foldername := path.Base(folders[j])
  139. fmt.Println(foldername)
  140. }
  141. }else if (len(os.Args) == 2 && os.Args[1] == "help"){
  142. //Show help message
  143. fmt.Println("ArOZ Online UM File Naming Method Converter")
  144. fmt.Println("Usage: \n ./fsconv Convert all files under current directory and its sub-directories into UTF-8 file naming method.")
  145. fmt.Println("./fsconv -um Convert all files under current directory and its sub-directories into UM File Naming Method representation.")
  146. fmt.Println("./fsconv -i {filepath_to_be_conv_to_UTF8}")
  147. fmt.Println("./fsconv -um {filepath_to_be_conv_to_UM-filenames")
  148. } else {
  149. //Given target directory
  150. }
  151. }