workspace.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SiYuan - Build Your Eternal Digital Garden
  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 api
  17. import (
  18. "errors"
  19. "fmt"
  20. "net/http"
  21. "os"
  22. "path/filepath"
  23. "strings"
  24. "time"
  25. "unicode/utf8"
  26. "github.com/88250/gulu"
  27. "github.com/gin-gonic/gin"
  28. "github.com/gofrs/flock"
  29. "github.com/siyuan-note/logging"
  30. "github.com/siyuan-note/siyuan/kernel/model"
  31. "github.com/siyuan-note/siyuan/kernel/util"
  32. )
  33. func createWorkspaceDir(c *gin.Context) {
  34. ret := gulu.Ret.NewResult()
  35. defer c.JSON(http.StatusOK, ret)
  36. arg, ok := util.JsonArg(c, ret)
  37. if !ok {
  38. return
  39. }
  40. absPath := arg["path"].(string)
  41. absPath = gulu.Str.RemoveInvisible(absPath)
  42. absPath = strings.TrimSpace(absPath)
  43. if isInvalidWorkspacePath(absPath) {
  44. ret.Code = -1
  45. ret.Msg = "This workspace name is not allowed, please use another name"
  46. return
  47. }
  48. if gulu.File.IsExist(absPath) {
  49. ret.Code = -1
  50. ret.Msg = model.Conf.Language(78)
  51. return
  52. }
  53. if err := os.MkdirAll(absPath, 0755); nil != err {
  54. ret.Code = -1
  55. ret.Msg = fmt.Sprintf("create workspace dir [%s] failed: %s", absPath, err)
  56. return
  57. }
  58. workspacePaths, err := readWorkspacePaths()
  59. if nil != err {
  60. ret.Code = -1
  61. ret.Msg = err.Error()
  62. return
  63. }
  64. workspacePaths = append(workspacePaths, absPath)
  65. if err = writeWorkspacePaths(workspacePaths); nil != err {
  66. ret.Code = -1
  67. ret.Msg = err.Error()
  68. return
  69. }
  70. }
  71. func removeWorkspaceDir(c *gin.Context) {
  72. ret := gulu.Ret.NewResult()
  73. defer c.JSON(http.StatusOK, ret)
  74. arg, ok := util.JsonArg(c, ret)
  75. if !ok {
  76. return
  77. }
  78. path := arg["path"].(string)
  79. workspacePaths, err := readWorkspacePaths()
  80. if nil != err {
  81. ret.Code = -1
  82. ret.Msg = err.Error()
  83. return
  84. }
  85. workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
  86. if err = writeWorkspacePaths(workspacePaths); nil != err {
  87. ret.Code = -1
  88. ret.Msg = err.Error()
  89. return
  90. }
  91. if util.WorkspaceDir == path && (util.ContainerIOS == util.Container || util.ContainerAndroid == util.Container) {
  92. os.Exit(util.ExitCodeOk)
  93. }
  94. }
  95. type Workspace struct {
  96. Path string `json:"path"`
  97. Closed bool `json:"closed"`
  98. }
  99. func getWorkspaces(c *gin.Context) {
  100. ret := gulu.Ret.NewResult()
  101. defer c.JSON(http.StatusOK, ret)
  102. workspacePaths, err := readWorkspacePaths()
  103. if nil != err {
  104. ret.Code = -1
  105. ret.Msg = err.Error()
  106. return
  107. }
  108. var workspaces []*Workspace
  109. for _, p := range workspacePaths {
  110. closed := false
  111. f := flock.New(filepath.Join(p, ".lock"))
  112. ok, _ := f.TryLock()
  113. if ok {
  114. closed = true
  115. }
  116. f.Unlock()
  117. workspaces = append(workspaces, &Workspace{Path: p, Closed: closed})
  118. }
  119. ret.Data = workspaces
  120. }
  121. func setWorkspaceDir(c *gin.Context) {
  122. ret := gulu.Ret.NewResult()
  123. defer c.JSON(http.StatusOK, ret)
  124. arg, ok := util.JsonArg(c, ret)
  125. if !ok {
  126. return
  127. }
  128. path := arg["path"].(string)
  129. if util.WorkspaceDir == path {
  130. ret.Code = -1
  131. ret.Msg = model.Conf.Language(78)
  132. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  133. return
  134. }
  135. if gulu.OS.IsWindows() {
  136. installDir := filepath.Dir(util.WorkingDir)
  137. if strings.HasPrefix(path, installDir) {
  138. ret.Code = -1
  139. ret.Msg = model.Conf.Language(98)
  140. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  141. return
  142. }
  143. }
  144. workspacePaths, err := readWorkspacePaths()
  145. if nil != err {
  146. ret.Code = -1
  147. ret.Msg = err.Error()
  148. return
  149. }
  150. workspacePaths = append(workspacePaths, path)
  151. workspacePaths = gulu.Str.RemoveDuplicatedElem(workspacePaths)
  152. workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
  153. workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
  154. if err = writeWorkspacePaths(workspacePaths); nil != err {
  155. ret.Code = -1
  156. ret.Msg = err.Error()
  157. return
  158. }
  159. if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
  160. util.PushMsg(model.Conf.Language(42), 1000*15)
  161. time.Sleep(time.Second * 2)
  162. model.Close(false, 1)
  163. }
  164. }
  165. func readWorkspacePaths() (ret []string, err error) {
  166. ret = []string{}
  167. workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
  168. data, err := os.ReadFile(workspaceConf)
  169. if nil != err {
  170. msg := fmt.Sprintf("read workspace conf [%s] failed: %s", workspaceConf, err)
  171. logging.LogErrorf(msg)
  172. err = errors.New(msg)
  173. return
  174. }
  175. if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
  176. msg := fmt.Sprintf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err)
  177. logging.LogErrorf(msg)
  178. err = errors.New(msg)
  179. return
  180. }
  181. return
  182. }
  183. func writeWorkspacePaths(workspacePaths []string) (err error) {
  184. workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
  185. data, err := gulu.JSON.MarshalJSON(workspacePaths)
  186. if nil != err {
  187. msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err)
  188. logging.LogErrorf(msg)
  189. err = errors.New(msg)
  190. return
  191. }
  192. if err = os.WriteFile(workspaceConf, data, 0644); nil != err {
  193. msg := fmt.Sprintf("write workspace conf [%s] failed: %s", workspaceConf, err)
  194. logging.LogErrorf(msg)
  195. err = errors.New(msg)
  196. return
  197. }
  198. return
  199. }
  200. func isInvalidWorkspacePath(absPath string) bool {
  201. if "" == absPath {
  202. return true
  203. }
  204. name := filepath.Base(absPath)
  205. if "" == name {
  206. return true
  207. }
  208. if strings.HasPrefix(name, ".") {
  209. return true
  210. }
  211. if !gulu.File.IsValidFilename(name) {
  212. return true
  213. }
  214. if 16 < utf8.RuneCountInString(name) {
  215. return true
  216. }
  217. return "siyuan" == name || "conf" == name || "home" == name || "data" == name || "temp" == name
  218. }