asset.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 cache
  17. import (
  18. "io/fs"
  19. "path/filepath"
  20. "strings"
  21. "sync"
  22. "time"
  23. "github.com/siyuan-note/filelock"
  24. "github.com/siyuan-note/logging"
  25. "github.com/siyuan-note/siyuan/kernel/util"
  26. )
  27. type Asset struct {
  28. HName string `json:"hName"`
  29. Path string `json:"path"`
  30. Updated int64 `json:"updated"`
  31. }
  32. var assetsCache = map[string]*Asset{}
  33. var assetsLock = sync.Mutex{}
  34. func GetAssets() (ret map[string]*Asset) {
  35. assetsLock.Lock()
  36. defer assetsLock.Unlock()
  37. ret = map[string]*Asset{}
  38. for k, v := range assetsCache {
  39. ret[k] = v
  40. }
  41. return
  42. }
  43. func RemoveAsset(path string) {
  44. assetsLock.Lock()
  45. defer assetsLock.Unlock()
  46. delete(assetsCache, path)
  47. }
  48. func ExistAsset(path string) (ret bool) {
  49. assetsLock.Lock()
  50. defer assetsLock.Unlock()
  51. _, ret = assetsCache[path]
  52. return
  53. }
  54. func LoadAssets() {
  55. defer logging.Recover()
  56. start := time.Now()
  57. assetsLock.Lock()
  58. defer assetsLock.Unlock()
  59. assetsCache = map[string]*Asset{}
  60. assets := util.GetDataAssetsAbsPath()
  61. filelock.Walk(assets, func(path string, info fs.FileInfo, err error) error {
  62. if nil == info {
  63. return err
  64. }
  65. if info.IsDir() {
  66. if strings.HasPrefix(info.Name(), ".") {
  67. return filepath.SkipDir
  68. }
  69. return nil
  70. }
  71. if strings.HasSuffix(info.Name(), ".sya") || strings.HasPrefix(info.Name(), ".") || filelock.IsHidden(path) {
  72. return nil
  73. }
  74. hName := util.RemoveID(info.Name())
  75. path = "assets" + filepath.ToSlash(strings.TrimPrefix(path, assets))
  76. assetsCache[path] = &Asset{
  77. HName: hName,
  78. Path: path,
  79. Updated: info.ModTime().Unix(),
  80. }
  81. return nil
  82. })
  83. elapsed := time.Since(start)
  84. if 2000 < elapsed.Milliseconds() {
  85. logging.LogInfof("loaded assets [%.2fs]", elapsed.Seconds())
  86. }
  87. }