path_helper.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package path_helper
  2. import (
  3. "path/filepath"
  4. "strings"
  5. )
  6. func FixShareFileProtocolsPath(orgPath string) string {
  7. orgPath = fixSMBPath(orgPath, smbPrefixBase, smbPrefixFull)
  8. orgPath = fixSMBPath(orgPath, afpPrefixBase, afpPrefixFull)
  9. orgPath = fixSMBPath(orgPath, nfsPrefixBase, nfsPrefixFull)
  10. return orgPath
  11. }
  12. func fixSMBPath(orgPath string, prefixBase, prefixFull string) string {
  13. if strings.HasPrefix(orgPath, prefixBase) == true {
  14. // 匹配最基本的前缀
  15. if strings.HasPrefix(orgPath, prefixFull) == true {
  16. // 完全符合
  17. return orgPath
  18. } else {
  19. // 被转义少了一个 '/'
  20. return strings.ReplaceAll(orgPath, prefixBase, prefixFull)
  21. }
  22. } else {
  23. // 无需调整,因为没得 smb 关键词
  24. return orgPath
  25. }
  26. }
  27. // ChangePhysicalPathToSharePath 从物理地址转换为静态文件服务器分享地址
  28. func ChangePhysicalPathToSharePath(physicalFullPath, pathUrlMapKey, sharePrefixPath string) string {
  29. dirName := strings.ReplaceAll(physicalFullPath, pathUrlMapKey, "")
  30. outPath := filepath.Join(sharePrefixPath, dirName)
  31. return outPath
  32. }
  33. const (
  34. smbPrefixBase = "smb:/"
  35. smbPrefixFull = "smb://"
  36. afpPrefixBase = "afp:/"
  37. afpPrefixFull = "afp://"
  38. nfsPrefixBase = "nfs:/"
  39. nfsPrefixFull = "nfs://"
  40. )