GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.0 KiB

  1. package utils
  2. import (
  3. "os"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "errors"
  8. )
  9. func GetCurrentDir() string {
  10. file, err := exec.LookPath(os.Args[0])
  11. if err != nil {
  12. c, _ := os.Getwd()
  13. return c
  14. }
  15. return strings.Replace(filepath.Dir(file), "\\", "/", -1)
  16. }
  17. func GetAbsolutePath(path string) string {
  18. p := GetAbsolutePathNoFilter(path)
  19. p = strings.Replace(p, "\\", "/", -1) // 统一使用/
  20. for {
  21. a := strings.Index(p, "//")
  22. if a == -1 {
  23. break
  24. }
  25. p = strings.Replace(p, "//", "/", -1)
  26. }
  27. return p
  28. }
  29. func GetAbsolutePathNoFilter(path string) string {
  30. if strings.Index(path, "file:/") == 0 {
  31. path = path[5:]
  32. mpos := strings.Index(path, ":")
  33. if mpos >= 0 {
  34. //去除开头斜杠, 如: ///C:/test
  35. for {
  36. if len(path) > 0 && (path[0] == '/' || path[0] == '\\') {
  37. path = path[1:]
  38. } else {
  39. break
  40. }
  41. }
  42. return (path)
  43. }
  44. for {
  45. if len(path) > 1 && (path[0] == '/' || path[0] == '\\') && (path[1] == '/' || path[1] == '\\') {
  46. path = path[1:]
  47. } else {
  48. break
  49. }
  50. }
  51. path, _ = filepath.Abs(path)
  52. return (path)
  53. } else if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  54. r, _ := filepath.Abs(GetCurrentDir() + path[1:])
  55. return (r)
  56. }
  57. r, _ := filepath.Abs(path)
  58. return (r)
  59. }
  60. func GetDir(path string) string {
  61. if strings.Index(path, "./") == 0 || strings.Index(path, ".\\") == 0 {
  62. return filepath.ToSlash(filepath.Dir(GetCurrentDir() + path[1:]))
  63. }
  64. return filepath.ToSlash(filepath.Dir(path))
  65. }
  66. func EnsureDir(dir string) string {
  67. fullDir := GetAbsolutePath(dir)
  68. if IsExists(fullDir) {
  69. return fullDir
  70. }
  71. os.MkdirAll(fullDir, 777)
  72. return fullDir
  73. }
  74. func EnsureFilePath(filePath string) string {
  75. filePath = GetAbsolutePath(filePath)
  76. EnsureDir(GetDir(filePath))
  77. return filePath
  78. }
  79. func IsExists(path string) bool {
  80. if path == "" {
  81. return false
  82. }
  83. _, err := os.Stat(path)
  84. if err != nil && os.IsNotExist(err) {
  85. return false
  86. }
  87. return true
  88. }
  89. /******************************************************************************
  90. *
  91. * @Function Name : GetCurrentPath
  92. *-----------------------------------------------------------------------------
  93. *
  94. * @Description : windows下获取传入相对路径的绝对路径
  95. *
  96. * @Function Parameters : dir 相对路径
  97. *
  98. * @Return Value : string 绝对路径
  99. *
  100. * @Return Value : error 执行出现的错误
  101. *
  102. * @Author : zhangxin
  103. *
  104. * @Date : 2021-03-19
  105. *
  106. ******************************************************************************/
  107. func GetCurrentPath(dir string) (string, error) {
  108. file, err := exec.LookPath(os.Args[0])
  109. if err != nil {
  110. return "", err
  111. }
  112. path, err := filepath.Abs(file)
  113. if err != nil {
  114. return "", err
  115. }
  116. i := strings.LastIndex(path, "/")
  117. if i < 0 {
  118. i = strings.LastIndex(path, "\\")
  119. }
  120. if i < 0 {
  121. return "", errors.New(`error: Can't find "/" or "\".`)
  122. }
  123. pathdir := path[0 : i+1]
  124. if len(dir) > 0 {
  125. dir = strings.Replace(dir, "/", "\\", -1)
  126. return filepath.Join(pathdir, dir), nil
  127. }
  128. return path[0 : i+1], nil
  129. }