asset.go 6.2 KB

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