storage.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 !filelock.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. ReplaceTypes *CriterionReplaceTypes `json:"replaceTypes"` // 替换类型过滤选项
  149. }
  150. type CriterionTypes struct {
  151. MathBlock bool `json:"mathBlock"`
  152. Table bool `json:"table"`
  153. Blockquote bool `json:"blockquote"`
  154. SuperBlock bool `json:"superBlock"`
  155. Paragraph bool `json:"paragraph"`
  156. Document bool `json:"document"`
  157. Heading bool `json:"heading"`
  158. List bool `json:"list"`
  159. ListItem bool `json:"listItem"`
  160. CodeBlock bool `json:"codeBlock"`
  161. HtmlBlock bool `json:"htmlBlock"`
  162. EmbedBlock bool `json:"embedBlock"`
  163. DatabaseBlock bool `json:"databaseBlock"`
  164. AudioBlock bool `json:"audioBlock"`
  165. VideoBlock bool `json:"videoBlock"`
  166. IFrameBlock bool `json:"iframeBlock"`
  167. WidgetBlock bool `json:"widgetBlock"`
  168. }
  169. type CriterionReplaceTypes struct {
  170. Text bool `json:"text"`
  171. ImgText bool `json:"imgText"`
  172. ImgTitle bool `json:"imgTitle"`
  173. ImgSrc bool `json:"imgSrc"`
  174. AText bool `json:"aText"`
  175. ATitle bool `json:"aTitle"`
  176. AHref bool `json:"aHref"`
  177. Code bool `json:"code"`
  178. Em bool `json:"em"`
  179. Strong bool `json:"strong"`
  180. InlineMath bool `json:"inlineMath"`
  181. InlineMemo bool `json:"inlineMemo"`
  182. Kbd bool `json:"kbd"`
  183. Mark bool `json:"mark"`
  184. S bool `json:"s"`
  185. Sub bool `json:"sub"`
  186. Sup bool `json:"sup"`
  187. Tag bool `json:"tag"`
  188. U bool `json:"u"`
  189. DocTitle bool `json:"docTitle"`
  190. CodeBlock bool `json:"codeBlock"`
  191. MathBlock bool `json:"mathBlock"`
  192. HtmlBlock bool `json:"htmlBlock"`
  193. }
  194. var criteriaLock = sync.Mutex{}
  195. func RemoveCriterion(name string) (err error) {
  196. criteriaLock.Lock()
  197. defer criteriaLock.Unlock()
  198. criteria, err := getCriteria()
  199. if nil != err {
  200. return
  201. }
  202. for i, c := range criteria {
  203. if c.Name == name {
  204. criteria = append(criteria[:i], criteria[i+1:]...)
  205. break
  206. }
  207. }
  208. err = setCriteria(criteria)
  209. return
  210. }
  211. func SetCriterion(criterion *Criterion) (err error) {
  212. if "" == criterion.Name {
  213. return errors.New(Conf.Language(142))
  214. }
  215. criteriaLock.Lock()
  216. defer criteriaLock.Unlock()
  217. criteria, err := getCriteria()
  218. if nil != err {
  219. return
  220. }
  221. update := false
  222. for i, c := range criteria {
  223. if c.Name == criterion.Name {
  224. criteria[i] = criterion
  225. update = true
  226. break
  227. }
  228. }
  229. if !update {
  230. criteria = append(criteria, criterion)
  231. }
  232. err = setCriteria(criteria)
  233. return
  234. }
  235. func GetCriteria() (ret []*Criterion) {
  236. criteriaLock.Lock()
  237. defer criteriaLock.Unlock()
  238. ret, _ = getCriteria()
  239. return
  240. }
  241. func setCriteria(criteria []*Criterion) (err error) {
  242. dirPath := filepath.Join(util.DataDir, "storage")
  243. if err = os.MkdirAll(dirPath, 0755); nil != err {
  244. logging.LogErrorf("create storage [criteria] dir failed: %s", err)
  245. return
  246. }
  247. data, err := gulu.JSON.MarshalIndentJSON(criteria, "", " ")
  248. if nil != err {
  249. logging.LogErrorf("marshal storage [criteria] failed: %s", err)
  250. return
  251. }
  252. lsPath := filepath.Join(dirPath, "criteria.json")
  253. err = filelock.WriteFile(lsPath, data)
  254. if nil != err {
  255. logging.LogErrorf("write storage [criteria] failed: %s", err)
  256. return
  257. }
  258. return
  259. }
  260. func getCriteria() (ret []*Criterion, err error) {
  261. ret = []*Criterion{}
  262. dataPath := filepath.Join(util.DataDir, "storage/criteria.json")
  263. if !filelock.IsExist(dataPath) {
  264. return
  265. }
  266. data, err := filelock.ReadFile(dataPath)
  267. if nil != err {
  268. logging.LogErrorf("read storage [criteria] failed: %s", err)
  269. return
  270. }
  271. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  272. logging.LogErrorf("unmarshal storage [criteria] failed: %s", err)
  273. return
  274. }
  275. return
  276. }
  277. var localStorageLock = sync.Mutex{}
  278. func RemoveLocalStorageVals(keys []string) (err error) {
  279. localStorageLock.Lock()
  280. defer localStorageLock.Unlock()
  281. localStorage := getLocalStorage()
  282. for _, key := range keys {
  283. delete(localStorage, key)
  284. }
  285. return setLocalStorage(localStorage)
  286. }
  287. func SetLocalStorageVal(key string, val interface{}) (err error) {
  288. localStorageLock.Lock()
  289. defer localStorageLock.Unlock()
  290. localStorage := getLocalStorage()
  291. localStorage[key] = val
  292. return setLocalStorage(localStorage)
  293. }
  294. func SetLocalStorage(val interface{}) (err error) {
  295. localStorageLock.Lock()
  296. defer localStorageLock.Unlock()
  297. return setLocalStorage(val)
  298. }
  299. func GetLocalStorage() (ret map[string]interface{}) {
  300. localStorageLock.Lock()
  301. defer localStorageLock.Unlock()
  302. return getLocalStorage()
  303. }
  304. func setLocalStorage(val interface{}) (err error) {
  305. if util.ReadOnly {
  306. return
  307. }
  308. dirPath := filepath.Join(util.DataDir, "storage")
  309. if err = os.MkdirAll(dirPath, 0755); nil != err {
  310. logging.LogErrorf("create storage [local] dir failed: %s", err)
  311. return
  312. }
  313. data, err := gulu.JSON.MarshalIndentJSON(val, "", " ")
  314. if nil != err {
  315. logging.LogErrorf("marshal storage [local] failed: %s", err)
  316. return
  317. }
  318. lsPath := filepath.Join(dirPath, "local.json")
  319. err = filelock.WriteFile(lsPath, data)
  320. if nil != err {
  321. logging.LogErrorf("write storage [local] failed: %s", err)
  322. return
  323. }
  324. return
  325. }
  326. func getLocalStorage() (ret map[string]interface{}) {
  327. // 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
  328. ret = map[string]interface{}{}
  329. lsPath := filepath.Join(util.DataDir, "storage/local.json")
  330. if !filelock.IsExist(lsPath) {
  331. return
  332. }
  333. data, err := filelock.ReadFile(lsPath)
  334. if nil != err {
  335. logging.LogErrorf("read storage [local] failed: %s", err)
  336. return
  337. }
  338. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  339. logging.LogErrorf("unmarshal storage [local] failed: %s", err)
  340. return
  341. }
  342. return
  343. }