path.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "bytes"
  19. "net"
  20. "os"
  21. "path"
  22. "strings"
  23. "time"
  24. "github.com/88250/gulu"
  25. "github.com/siyuan-note/logging"
  26. )
  27. var (
  28. SSL = false
  29. UserAgent = "SiYuan/" + Ver
  30. )
  31. const (
  32. AliyunServer = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
  33. BazaarStatServer = "http://bazaar.b3logfile.com" // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
  34. BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取集市包
  35. )
  36. func ShortPathForBootingDisplay(p string) string {
  37. if 25 > len(p) {
  38. return p
  39. }
  40. p = strings.TrimSuffix(p, ".sy")
  41. p = path.Base(p)
  42. return p
  43. }
  44. func IsIDPattern(str string) bool {
  45. if len("20060102150405-1a2b3c4") != len(str) {
  46. return false
  47. }
  48. if 1 != strings.Count(str, "-") {
  49. return false
  50. }
  51. parts := strings.Split(str, "-")
  52. idPart := parts[0]
  53. if 14 != len(idPart) {
  54. return false
  55. }
  56. for _, c := range idPart {
  57. if !('0' <= c && '9' >= c) {
  58. return false
  59. }
  60. }
  61. randPart := parts[1]
  62. if 7 != len(randPart) {
  63. return false
  64. }
  65. for _, c := range randPart {
  66. if !('a' <= c && 'z' >= c) && !('0' <= c && '9' >= c) {
  67. return false
  68. }
  69. }
  70. return true
  71. }
  72. var LocalIPs []string
  73. func GetLocalIPs() (ret []string) {
  74. if ContainerAndroid == Container {
  75. // Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
  76. LocalIPs = append(LocalIPs, "127.0.0.1")
  77. LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
  78. return LocalIPs
  79. }
  80. ret = []string{}
  81. addrs, err := net.InterfaceAddrs()
  82. if nil != err {
  83. logging.LogWarnf("get interface addresses failed: %s", err)
  84. return
  85. }
  86. for _, addr := range addrs {
  87. if networkIp, ok := addr.(*net.IPNet); ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil &&
  88. bytes.Equal([]byte{255, 255, 255, 0}, networkIp.Mask) {
  89. ret = append(ret, networkIp.IP.String())
  90. }
  91. }
  92. ret = append(ret, "127.0.0.1")
  93. ret = gulu.Str.RemoveDuplicatedElem(ret)
  94. return
  95. }
  96. func isRunningInDockerContainer() bool {
  97. if _, runInContainer := os.LookupEnv("RUN_IN_CONTAINER"); runInContainer {
  98. return true
  99. }
  100. if _, err := os.Stat("/.dockerenv"); err == nil {
  101. return true
  102. }
  103. return false
  104. }
  105. func IsRelativePath(dest string) bool {
  106. if 1 > len(dest) {
  107. return true
  108. }
  109. if '/' == dest[0] {
  110. return false
  111. }
  112. return !strings.Contains(dest, ":/") && !strings.Contains(dest, ":\\")
  113. }
  114. func TimeFromID(id string) (ret string) {
  115. if 14 > len(id) {
  116. logging.LogWarnf("invalid id [%s], stack [\n%s]", id, logging.ShortStack())
  117. return time.Now().Format("20060102150405")
  118. }
  119. ret = id[:14]
  120. return
  121. }