file.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. "io"
  20. "os"
  21. "path"
  22. "path/filepath"
  23. "strings"
  24. "unicode/utf8"
  25. "github.com/88250/gulu"
  26. "github.com/88250/lute/ast"
  27. "github.com/siyuan-note/logging"
  28. )
  29. func IsEmptyDir(p string) bool {
  30. if !gulu.File.IsDir(p) {
  31. return false
  32. }
  33. files, err := os.ReadDir(p)
  34. if nil != err {
  35. return false
  36. }
  37. return 1 > len(files)
  38. }
  39. func RemoveID(name string) string {
  40. ext := path.Ext(name)
  41. name = strings.TrimSuffix(name, ext)
  42. if 23 < len(name) {
  43. name = name[:len(name)-23]
  44. }
  45. return name + ext
  46. }
  47. func AssetName(name string) string {
  48. _, id := LastID(name)
  49. ext := path.Ext(name)
  50. name = name[0 : len(name)-len(ext)]
  51. if !ast.IsNodeIDPattern(id) {
  52. id = ast.NewNodeID()
  53. name = name + "-" + id + ext
  54. } else {
  55. if !ast.IsNodeIDPattern(name) {
  56. name = name[:len(name)-len(id)-1] + "-" + id + ext
  57. } else {
  58. name = name + ext
  59. }
  60. }
  61. return name
  62. }
  63. func LastID(p string) (name, id string) {
  64. name = path.Base(p)
  65. ext := path.Ext(name)
  66. id = strings.TrimSuffix(name, ext)
  67. if 22 < len(id) {
  68. id = id[len(id)-22:]
  69. }
  70. return
  71. }
  72. func IsCorruptedSYData(data []byte) bool {
  73. if 64 > len(data) || '{' != data[0] {
  74. return true
  75. }
  76. return false
  77. }
  78. func FilterUploadFileName(name string) string {
  79. ret := FilterFileName(name)
  80. // 插入资源文件时去除 `[`、`(` 等符号 https://github.com/siyuan-note/siyuan/issues/6708
  81. ret = strings.ReplaceAll(ret, "~", "")
  82. //ret = strings.ReplaceAll(ret, "_", "") // 插入资源文件时允许下划线 https://github.com/siyuan-note/siyuan/issues/3534
  83. ret = strings.ReplaceAll(ret, "[", "")
  84. ret = strings.ReplaceAll(ret, "]", "")
  85. ret = strings.ReplaceAll(ret, "(", "")
  86. ret = strings.ReplaceAll(ret, ")", "")
  87. ret = strings.ReplaceAll(ret, "!", "")
  88. ret = strings.ReplaceAll(ret, "`", "")
  89. ret = strings.ReplaceAll(ret, "&", "")
  90. ret = strings.ReplaceAll(ret, "{", "")
  91. ret = strings.ReplaceAll(ret, "}", "")
  92. ret = strings.ReplaceAll(ret, "=", "")
  93. ret = strings.ReplaceAll(ret, "#", "")
  94. ret = strings.ReplaceAll(ret, "%", "")
  95. ret = strings.ReplaceAll(ret, "$", "")
  96. ret = TruncateLenFileName(ret)
  97. return ret
  98. }
  99. func TruncateLenFileName(name string) (ret string) {
  100. // 插入资源文件时文件名长度最大限制 189 字节 https://github.com/siyuan-note/siyuan/issues/7099
  101. var byteCount int
  102. buf := bytes.Buffer{}
  103. for _, r := range name {
  104. byteCount += utf8.RuneLen(r)
  105. if 189 < byteCount {
  106. break
  107. }
  108. buf.WriteRune(r)
  109. }
  110. ret = buf.String()
  111. return
  112. }
  113. func FilterFilePath(p string) (ret string) {
  114. parts := strings.Split(p, "/")
  115. var filteredParts []string
  116. for _, part := range parts {
  117. filteredParts = append(filteredParts, FilterFileName(part))
  118. }
  119. ret = strings.Join(filteredParts, "/")
  120. return
  121. }
  122. func FilterFileName(name string) string {
  123. name = strings.ReplaceAll(name, "\\", "")
  124. name = strings.ReplaceAll(name, "/", "")
  125. name = strings.ReplaceAll(name, ":", "")
  126. name = strings.ReplaceAll(name, "*", "")
  127. name = strings.ReplaceAll(name, "?", "")
  128. name = strings.ReplaceAll(name, "\"", "")
  129. name = strings.ReplaceAll(name, "'", "")
  130. name = strings.ReplaceAll(name, "<", "")
  131. name = strings.ReplaceAll(name, ">", "")
  132. name = strings.ReplaceAll(name, "|", "")
  133. name = strings.TrimSpace(name)
  134. return name
  135. }
  136. func IsSubPath(absPath, toCheckPath string) bool {
  137. if 1 > len(absPath) || 1 > len(toCheckPath) {
  138. return false
  139. }
  140. if gulu.OS.IsWindows() {
  141. if filepath.IsAbs(absPath) && filepath.IsAbs(toCheckPath) {
  142. if strings.ToLower(absPath)[0] != strings.ToLower(toCheckPath)[0] {
  143. // 不在一个盘
  144. return false
  145. }
  146. }
  147. }
  148. up := ".." + string(os.PathSeparator)
  149. rel, err := filepath.Rel(absPath, toCheckPath)
  150. if err != nil {
  151. return false
  152. }
  153. if !strings.HasPrefix(rel, up) && rel != ".." {
  154. return true
  155. }
  156. return false
  157. }
  158. func SizeOfDirectory(path string) (size int64, err error) {
  159. err = filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
  160. if nil != err {
  161. return err
  162. }
  163. if !info.IsDir() {
  164. s := info.Size()
  165. size += s
  166. } else {
  167. size += 4096
  168. }
  169. return nil
  170. })
  171. if nil != err {
  172. logging.LogErrorf("size of dir [%s] failed: %s", path, err)
  173. }
  174. return
  175. }
  176. func DataSize() (dataSize, assetsSize int64) {
  177. filepath.Walk(DataDir, func(path string, info os.FileInfo, err error) error {
  178. if nil != err {
  179. if os.IsNotExist(err) {
  180. return nil
  181. }
  182. logging.LogErrorf("size of data failed: %s", err)
  183. return io.EOF
  184. }
  185. if !info.IsDir() {
  186. s := info.Size()
  187. dataSize += s
  188. if strings.Contains(strings.TrimPrefix(path, DataDir), "assets") {
  189. assetsSize += s
  190. }
  191. } else {
  192. dataSize += 4096
  193. }
  194. return nil
  195. })
  196. return
  197. }
  198. func CeilSize(size int64) int64 {
  199. if 100*1024*1024 > size {
  200. return 100 * 1024 * 1024
  201. }
  202. for i := int64(1); i < 40; i++ {
  203. if 1024*1024*200*i > size {
  204. return 1024 * 1024 * 200 * i
  205. }
  206. }
  207. return 1024*1024*200*40 + 1
  208. }
  209. func IsReservedFilename(baseName string) bool {
  210. return "assets" == baseName || "templates" == baseName || "widgets" == baseName || "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".")
  211. }