asset.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. "net/http"
  19. "os"
  20. "path/filepath"
  21. "strings"
  22. "github.com/88250/gulu"
  23. "github.com/gin-gonic/gin"
  24. "github.com/siyuan-note/siyuan/kernel/filesys"
  25. "github.com/siyuan-note/siyuan/kernel/model"
  26. "github.com/siyuan-note/siyuan/kernel/util"
  27. )
  28. func renameAsset(c *gin.Context) {
  29. ret := gulu.Ret.NewResult()
  30. defer c.JSON(http.StatusOK, ret)
  31. arg, ok := util.JsonArg(c, ret)
  32. if !ok {
  33. return
  34. }
  35. oldPath := arg["oldPath"].(string)
  36. newName := arg["newName"].(string)
  37. err := model.RenameAsset(oldPath, newName)
  38. if nil != err {
  39. ret.Code = -1
  40. ret.Msg = err.Error()
  41. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  42. return
  43. }
  44. }
  45. func getDocImageAssets(c *gin.Context) {
  46. ret := gulu.Ret.NewResult()
  47. defer c.JSON(http.StatusOK, ret)
  48. arg, ok := util.JsonArg(c, ret)
  49. if !ok {
  50. return
  51. }
  52. id := arg["id"].(string)
  53. assets, err := model.DocImageAssets(id)
  54. if nil != err {
  55. ret.Code = -1
  56. ret.Msg = err.Error()
  57. return
  58. }
  59. ret.Data = assets
  60. }
  61. func setFileAnnotation(c *gin.Context) {
  62. ret := gulu.Ret.NewResult()
  63. defer c.JSON(http.StatusOK, ret)
  64. arg, ok := util.JsonArg(c, ret)
  65. if !ok {
  66. return
  67. }
  68. p := arg["path"].(string)
  69. p = strings.ReplaceAll(p, "%23", "#")
  70. data := arg["data"].(string)
  71. writePath, err := resolveFileAnnotationAbsPath(p)
  72. if nil != err {
  73. ret.Code = -1
  74. ret.Msg = err.Error()
  75. return
  76. }
  77. if err := filesys.WriteFileSafer(writePath, []byte(data)); nil != err {
  78. ret.Code = -1
  79. ret.Msg = err.Error()
  80. return
  81. }
  82. model.IncSync()
  83. }
  84. func getFileAnnotation(c *gin.Context) {
  85. ret := gulu.Ret.NewResult()
  86. defer c.JSON(http.StatusOK, ret)
  87. arg, ok := util.JsonArg(c, ret)
  88. if !ok {
  89. return
  90. }
  91. p := arg["path"].(string)
  92. p = strings.ReplaceAll(p, "%23", "#")
  93. readPath, err := resolveFileAnnotationAbsPath(p)
  94. if nil != err {
  95. ret.Code = -1
  96. ret.Msg = err.Error()
  97. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  98. return
  99. }
  100. if !gulu.File.IsExist(readPath) {
  101. ret.Code = 1
  102. return
  103. }
  104. data, err := os.ReadFile(readPath)
  105. if nil != err {
  106. ret.Code = -1
  107. ret.Msg = err.Error()
  108. return
  109. }
  110. ret.Data = map[string]interface{}{
  111. "data": string(data),
  112. }
  113. }
  114. func resolveFileAnnotationAbsPath(assetRelPath string) (ret string, err error) {
  115. filePath := strings.TrimSuffix(assetRelPath, ".sya")
  116. absPath, err := model.GetAssetAbsPath(filePath)
  117. if nil != err {
  118. return
  119. }
  120. dir := filepath.Dir(absPath)
  121. base := filepath.Base(assetRelPath)
  122. ret = filepath.Join(dir, base)
  123. return
  124. }
  125. func removeUnusedAsset(c *gin.Context) {
  126. ret := gulu.Ret.NewResult()
  127. defer c.JSON(http.StatusOK, ret)
  128. arg, ok := util.JsonArg(c, ret)
  129. if !ok {
  130. return
  131. }
  132. p := arg["path"].(string)
  133. asset := model.RemoveUnusedAsset(p)
  134. ret.Data = map[string]interface{}{
  135. "path": asset,
  136. }
  137. }
  138. func removeUnusedAssets(c *gin.Context) {
  139. ret := gulu.Ret.NewResult()
  140. defer c.JSON(http.StatusOK, ret)
  141. paths := model.RemoveUnusedAssets()
  142. ret.Data = map[string]interface{}{
  143. "paths": paths,
  144. }
  145. }
  146. func getUnusedAssets(c *gin.Context) {
  147. ret := gulu.Ret.NewResult()
  148. defer c.JSON(http.StatusOK, ret)
  149. unusedAssets := model.UnusedAssets()
  150. ret.Data = map[string]interface{}{
  151. "unusedAssets": unusedAssets,
  152. }
  153. }
  154. func resolveAssetPath(c *gin.Context) {
  155. ret := gulu.Ret.NewResult()
  156. defer c.JSON(http.StatusOK, ret)
  157. arg, ok := util.JsonArg(c, ret)
  158. if !ok {
  159. return
  160. }
  161. path := arg["path"].(string)
  162. p, err := model.GetAssetAbsPath(path)
  163. if nil != err {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  167. return
  168. }
  169. ret.Data = p
  170. return
  171. }
  172. func uploadCloud(c *gin.Context) {
  173. ret := gulu.Ret.NewResult()
  174. defer c.JSON(http.StatusOK, ret)
  175. arg, ok := util.JsonArg(c, ret)
  176. if !ok {
  177. return
  178. }
  179. rootID := arg["id"].(string)
  180. err := model.UploadAssets2Cloud(rootID)
  181. if nil != err {
  182. ret.Code = -1
  183. ret.Msg = err.Error()
  184. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  185. } else {
  186. util.PushMsg(model.Conf.Language(41), 3000)
  187. }
  188. }
  189. func insertLocalAssets(c *gin.Context) {
  190. ret := gulu.Ret.NewResult()
  191. defer c.JSON(http.StatusOK, ret)
  192. arg, ok := util.JsonArg(c, ret)
  193. if !ok {
  194. return
  195. }
  196. assetPathsArg := arg["assetPaths"].([]interface{})
  197. var assetPaths []string
  198. for _, pathArg := range assetPathsArg {
  199. assetPaths = append(assetPaths, pathArg.(string))
  200. }
  201. isUpload := true
  202. isUploadArg := arg["isUpload"]
  203. if nil != isUploadArg {
  204. isUpload = isUploadArg.(bool)
  205. }
  206. id := arg["id"].(string)
  207. succMap, err := model.InsertLocalAssets(id, assetPaths, isUpload)
  208. if nil != err {
  209. ret.Code = -1
  210. ret.Msg = err.Error()
  211. return
  212. }
  213. ret.Data = map[string]interface{}{
  214. "succMap": succMap,
  215. }
  216. }