mount.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. "fmt"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. "sync"
  24. "time"
  25. "unicode/utf8"
  26. "github.com/88250/gulu"
  27. "github.com/88250/lute/ast"
  28. "github.com/siyuan-note/filelock"
  29. "github.com/siyuan-note/logging"
  30. "github.com/siyuan-note/siyuan/kernel/treenode"
  31. "github.com/siyuan-note/siyuan/kernel/util"
  32. )
  33. func CreateBox(name string) (id string, err error) {
  34. name = gulu.Str.RemoveInvisible(name)
  35. if 512 < utf8.RuneCountInString(name) {
  36. // 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
  37. err = errors.New(Conf.Language(106))
  38. return
  39. }
  40. if "" == name {
  41. name = Conf.language(105)
  42. }
  43. WaitForWritingFiles()
  44. createDocLock.Lock()
  45. defer createDocLock.Unlock()
  46. id = ast.NewNodeID()
  47. boxLocalPath := filepath.Join(util.DataDir, id)
  48. err = os.MkdirAll(boxLocalPath, 0755)
  49. if nil != err {
  50. return
  51. }
  52. box := &Box{ID: id, Name: name}
  53. boxConf := box.GetConf()
  54. boxConf.Name = name
  55. box.SaveConf(boxConf)
  56. IncSync()
  57. logging.LogInfof("created box [%s]", id)
  58. return
  59. }
  60. func RenameBox(boxID, name string) (err error) {
  61. box := Conf.Box(boxID)
  62. if nil == box {
  63. return errors.New(Conf.Language(0))
  64. }
  65. if 512 < utf8.RuneCountInString(name) {
  66. // 限制笔记本名和文档名最大长度为 `512` https://github.com/siyuan-note/siyuan/issues/6299
  67. err = errors.New(Conf.Language(106))
  68. return
  69. }
  70. if "" == name {
  71. name = Conf.language(105)
  72. }
  73. boxConf := box.GetConf()
  74. boxConf.Name = name
  75. box.Name = name
  76. box.SaveConf(boxConf)
  77. IncSync()
  78. logging.LogInfof("renamed box [%s] to [%s]", boxID, name)
  79. return
  80. }
  81. var boxLock = sync.Map{}
  82. func RemoveBox(boxID string) (err error) {
  83. if _, ok := boxLock.Load(boxID); ok {
  84. err = fmt.Errorf(Conf.language(239))
  85. return
  86. }
  87. boxLock.Store(boxID, true)
  88. defer boxLock.Delete(boxID)
  89. if util.IsReservedFilename(boxID) {
  90. return errors.New(fmt.Sprintf("can not remove [%s] caused by it is a reserved file", boxID))
  91. }
  92. WaitForWritingFiles()
  93. isUserGuide := IsUserGuide(boxID)
  94. createDocLock.Lock()
  95. defer createDocLock.Unlock()
  96. localPath := filepath.Join(util.DataDir, boxID)
  97. if !filelock.IsExist(localPath) {
  98. return
  99. }
  100. if !gulu.File.IsDir(localPath) {
  101. return errors.New(fmt.Sprintf("can not remove [%s] caused by it is not a dir", boxID))
  102. }
  103. if !isUserGuide {
  104. var historyDir string
  105. historyDir, err = GetHistoryDir(HistoryOpDelete)
  106. if nil != err {
  107. logging.LogErrorf("get history dir failed: %s", err)
  108. return
  109. }
  110. p := strings.TrimPrefix(localPath, util.DataDir)
  111. historyPath := filepath.Join(historyDir, p)
  112. if err = filelock.Copy(localPath, historyPath); nil != err {
  113. logging.LogErrorf("gen sync history failed: %s", err)
  114. return
  115. }
  116. copyBoxAssetsToDataAssets(boxID)
  117. }
  118. unmount0(boxID)
  119. if err = filelock.Remove(localPath); nil != err {
  120. return
  121. }
  122. IncSync()
  123. logging.LogInfof("removed box [%s]", boxID)
  124. return
  125. }
  126. func Unmount(boxID string) {
  127. WaitForWritingFiles()
  128. unmount0(boxID)
  129. evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
  130. evt.Data = map[string]interface{}{
  131. "box": boxID,
  132. }
  133. util.PushEvent(evt)
  134. }
  135. func unmount0(boxID string) {
  136. box := Conf.Box(boxID)
  137. if nil == box {
  138. return
  139. }
  140. boxConf := box.GetConf()
  141. boxConf.Closed = true
  142. box.SaveConf(boxConf)
  143. box.Unindex()
  144. }
  145. func Mount(boxID string) (alreadyMount bool, err error) {
  146. if _, ok := boxLock.Load(boxID); ok {
  147. err = fmt.Errorf(Conf.language(239))
  148. return
  149. }
  150. boxLock.Store(boxID, true)
  151. defer boxLock.Delete(boxID)
  152. WaitForWritingFiles()
  153. isUserGuide := IsUserGuide(boxID)
  154. localPath := filepath.Join(util.DataDir, boxID)
  155. var reMountGuide bool
  156. if isUserGuide {
  157. // 重新挂载帮助文档
  158. guideBox := Conf.Box(boxID)
  159. if nil != guideBox {
  160. unmount0(guideBox.ID)
  161. reMountGuide = true
  162. }
  163. if err = filelock.Remove(localPath); nil != err {
  164. return
  165. }
  166. p := filepath.Join(util.WorkingDir, "guide", boxID)
  167. if err = filelock.Copy(p, localPath); nil != err {
  168. return
  169. }
  170. avDirPath := filepath.Join(util.WorkingDir, "guide", boxID, "storage", "av")
  171. if filelock.IsExist(avDirPath) {
  172. if err = filelock.Copy(avDirPath, filepath.Join(util.DataDir, "storage", "av")); nil != err {
  173. return
  174. }
  175. }
  176. if box := Conf.Box(boxID); nil != box {
  177. boxConf := box.GetConf()
  178. boxConf.Closed = true
  179. box.SaveConf(boxConf)
  180. }
  181. if Conf.OpenHelp {
  182. Conf.OpenHelp = false
  183. Conf.Save()
  184. }
  185. go func() {
  186. time.Sleep(time.Second * 3)
  187. util.PushErrMsg(Conf.Language(52), 7000)
  188. // 每次打开帮助文档时自动检查版本更新并提醒 https://github.com/siyuan-note/siyuan/issues/5057
  189. time.Sleep(time.Second * 10)
  190. CheckUpdate(true)
  191. }()
  192. }
  193. if !gulu.File.IsDir(localPath) {
  194. return false, errors.New("can not open file, just support open folder only")
  195. }
  196. for _, box := range Conf.GetOpenedBoxes() {
  197. if box.ID == boxID {
  198. return true, nil
  199. }
  200. }
  201. box := &Box{ID: boxID}
  202. boxConf := box.GetConf()
  203. boxConf.Closed = false
  204. box.SaveConf(boxConf)
  205. box.Index()
  206. // 缓存根一级的文档树展开
  207. ListDocTree(box.ID, "/", util.SortModeUnassigned, false, false, Conf.FileTree.MaxListCount)
  208. treenode.SaveBlockTree(false)
  209. util.ClearPushProgress(100)
  210. if reMountGuide {
  211. return true, nil
  212. }
  213. return false, nil
  214. }
  215. func IsUserGuide(boxID string) bool {
  216. return "20210808180117-czj9bvb" == boxID || "20210808180117-6v0mkxr" == boxID || "20211226090932-5lcq56f" == boxID || "20240530133126-axarxgx" == boxID
  217. }