path.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. "path/filepath"
  23. "sort"
  24. "strings"
  25. "time"
  26. "github.com/88250/gulu"
  27. "github.com/siyuan-note/logging"
  28. )
  29. var (
  30. SSL = false
  31. UserAgent = "SiYuan/" + Ver
  32. )
  33. const (
  34. AliyunServer = "https://siyuan-sync.b3logfile.com" // 云端服务地址,阿里云负载均衡,用于接口,数据同步文件上传、下载会走七牛云 OSS http://siyuan-data.b3logfile.com
  35. BazaarStatServer = "http://bazaar.b3logfile.com" // 集市包统计服务地址,直接对接 Bucket 没有 CDN 缓存
  36. BazaarOSSServer = "https://oss.b3logfile.com" // 云端对象存储地址,七牛云,仅用于读取集市包
  37. )
  38. func ShortPathForBootingDisplay(p string) string {
  39. if 25 > len(p) {
  40. return p
  41. }
  42. p = strings.TrimSuffix(p, ".sy")
  43. p = path.Base(p)
  44. return p
  45. }
  46. func IsIDPattern(str string) bool {
  47. if len("20060102150405-1a2b3c4") != len(str) {
  48. return false
  49. }
  50. if 1 != strings.Count(str, "-") {
  51. return false
  52. }
  53. parts := strings.Split(str, "-")
  54. idPart := parts[0]
  55. if 14 != len(idPart) {
  56. return false
  57. }
  58. for _, c := range idPart {
  59. if !('0' <= c && '9' >= c) {
  60. return false
  61. }
  62. }
  63. randPart := parts[1]
  64. if 7 != len(randPart) {
  65. return false
  66. }
  67. for _, c := range randPart {
  68. if !('a' <= c && 'z' >= c) && !('0' <= c && '9' >= c) {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. var LocalIPs []string
  75. func GetLocalIPs() (ret []string) {
  76. if ContainerAndroid == Container {
  77. // Android 上用不了 net.InterfaceAddrs() https://github.com/golang/go/issues/40569,所以前面使用启动内核传入的参数 localIPs
  78. LocalIPs = append(LocalIPs, LocalHost)
  79. LocalIPs = gulu.Str.RemoveDuplicatedElem(LocalIPs)
  80. return LocalIPs
  81. }
  82. ret = []string{}
  83. addrs, err := net.InterfaceAddrs()
  84. if nil != err {
  85. logging.LogWarnf("get interface addresses failed: %s", err)
  86. return
  87. }
  88. for _, addr := range addrs {
  89. if networkIp, ok := addr.(*net.IPNet); ok && !networkIp.IP.IsLoopback() && networkIp.IP.To4() != nil &&
  90. bytes.Equal([]byte{255, 255, 255, 0}, networkIp.Mask) {
  91. ret = append(ret, networkIp.IP.String())
  92. }
  93. }
  94. ret = append(ret, LocalHost)
  95. ret = gulu.Str.RemoveDuplicatedElem(ret)
  96. return
  97. }
  98. func isRunningInDockerContainer() bool {
  99. if _, runInContainer := os.LookupEnv("RUN_IN_CONTAINER"); runInContainer {
  100. return true
  101. }
  102. if _, err := os.Stat("/.dockerenv"); err == nil {
  103. return true
  104. }
  105. return false
  106. }
  107. func IsRelativePath(dest string) bool {
  108. if 1 > len(dest) {
  109. return true
  110. }
  111. if '/' == dest[0] {
  112. return false
  113. }
  114. return !strings.Contains(dest, ":/") && !strings.Contains(dest, ":\\")
  115. }
  116. func TimeFromID(id string) (ret string) {
  117. if 14 > len(id) {
  118. logging.LogWarnf("invalid id [%s], stack [\n%s]", id, logging.ShortStack())
  119. return time.Now().Format("20060102150405")
  120. }
  121. ret = id[:14]
  122. return
  123. }
  124. func GetChildDocDepth(treeAbsPath string) (ret int) {
  125. dir := strings.TrimSuffix(treeAbsPath, ".sy")
  126. if !gulu.File.IsDir(dir) {
  127. return
  128. }
  129. baseDepth := strings.Count(filepath.ToSlash(treeAbsPath), "/")
  130. depth := 1
  131. filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
  132. p := filepath.ToSlash(path)
  133. currentDepth := strings.Count(p, "/")
  134. if depth < currentDepth {
  135. depth = currentDepth
  136. }
  137. return nil
  138. })
  139. ret = depth - baseDepth
  140. return
  141. }
  142. func NormalizeEndpoint(endpoint string) string {
  143. endpoint = strings.TrimSpace(endpoint)
  144. if "" == endpoint {
  145. return ""
  146. }
  147. if !strings.HasPrefix(endpoint, "http://") && !strings.HasPrefix(endpoint, "https://") {
  148. endpoint = "http://" + endpoint
  149. }
  150. if !strings.HasSuffix(endpoint, "/") {
  151. endpoint = endpoint + "/"
  152. }
  153. return endpoint
  154. }
  155. func FilterMoveDocFromPaths(fromPaths []string, toPath string) (ret []string) {
  156. tmp := FilterSelfChildDocs(fromPaths)
  157. for _, fromPath := range tmp {
  158. fromDir := strings.TrimSuffix(fromPath, ".sy")
  159. if strings.HasPrefix(toPath, fromDir) {
  160. continue
  161. }
  162. ret = append(ret, fromPath)
  163. }
  164. return
  165. }
  166. func FilterSelfChildDocs(paths []string) (ret []string) {
  167. sort.Slice(paths, func(i, j int) bool { return strings.Count(paths[i], "/") < strings.Count(paths[j], "/") })
  168. dirs := map[string]string{}
  169. for _, fromPath := range paths {
  170. dir := strings.TrimSuffix(fromPath, ".sy")
  171. existParent := false
  172. for d, _ := range dirs {
  173. if strings.HasPrefix(fromPath, d) {
  174. existParent = true
  175. break
  176. }
  177. }
  178. if existParent {
  179. continue
  180. }
  181. dirs[dir] = fromPath
  182. ret = append(ret, fromPath)
  183. }
  184. return
  185. }