file.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // SiYuan - Refactor your thinking
  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. "io/fs"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "strings"
  25. "unicode/utf8"
  26. "github.com/88250/gulu"
  27. "github.com/88250/lute/ast"
  28. "github.com/siyuan-note/logging"
  29. )
  30. func IsEmptyDir(p string) bool {
  31. if !gulu.File.IsDir(p) {
  32. return false
  33. }
  34. files, err := os.ReadDir(p)
  35. if nil != err {
  36. return false
  37. }
  38. return 1 > len(files)
  39. }
  40. func IsSymlink(dir fs.DirEntry) bool {
  41. return dir.Type() == fs.ModeSymlink
  42. }
  43. func IsDirRegularOrSymlink(dir fs.DirEntry) bool {
  44. return dir.IsDir() || IsSymlink(dir)
  45. }
  46. func IsPathRegularDirOrSymlinkDir(path string) bool {
  47. fio, err := os.Stat(path)
  48. if os.IsNotExist(err) {
  49. return false
  50. }
  51. if nil != err {
  52. return false
  53. }
  54. return fio.IsDir()
  55. }
  56. func RemoveID(name string) string {
  57. ext := path.Ext(name)
  58. name = strings.TrimSuffix(name, ext)
  59. if 23 < len(name) {
  60. name = name[:len(name)-23]
  61. }
  62. return name + ext
  63. }
  64. func AssetName(name string) string {
  65. _, id := LastID(name)
  66. ext := path.Ext(name)
  67. name = name[0 : len(name)-len(ext)]
  68. if !ast.IsNodeIDPattern(id) {
  69. id = ast.NewNodeID()
  70. name = name + "-" + id + ext
  71. } else {
  72. if !ast.IsNodeIDPattern(name) {
  73. name = name[:len(name)-len(id)-1] + "-" + id + ext
  74. } else {
  75. name = name + ext
  76. }
  77. }
  78. return name
  79. }
  80. func LastID(p string) (name, id string) {
  81. name = path.Base(p)
  82. ext := path.Ext(name)
  83. id = strings.TrimSuffix(name, ext)
  84. if 22 < len(id) {
  85. id = id[len(id)-22:]
  86. }
  87. return
  88. }
  89. func IsCorruptedSYData(data []byte) bool {
  90. if 64 > len(data) || '{' != data[0] {
  91. return true
  92. }
  93. return false
  94. }
  95. func FilterUploadFileName(name string) string {
  96. ret := FilterFileName(name)
  97. // 插入资源文件时去除 `[`、`(` 等符号 https://github.com/siyuan-note/siyuan/issues/6708
  98. ret = strings.ReplaceAll(ret, "~", "")
  99. //ret = strings.ReplaceAll(ret, "_", "") // 插入资源文件时允许下划线 https://github.com/siyuan-note/siyuan/issues/3534
  100. ret = strings.ReplaceAll(ret, "[", "")
  101. ret = strings.ReplaceAll(ret, "]", "")
  102. ret = strings.ReplaceAll(ret, "(", "")
  103. ret = strings.ReplaceAll(ret, ")", "")
  104. ret = strings.ReplaceAll(ret, "!", "")
  105. ret = strings.ReplaceAll(ret, "`", "")
  106. ret = strings.ReplaceAll(ret, "&", "")
  107. ret = strings.ReplaceAll(ret, "{", "")
  108. ret = strings.ReplaceAll(ret, "}", "")
  109. ret = strings.ReplaceAll(ret, "=", "")
  110. ret = strings.ReplaceAll(ret, "#", "")
  111. ret = strings.ReplaceAll(ret, "%", "")
  112. ret = strings.ReplaceAll(ret, "$", "")
  113. ret = TruncateLenFileName(ret)
  114. return ret
  115. }
  116. func TruncateLenFileName(name string) (ret string) {
  117. // 插入资源文件时文件名长度最大限制 189 字节 https://github.com/siyuan-note/siyuan/issues/7099
  118. var byteCount int
  119. buf := bytes.Buffer{}
  120. for _, r := range name {
  121. byteCount += utf8.RuneLen(r)
  122. if 189 < byteCount {
  123. break
  124. }
  125. buf.WriteRune(r)
  126. }
  127. ret = buf.String()
  128. return
  129. }
  130. func FilterFilePath(p string) (ret string) {
  131. parts := strings.Split(p, "/")
  132. var filteredParts []string
  133. for _, part := range parts {
  134. filteredParts = append(filteredParts, FilterFileName(part))
  135. }
  136. ret = strings.Join(filteredParts, "/")
  137. return
  138. }
  139. func FilterFileName(name string) string {
  140. name = strings.ReplaceAll(name, "\\", "")
  141. name = strings.ReplaceAll(name, "/", "")
  142. name = strings.ReplaceAll(name, ":", "")
  143. name = strings.ReplaceAll(name, "*", "")
  144. name = strings.ReplaceAll(name, "?", "")
  145. name = strings.ReplaceAll(name, "\"", "")
  146. name = strings.ReplaceAll(name, "'", "")
  147. name = strings.ReplaceAll(name, "<", "")
  148. name = strings.ReplaceAll(name, ">", "")
  149. name = strings.ReplaceAll(name, "|", "")
  150. name = strings.TrimSpace(name)
  151. return name
  152. }
  153. func IsSubPath(absPath, toCheckPath string) bool {
  154. if 1 > len(absPath) || 1 > len(toCheckPath) {
  155. return false
  156. }
  157. if gulu.OS.IsWindows() {
  158. if filepath.IsAbs(absPath) && filepath.IsAbs(toCheckPath) {
  159. if strings.ToLower(absPath)[0] != strings.ToLower(toCheckPath)[0] {
  160. // 不在一个盘
  161. return false
  162. }
  163. }
  164. }
  165. up := ".." + string(os.PathSeparator)
  166. rel, err := filepath.Rel(absPath, toCheckPath)
  167. if err != nil {
  168. return false
  169. }
  170. if !strings.HasPrefix(rel, up) && rel != ".." {
  171. return true
  172. }
  173. return false
  174. }
  175. func SizeOfDirectory(path string) (size int64, err error) {
  176. err = filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
  177. if nil != err {
  178. return err
  179. }
  180. if !info.IsDir() {
  181. s := info.Size()
  182. size += s
  183. } else {
  184. size += 4096
  185. }
  186. return nil
  187. })
  188. if nil != err {
  189. logging.LogErrorf("size of dir [%s] failed: %s", path, err)
  190. }
  191. return
  192. }
  193. func DataSize() (dataSize, assetsSize int64) {
  194. filepath.Walk(DataDir, func(path string, info os.FileInfo, err error) error {
  195. if nil != err {
  196. if os.IsNotExist(err) {
  197. return nil
  198. }
  199. logging.LogErrorf("size of data failed: %s", err)
  200. return io.EOF
  201. }
  202. if !info.IsDir() {
  203. s := info.Size()
  204. dataSize += s
  205. if strings.Contains(strings.TrimPrefix(path, DataDir), "assets") {
  206. assetsSize += s
  207. }
  208. } else {
  209. dataSize += 4096
  210. }
  211. return nil
  212. })
  213. return
  214. }
  215. func CeilSize(size int64) int64 {
  216. if 100*1024*1024 > size {
  217. return 100 * 1024 * 1024
  218. }
  219. for i := int64(1); i < 40; i++ {
  220. if 1024*1024*200*i > size {
  221. return 1024 * 1024 * 200 * i
  222. }
  223. }
  224. return 1024*1024*200*40 + 1
  225. }
  226. func IsReservedFilename(baseName string) bool {
  227. return "assets" == baseName || "templates" == baseName || "widgets" == baseName || "emojis" == baseName || ".siyuan" == baseName || strings.HasPrefix(baseName, ".")
  228. }
  229. func WalkWithSymlinks(root string, fn filepath.WalkFunc) error {
  230. // 感谢 https://github.com/edwardrf/symwalk/blob/main/symwalk.go
  231. rr, err := filepath.EvalSymlinks(root) // Find real base if there is any symlinks in the path
  232. if err != nil {
  233. return err
  234. }
  235. visitedDirs := make(map[string]struct{})
  236. return filepath.Walk(rr, getWalkFn(visitedDirs, fn))
  237. }
  238. func getWalkFn(visitedDirs map[string]struct{}, fn filepath.WalkFunc) filepath.WalkFunc {
  239. return func(path string, info os.FileInfo, err error) error {
  240. if err != nil {
  241. return fn(path, info, err)
  242. }
  243. if info.IsDir() {
  244. if _, ok := visitedDirs[path]; ok {
  245. return filepath.SkipDir
  246. }
  247. visitedDirs[path] = struct{}{}
  248. }
  249. if err := fn(path, info, err); err != nil {
  250. return err
  251. }
  252. if info.Mode()&os.ModeSymlink == 0 {
  253. return nil
  254. }
  255. // path is a symlink
  256. rp, err := filepath.EvalSymlinks(path)
  257. if err != nil {
  258. return err
  259. }
  260. ri, err := os.Stat(rp)
  261. if err != nil {
  262. return err
  263. }
  264. if ri.IsDir() {
  265. return filepath.Walk(rp, getWalkFn(visitedDirs, fn))
  266. }
  267. return nil
  268. }
  269. }