path_helper.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // 首先需要判断这个需要替换的部分是否是包含关系
  34. if strings.HasPrefix(physicalFullPath, pathUrlMapKey) == true {
  35. dirName := strings.ReplaceAll(physicalFullPath, pathUrlMapKey, "")
  36. outPath := filepath.Join(sharePrefixPath, dirName)
  37. return outPath
  38. } else {
  39. return ""
  40. }
  41. */
  42. }
  43. const (
  44. smbPrefixBase = "smb:/"
  45. smbPrefixFull = "smb://"
  46. afpPrefixBase = "afp:/"
  47. afpPrefixFull = "afp://"
  48. nfsPrefixBase = "nfs:/"
  49. nfsPrefixFull = "nfs://"
  50. )