storage.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 model
  17. import (
  18. "errors"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. "sync"
  23. "github.com/88250/gulu"
  24. "github.com/88250/lute/parse"
  25. "github.com/siyuan-note/filelock"
  26. "github.com/siyuan-note/logging"
  27. "github.com/siyuan-note/siyuan/kernel/treenode"
  28. "github.com/siyuan-note/siyuan/kernel/util"
  29. )
  30. type RecentDoc struct {
  31. RootID string `json:"rootID"`
  32. Icon string `json:"icon"`
  33. Title string `json:"title"`
  34. }
  35. var recentDocLock = sync.Mutex{}
  36. func RemoveRecentDoc(ids []string) {
  37. recentDocLock.Lock()
  38. defer recentDocLock.Unlock()
  39. recentDocs, err := getRecentDocs()
  40. if nil != err {
  41. return
  42. }
  43. ids = gulu.Str.RemoveDuplicatedElem(ids)
  44. for i, doc := range recentDocs {
  45. if gulu.Str.Contains(doc.RootID, ids) {
  46. recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
  47. break
  48. }
  49. }
  50. err = setRecentDocs(recentDocs)
  51. if nil != err {
  52. return
  53. }
  54. return
  55. }
  56. func SetRecentDocByTree(tree *parse.Tree) {
  57. recentDoc := &RecentDoc{
  58. RootID: tree.Root.ID,
  59. Icon: tree.Root.IALAttr("icon"),
  60. Title: tree.Root.IALAttr("title"),
  61. }
  62. SetRecentDoc(recentDoc)
  63. }
  64. func SetRecentDoc(doc *RecentDoc) (err error) {
  65. recentDocLock.Lock()
  66. defer recentDocLock.Unlock()
  67. recentDocs, err := getRecentDocs()
  68. if nil != err {
  69. return
  70. }
  71. for i, c := range recentDocs {
  72. if c.RootID == doc.RootID {
  73. recentDocs = append(recentDocs[:i], recentDocs[i+1:]...)
  74. break
  75. }
  76. }
  77. recentDocs = append([]*RecentDoc{doc}, recentDocs...)
  78. if 32 < len(recentDocs) {
  79. recentDocs = recentDocs[:32]
  80. }
  81. err = setRecentDocs(recentDocs)
  82. return
  83. }
  84. func GetRecentDocs() (ret []*RecentDoc, err error) {
  85. recentDocLock.Lock()
  86. defer recentDocLock.Unlock()
  87. return getRecentDocs()
  88. }
  89. func setRecentDocs(recentDocs []*RecentDoc) (err error) {
  90. dirPath := filepath.Join(util.DataDir, "storage")
  91. if err = os.MkdirAll(dirPath, 0755); nil != err {
  92. logging.LogErrorf("create storage [recent-doc] dir failed: %s", err)
  93. return
  94. }
  95. data, err := gulu.JSON.MarshalIndentJSON(recentDocs, "", " ")
  96. if nil != err {
  97. logging.LogErrorf("marshal storage [recent-doc] failed: %s", err)
  98. return
  99. }
  100. lsPath := filepath.Join(dirPath, "recent-doc.json")
  101. err = filelock.WriteFile(lsPath, data)
  102. if nil != err {
  103. logging.LogErrorf("write storage [recent-doc] failed: %s", err)
  104. return
  105. }
  106. return
  107. }
  108. func getRecentDocs() (ret []*RecentDoc, err error) {
  109. tmp := []*RecentDoc{}
  110. dataPath := filepath.Join(util.DataDir, "storage/recent-doc.json")
  111. if !gulu.File.IsExist(dataPath) {
  112. return
  113. }
  114. data, err := filelock.ReadFile(dataPath)
  115. if nil != err {
  116. logging.LogErrorf("read storage [recent-doc] failed: %s", err)
  117. return
  118. }
  119. if err = gulu.JSON.UnmarshalJSON(data, &tmp); nil != err {
  120. logging.LogErrorf("unmarshal storage [recent-doc] failed: %s", err)
  121. return
  122. }
  123. var notExists []string
  124. for _, doc := range tmp {
  125. if bt := treenode.GetBlockTree(doc.RootID); nil != bt {
  126. doc.Title = path.Base(bt.HPath) // Recent docs not updated after renaming https://github.com/siyuan-note/siyuan/issues/7827
  127. ret = append(ret, doc)
  128. } else {
  129. notExists = append(notExists, doc.RootID)
  130. }
  131. }
  132. if 0 < len(notExists) {
  133. setRecentDocs(ret)
  134. }
  135. return
  136. }
  137. type Criterion struct {
  138. Name string `json:"name"`
  139. Sort int `json:"sort"` // 0:按块类型(默认),1:按创建时间升序,2:按创建时间降序,3:按更新时间升序,4:按更新时间降序,5:按内容顺序(仅在按文档分组时)
  140. Group int `json:"group"` // 0:不分组,1:按文档分组
  141. HasReplace bool `json:"hasReplace"` // 是否有替换
  142. Method int `json:"method"` // 0:文本,1:查询语法,2:SQL,3:正则表达式
  143. HPath string `json:"hPath"`
  144. IDPath []string `json:"idPath"`
  145. K string `json:"k"` // 搜索关键字
  146. R string `json:"r"` // 替换关键字
  147. Types *CriterionTypes `json:"types"` // 类型过滤选项
  148. }
  149. type CriterionTypes struct {
  150. MathBlock bool `json:"mathBlock"`
  151. Table bool `json:"table"`
  152. Blockquote bool `json:"blockquote"`
  153. SuperBlock bool `json:"superBlock"`
  154. Paragraph bool `json:"paragraph"`
  155. Document bool `json:"document"`
  156. Heading bool `json:"heading"`
  157. List bool `json:"list"`
  158. ListItem bool `json:"listItem"`
  159. CodeBlock bool `json:"codeBlock"`
  160. HtmlBlock bool `json:"htmlBlock"`
  161. EmbedBlock bool `json:"embedBlock"`
  162. }
  163. var criteriaLock = sync.Mutex{}
  164. func RemoveCriterion(name string) (err error) {
  165. criteriaLock.Lock()
  166. defer criteriaLock.Unlock()
  167. criteria, err := getCriteria()
  168. if nil != err {
  169. return
  170. }
  171. for i, c := range criteria {
  172. if c.Name == name {
  173. criteria = append(criteria[:i], criteria[i+1:]...)
  174. break
  175. }
  176. }
  177. err = setCriteria(criteria)
  178. return
  179. }
  180. func SetCriterion(criterion *Criterion) (err error) {
  181. if "" == criterion.Name {
  182. return errors.New(Conf.Language(142))
  183. }
  184. criteriaLock.Lock()
  185. defer criteriaLock.Unlock()
  186. criteria, err := getCriteria()
  187. if nil != err {
  188. return
  189. }
  190. update := false
  191. for i, c := range criteria {
  192. if c.Name == criterion.Name {
  193. criteria[i] = criterion
  194. update = true
  195. break
  196. }
  197. }
  198. if !update {
  199. criteria = append(criteria, criterion)
  200. }
  201. err = setCriteria(criteria)
  202. return
  203. }
  204. func GetCriteria() (ret []*Criterion) {
  205. criteriaLock.Lock()
  206. defer criteriaLock.Unlock()
  207. ret, _ = getCriteria()
  208. return
  209. }
  210. func setCriteria(criteria []*Criterion) (err error) {
  211. dirPath := filepath.Join(util.DataDir, "storage")
  212. if err = os.MkdirAll(dirPath, 0755); nil != err {
  213. logging.LogErrorf("create storage [criteria] dir failed: %s", err)
  214. return
  215. }
  216. data, err := gulu.JSON.MarshalIndentJSON(criteria, "", " ")
  217. if nil != err {
  218. logging.LogErrorf("marshal storage [criteria] failed: %s", err)
  219. return
  220. }
  221. lsPath := filepath.Join(dirPath, "criteria.json")
  222. err = filelock.WriteFile(lsPath, data)
  223. if nil != err {
  224. logging.LogErrorf("write storage [criteria] failed: %s", err)
  225. return
  226. }
  227. return
  228. }
  229. func getCriteria() (ret []*Criterion, err error) {
  230. ret = []*Criterion{}
  231. dataPath := filepath.Join(util.DataDir, "storage/criteria.json")
  232. if !gulu.File.IsExist(dataPath) {
  233. return
  234. }
  235. data, err := filelock.ReadFile(dataPath)
  236. if nil != err {
  237. logging.LogErrorf("read storage [criteria] failed: %s", err)
  238. return
  239. }
  240. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  241. logging.LogErrorf("unmarshal storage [criteria] failed: %s", err)
  242. return
  243. }
  244. return
  245. }
  246. var localStorageLock = sync.Mutex{}
  247. func RemoveLocalStorageVals(keys []string) (err error) {
  248. localStorageLock.Lock()
  249. defer localStorageLock.Unlock()
  250. localStorage := getLocalStorage()
  251. for _, key := range keys {
  252. delete(localStorage, key)
  253. }
  254. return setLocalStorage(localStorage)
  255. }
  256. func SetLocalStorageVal(key string, val interface{}) (err error) {
  257. localStorageLock.Lock()
  258. defer localStorageLock.Unlock()
  259. localStorage := getLocalStorage()
  260. localStorage[key] = val
  261. return setLocalStorage(localStorage)
  262. }
  263. func SetLocalStorage(val interface{}) (err error) {
  264. localStorageLock.Lock()
  265. defer localStorageLock.Unlock()
  266. return setLocalStorage(val)
  267. }
  268. func GetLocalStorage() (ret map[string]interface{}) {
  269. localStorageLock.Lock()
  270. defer localStorageLock.Unlock()
  271. return getLocalStorage()
  272. }
  273. func setLocalStorage(val interface{}) (err error) {
  274. if util.ReadOnly {
  275. return
  276. }
  277. dirPath := filepath.Join(util.DataDir, "storage")
  278. if err = os.MkdirAll(dirPath, 0755); nil != err {
  279. logging.LogErrorf("create storage [local] dir failed: %s", err)
  280. return
  281. }
  282. data, err := gulu.JSON.MarshalIndentJSON(val, "", " ")
  283. if nil != err {
  284. logging.LogErrorf("marshal storage [local] failed: %s", err)
  285. return
  286. }
  287. lsPath := filepath.Join(dirPath, "local.json")
  288. err = filelock.WriteFile(lsPath, data)
  289. if nil != err {
  290. logging.LogErrorf("write storage [local] failed: %s", err)
  291. return
  292. }
  293. return
  294. }
  295. func getLocalStorage() (ret map[string]interface{}) {
  296. // When local.json is corrupted, clear the file to avoid being unable to enter the main interface https://github.com/siyuan-note/siyuan/issues/7911
  297. ret = map[string]interface{}{}
  298. lsPath := filepath.Join(util.DataDir, "storage/local.json")
  299. if !gulu.File.IsExist(lsPath) {
  300. return
  301. }
  302. data, err := filelock.ReadFile(lsPath)
  303. if nil != err {
  304. logging.LogErrorf("read storage [local] failed: %s", err)
  305. return
  306. }
  307. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  308. logging.LogErrorf("unmarshal storage [local] failed: %s", err)
  309. return
  310. }
  311. return
  312. }