workspace.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 api
  17. import (
  18. "fmt"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "sort"
  23. "strings"
  24. "time"
  25. "unicode/utf8"
  26. "github.com/88250/gulu"
  27. "github.com/facette/natsort"
  28. "github.com/gin-gonic/gin"
  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 checkWorkspaceDir(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. path := arg["path"].(string)
  41. if isInvalidWorkspacePath(path) {
  42. ret.Code = -1
  43. ret.Msg = "This workspace name is not allowed, please use another name"
  44. return
  45. }
  46. if !gulu.File.IsExist(path) {
  47. ret.Code = -1
  48. ret.Msg = "This workspace does not exist"
  49. return
  50. }
  51. entries, err := os.ReadDir(path)
  52. if nil != err {
  53. ret.Code = -1
  54. ret.Msg = fmt.Sprintf("read workspace dir [%s] failed: %s", path, err)
  55. }
  56. var existsConf, existsData bool
  57. for _, entry := range entries {
  58. if !existsConf {
  59. existsConf = "conf" == entry.Name() && entry.IsDir()
  60. }
  61. if !existsData {
  62. existsData = "data" == entry.Name() && entry.IsDir()
  63. }
  64. if existsConf && existsData {
  65. break
  66. }
  67. }
  68. if existsConf {
  69. existsConf = gulu.File.IsExist(filepath.Join(path, "conf", "conf.json"))
  70. }
  71. ret.Data = map[string]interface{}{
  72. "isWorkspace": existsConf && existsData,
  73. }
  74. }
  75. func createWorkspaceDir(c *gin.Context) {
  76. ret := gulu.Ret.NewResult()
  77. defer c.JSON(http.StatusOK, ret)
  78. arg, ok := util.JsonArg(c, ret)
  79. if !ok {
  80. return
  81. }
  82. absPath := arg["path"].(string)
  83. absPath = gulu.Str.RemoveInvisible(absPath)
  84. absPath = strings.TrimSpace(absPath)
  85. if isInvalidWorkspacePath(absPath) {
  86. ret.Code = -1
  87. ret.Msg = "This workspace name is not allowed, please use another name"
  88. return
  89. }
  90. if !gulu.File.IsExist(absPath) {
  91. if err := os.MkdirAll(absPath, 0755); nil != err {
  92. ret.Code = -1
  93. ret.Msg = fmt.Sprintf("create workspace dir [%s] failed: %s", absPath, err)
  94. return
  95. }
  96. }
  97. workspacePaths, err := util.ReadWorkspacePaths()
  98. if nil != err {
  99. ret.Code = -1
  100. ret.Msg = err.Error()
  101. return
  102. }
  103. workspacePaths = append(workspacePaths, absPath)
  104. if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
  105. ret.Code = -1
  106. ret.Msg = err.Error()
  107. return
  108. }
  109. }
  110. func removeWorkspaceDir(c *gin.Context) {
  111. ret := gulu.Ret.NewResult()
  112. defer c.JSON(http.StatusOK, ret)
  113. arg, ok := util.JsonArg(c, ret)
  114. if !ok {
  115. return
  116. }
  117. path := arg["path"].(string)
  118. if util.IsWorkspaceLocked(path) || util.WorkspaceDir == path {
  119. msg := "Cannot remove current workspace"
  120. ret.Code = -1
  121. ret.Msg = msg
  122. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  123. return
  124. }
  125. workspacePaths, err := util.ReadWorkspacePaths()
  126. if nil != err {
  127. ret.Code = -1
  128. ret.Msg = err.Error()
  129. return
  130. }
  131. workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
  132. if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
  133. ret.Code = -1
  134. ret.Msg = err.Error()
  135. return
  136. }
  137. }
  138. func removeWorkspaceDirPhysically(c *gin.Context) {
  139. ret := gulu.Ret.NewResult()
  140. defer c.JSON(http.StatusOK, ret)
  141. arg, ok := util.JsonArg(c, ret)
  142. if !ok {
  143. return
  144. }
  145. path := arg["path"].(string)
  146. if gulu.File.IsDir(path) {
  147. err := os.RemoveAll(path)
  148. if nil != err {
  149. ret.Code = -1
  150. ret.Msg = err.Error()
  151. return
  152. }
  153. }
  154. logging.LogInfof("removed workspace [%s] physically", path)
  155. if util.WorkspaceDir == path {
  156. os.Exit(logging.ExitCodeOk)
  157. }
  158. }
  159. type Workspace struct {
  160. Path string `json:"path"`
  161. Closed bool `json:"closed"`
  162. }
  163. func getMobileWorkspaces(c *gin.Context) {
  164. ret := gulu.Ret.NewResult()
  165. defer c.JSON(http.StatusOK, ret)
  166. if util.ContainerIOS != util.Container && util.ContainerAndroid != util.Container {
  167. return
  168. }
  169. root := filepath.Dir(util.WorkspaceDir)
  170. dirs, err := os.ReadDir(root)
  171. if nil != err {
  172. logging.LogErrorf("read dir [%s] failed: %s", root, err)
  173. ret.Code = -1
  174. ret.Msg = err.Error()
  175. return
  176. }
  177. ret.Data = []string{}
  178. var paths []string
  179. for _, dir := range dirs {
  180. if dir.IsDir() {
  181. absPath := filepath.Join(root, dir.Name())
  182. if isInvalidWorkspacePath(absPath) {
  183. continue
  184. }
  185. paths = append(paths, absPath)
  186. }
  187. }
  188. ret.Data = paths
  189. }
  190. func getWorkspaces(c *gin.Context) {
  191. ret := gulu.Ret.NewResult()
  192. defer c.JSON(http.StatusOK, ret)
  193. workspacePaths, err := util.ReadWorkspacePaths()
  194. if nil != err {
  195. ret.Code = -1
  196. ret.Msg = err.Error()
  197. return
  198. }
  199. var workspaces, openedWorkspaces, closedWorkspaces []*Workspace
  200. for _, p := range workspacePaths {
  201. closed := !util.IsWorkspaceLocked(p)
  202. if closed {
  203. closedWorkspaces = append(closedWorkspaces, &Workspace{Path: p, Closed: closed})
  204. } else {
  205. openedWorkspaces = append(openedWorkspaces, &Workspace{Path: p, Closed: closed})
  206. }
  207. }
  208. sort.Slice(openedWorkspaces, func(i, j int) bool {
  209. return natsort.Compare(util.RemoveEmojiInvisible(filepath.Base(openedWorkspaces[i].Path)), util.RemoveEmojiInvisible(filepath.Base(openedWorkspaces[j].Path)))
  210. })
  211. sort.Slice(closedWorkspaces, func(i, j int) bool {
  212. return natsort.Compare(util.RemoveEmojiInvisible(filepath.Base(closedWorkspaces[i].Path)), util.RemoveEmojiInvisible(filepath.Base(closedWorkspaces[j].Path)))
  213. })
  214. workspaces = append(workspaces, openedWorkspaces...)
  215. workspaces = append(workspaces, closedWorkspaces...)
  216. ret.Data = workspaces
  217. }
  218. func setWorkspaceDir(c *gin.Context) {
  219. ret := gulu.Ret.NewResult()
  220. defer c.JSON(http.StatusOK, ret)
  221. arg, ok := util.JsonArg(c, ret)
  222. if !ok {
  223. return
  224. }
  225. path := arg["path"].(string)
  226. if util.WorkspaceDir == path {
  227. ret.Code = -1
  228. ret.Msg = model.Conf.Language(78)
  229. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  230. return
  231. }
  232. if util.IsCloudDrivePath(path) {
  233. ret.Code = -1
  234. ret.Msg = model.Conf.Language(196)
  235. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  236. return
  237. }
  238. if gulu.OS.IsWindows() {
  239. // 改进判断工作空间路径实现 https://github.com/siyuan-note/siyuan/issues/7569
  240. installDirLower := strings.ToLower(filepath.Dir(util.WorkingDir))
  241. pathLower := strings.ToLower(path)
  242. if strings.HasPrefix(pathLower, installDirLower) && util.IsSubPath(installDirLower, pathLower) {
  243. ret.Code = -1
  244. ret.Msg = model.Conf.Language(98)
  245. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  246. return
  247. }
  248. }
  249. workspacePaths, err := util.ReadWorkspacePaths()
  250. if nil != err {
  251. ret.Code = -1
  252. ret.Msg = err.Error()
  253. return
  254. }
  255. workspacePaths = append(workspacePaths, path)
  256. workspacePaths = gulu.Str.RemoveDuplicatedElem(workspacePaths)
  257. workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
  258. workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
  259. if err = util.WriteWorkspacePaths(workspacePaths); nil != err {
  260. ret.Code = -1
  261. ret.Msg = err.Error()
  262. return
  263. }
  264. if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
  265. util.PushMsg(model.Conf.Language(42), 1000*15)
  266. time.Sleep(time.Second * 1)
  267. model.Close(false, false, 1)
  268. time.Sleep(time.Second * 1)
  269. }
  270. }
  271. func isInvalidWorkspacePath(absPath string) bool {
  272. if "" == absPath {
  273. return true
  274. }
  275. name := filepath.Base(absPath)
  276. if "" == name {
  277. return true
  278. }
  279. if strings.HasPrefix(name, ".") {
  280. return true
  281. }
  282. if !gulu.File.IsValidFilename(name) {
  283. return true
  284. }
  285. if 32 < utf8.RuneCountInString(name) {
  286. // Adjust workspace name length limit to 32 runes https://github.com/siyuan-note/siyuan/issues/9440
  287. return true
  288. }
  289. toLower := strings.ToLower(name)
  290. return gulu.Str.Contains(toLower, []string{"conf", "home", "data", "temp"})
  291. }