asset.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. "strings"
  23. "github.com/88250/go-humanize"
  24. "github.com/88250/gulu"
  25. "github.com/djherbis/times"
  26. "github.com/gin-gonic/gin"
  27. "github.com/siyuan-note/filelock"
  28. "github.com/siyuan-note/siyuan/kernel/model"
  29. "github.com/siyuan-note/siyuan/kernel/util"
  30. )
  31. func statAsset(c *gin.Context) {
  32. ret := gulu.Ret.NewResult()
  33. defer c.JSON(http.StatusOK, ret)
  34. arg, ok := util.JsonArg(c, ret)
  35. if !ok {
  36. return
  37. }
  38. path := arg["path"].(string)
  39. var p string
  40. if strings.HasPrefix(path, "assets/") {
  41. var err error
  42. p, err = model.GetAssetAbsPath(path)
  43. if nil != err {
  44. ret.Code = 1
  45. return
  46. }
  47. } else if strings.HasPrefix(path, "file://") {
  48. p = strings.TrimPrefix(path, "file://")
  49. } else {
  50. ret.Code = 1
  51. return
  52. }
  53. info, err := os.Stat(p)
  54. if nil != err {
  55. ret.Code = 1
  56. return
  57. }
  58. t, err := times.Stat(p)
  59. if nil != err {
  60. ret.Code = 1
  61. return
  62. }
  63. updated := t.ModTime().UnixMilli()
  64. hUpdated := t.ModTime().Format("2006-01-02 15:04:05")
  65. created := updated
  66. hCreated := hUpdated
  67. // Check birthtime before use
  68. if t.HasBirthTime() {
  69. created = t.BirthTime().UnixMilli()
  70. hCreated = t.BirthTime().Format("2006-01-02 15:04:05")
  71. }
  72. ret.Data = map[string]interface{}{
  73. "size": info.Size(),
  74. "hSize": humanize.Bytes(uint64(info.Size())),
  75. "created": created,
  76. "hCreated": hCreated,
  77. "updated": updated,
  78. "hUpdated": hUpdated,
  79. }
  80. }
  81. func fullReindexAssetContent(c *gin.Context) {
  82. ret := gulu.Ret.NewResult()
  83. defer c.JSON(http.StatusOK, ret)
  84. model.ReindexAssetContent()
  85. }
  86. func getImageOCRText(c *gin.Context) {
  87. ret := gulu.Ret.NewResult()
  88. defer c.JSON(http.StatusOK, ret)
  89. arg, ok := util.JsonArg(c, ret)
  90. if !ok {
  91. return
  92. }
  93. path := arg["path"].(string)
  94. force := false
  95. if forceArg := arg["force"]; nil != forceArg {
  96. force = forceArg.(bool)
  97. }
  98. ret.Data = map[string]interface{}{
  99. "text": util.GetAssetText(path, force),
  100. }
  101. }
  102. func setImageOCRText(c *gin.Context) {
  103. ret := gulu.Ret.NewResult()
  104. defer c.JSON(http.StatusOK, ret)
  105. arg, ok := util.JsonArg(c, ret)
  106. if !ok {
  107. return
  108. }
  109. path := arg["path"].(string)
  110. text := arg["text"].(string)
  111. util.SetAssetText(path, text)
  112. }
  113. func renameAsset(c *gin.Context) {
  114. ret := gulu.Ret.NewResult()
  115. defer c.JSON(http.StatusOK, ret)
  116. arg, ok := util.JsonArg(c, ret)
  117. if !ok {
  118. return
  119. }
  120. oldPath := arg["oldPath"].(string)
  121. newName := arg["newName"].(string)
  122. err := model.RenameAsset(oldPath, newName)
  123. if nil != err {
  124. ret.Code = -1
  125. ret.Msg = err.Error()
  126. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  127. return
  128. }
  129. }
  130. func getDocImageAssets(c *gin.Context) {
  131. ret := gulu.Ret.NewResult()
  132. defer c.JSON(http.StatusOK, ret)
  133. arg, ok := util.JsonArg(c, ret)
  134. if !ok {
  135. return
  136. }
  137. id := arg["id"].(string)
  138. assets, err := model.DocImageAssets(id)
  139. if nil != err {
  140. ret.Code = -1
  141. ret.Msg = err.Error()
  142. return
  143. }
  144. ret.Data = assets
  145. }
  146. func setFileAnnotation(c *gin.Context) {
  147. ret := gulu.Ret.NewResult()
  148. defer c.JSON(http.StatusOK, ret)
  149. arg, ok := util.JsonArg(c, ret)
  150. if !ok {
  151. return
  152. }
  153. p := arg["path"].(string)
  154. p = strings.ReplaceAll(p, "%23", "#")
  155. data := arg["data"].(string)
  156. writePath, err := resolveFileAnnotationAbsPath(p)
  157. if nil != err {
  158. ret.Code = -1
  159. ret.Msg = err.Error()
  160. return
  161. }
  162. if err := filelock.WriteFile(writePath, []byte(data)); nil != err {
  163. ret.Code = -1
  164. ret.Msg = err.Error()
  165. return
  166. }
  167. model.IncSync()
  168. }
  169. func getFileAnnotation(c *gin.Context) {
  170. ret := gulu.Ret.NewResult()
  171. defer c.JSON(http.StatusOK, ret)
  172. arg, ok := util.JsonArg(c, ret)
  173. if !ok {
  174. return
  175. }
  176. p := arg["path"].(string)
  177. p = strings.ReplaceAll(p, "%23", "#")
  178. readPath, err := resolveFileAnnotationAbsPath(p)
  179. if nil != err {
  180. ret.Code = -1
  181. ret.Msg = err.Error()
  182. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  183. return
  184. }
  185. if !filelock.IsExist(readPath) {
  186. ret.Code = 1
  187. return
  188. }
  189. data, err := filelock.ReadFile(readPath)
  190. if nil != err {
  191. ret.Code = -1
  192. ret.Msg = err.Error()
  193. return
  194. }
  195. ret.Data = map[string]interface{}{
  196. "data": string(data),
  197. }
  198. }
  199. func resolveFileAnnotationAbsPath(assetRelPath string) (ret string, err error) {
  200. filePath := strings.TrimSuffix(assetRelPath, ".sya")
  201. absPath, err := model.GetAssetAbsPath(filePath)
  202. if nil != err {
  203. return
  204. }
  205. dir := filepath.Dir(absPath)
  206. base := filepath.Base(assetRelPath)
  207. ret = filepath.Join(dir, base)
  208. return
  209. }
  210. func removeUnusedAsset(c *gin.Context) {
  211. ret := gulu.Ret.NewResult()
  212. defer c.JSON(http.StatusOK, ret)
  213. arg, ok := util.JsonArg(c, ret)
  214. if !ok {
  215. return
  216. }
  217. p := arg["path"].(string)
  218. asset := model.RemoveUnusedAsset(p)
  219. ret.Data = map[string]interface{}{
  220. "path": asset,
  221. }
  222. }
  223. func removeUnusedAssets(c *gin.Context) {
  224. ret := gulu.Ret.NewResult()
  225. defer c.JSON(http.StatusOK, ret)
  226. paths := model.RemoveUnusedAssets()
  227. ret.Data = map[string]interface{}{
  228. "paths": paths,
  229. }
  230. }
  231. func getUnusedAssets(c *gin.Context) {
  232. ret := gulu.Ret.NewResult()
  233. defer c.JSON(http.StatusOK, ret)
  234. unusedAssets := model.UnusedAssets()
  235. ret.Data = map[string]interface{}{
  236. "unusedAssets": unusedAssets,
  237. }
  238. }
  239. func getMissingAssets(c *gin.Context) {
  240. ret := gulu.Ret.NewResult()
  241. defer c.JSON(http.StatusOK, ret)
  242. missingAssets := model.MissingAssets()
  243. ret.Data = map[string]interface{}{
  244. "missingAssets": missingAssets,
  245. }
  246. }
  247. func resolveAssetPath(c *gin.Context) {
  248. ret := gulu.Ret.NewResult()
  249. defer c.JSON(http.StatusOK, ret)
  250. arg, ok := util.JsonArg(c, ret)
  251. if !ok {
  252. return
  253. }
  254. path := arg["path"].(string)
  255. p, err := model.GetAssetAbsPath(path)
  256. if nil != err {
  257. ret.Code = -1
  258. ret.Msg = err.Error()
  259. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  260. return
  261. }
  262. ret.Data = p
  263. return
  264. }
  265. func uploadCloud(c *gin.Context) {
  266. ret := gulu.Ret.NewResult()
  267. defer c.JSON(http.StatusOK, ret)
  268. arg, ok := util.JsonArg(c, ret)
  269. if !ok {
  270. return
  271. }
  272. rootID := arg["id"].(string)
  273. count, err := model.UploadAssets2Cloud(rootID)
  274. if nil != err {
  275. ret.Code = -1
  276. ret.Msg = err.Error()
  277. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  278. return
  279. }
  280. util.PushMsg(fmt.Sprintf(model.Conf.Language(41), count), 3000)
  281. }
  282. func insertLocalAssets(c *gin.Context) {
  283. ret := gulu.Ret.NewResult()
  284. defer c.JSON(http.StatusOK, ret)
  285. arg, ok := util.JsonArg(c, ret)
  286. if !ok {
  287. return
  288. }
  289. assetPathsArg := arg["assetPaths"].([]interface{})
  290. var assetPaths []string
  291. for _, pathArg := range assetPathsArg {
  292. assetPaths = append(assetPaths, pathArg.(string))
  293. }
  294. isUpload := true
  295. isUploadArg := arg["isUpload"]
  296. if nil != isUploadArg {
  297. isUpload = isUploadArg.(bool)
  298. }
  299. id := arg["id"].(string)
  300. succMap, err := model.InsertLocalAssets(id, assetPaths, isUpload)
  301. if nil != err {
  302. ret.Code = -1
  303. ret.Msg = err.Error()
  304. return
  305. }
  306. ret.Data = map[string]interface{}{
  307. "succMap": succMap,
  308. }
  309. }