沈阳玫苑物业管理后端
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.

121 lines
2.5 KiB

  1. package utils
  2. import (
  3. "os"
  4. "os/exec"
  5. "errors"
  6. "path/filepath"
  7. "strings"
  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. //windows环境下获取绝对路径
  90. func GetCurrentPath(dir string) (string, error) {
  91. file, err := exec.LookPath(os.Args[0])
  92. if err != nil {
  93. return "", err
  94. }
  95. path, err := filepath.Abs(file)
  96. if err != nil {
  97. return "", err
  98. }
  99. i := strings.LastIndex(path, "/")
  100. if i < 0 {
  101. i = strings.LastIndex(path, "\\")
  102. }
  103. if i < 0 {
  104. return "", errors.New(`error: Can't find "/" or "\".`)
  105. }
  106. pathdir := string(path[0 : i+1])
  107. if len(dir) > 0 {
  108. dir = strings.Replace(dir, "/", "\\", -1)
  109. return filepath.Join(pathdir, dir), nil
  110. }
  111. return string(path[0 : i+1]), nil
  112. }