asset.go 5.8 KB

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