asset.go 5.0 KB

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