asset.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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.BytesCustomCeil(uint64(info.Size()), 2),
  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. ret.Data = map[string]interface{}{
  95. "text": util.GetAssetText(path),
  96. }
  97. }
  98. func setImageOCRText(c *gin.Context) {
  99. ret := gulu.Ret.NewResult()
  100. defer c.JSON(http.StatusOK, ret)
  101. arg, ok := util.JsonArg(c, ret)
  102. if !ok {
  103. return
  104. }
  105. path := arg["path"].(string)
  106. text := arg["text"].(string)
  107. util.SetAssetText(path, text)
  108. }
  109. func ocr(c *gin.Context) {
  110. ret := gulu.Ret.NewResult()
  111. defer c.JSON(http.StatusOK, ret)
  112. arg, ok := util.JsonArg(c, ret)
  113. if !ok {
  114. return
  115. }
  116. path := arg["path"].(string)
  117. ocrJSON := util.OcrAsset(path)
  118. ret.Data = map[string]interface{}{
  119. "text": util.GetOcrJsonText(ocrJSON),
  120. "ocrJSON": ocrJSON,
  121. }
  122. }
  123. func renameAsset(c *gin.Context) {
  124. ret := gulu.Ret.NewResult()
  125. defer c.JSON(http.StatusOK, ret)
  126. arg, ok := util.JsonArg(c, ret)
  127. if !ok {
  128. return
  129. }
  130. oldPath := arg["oldPath"].(string)
  131. newName := arg["newName"].(string)
  132. err := model.RenameAsset(oldPath, newName)
  133. if nil != err {
  134. ret.Code = -1
  135. ret.Msg = err.Error()
  136. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  137. return
  138. }
  139. }
  140. func getDocImageAssets(c *gin.Context) {
  141. ret := gulu.Ret.NewResult()
  142. defer c.JSON(http.StatusOK, ret)
  143. arg, ok := util.JsonArg(c, ret)
  144. if !ok {
  145. return
  146. }
  147. id := arg["id"].(string)
  148. assets, err := model.DocImageAssets(id)
  149. if nil != err {
  150. ret.Code = -1
  151. ret.Msg = err.Error()
  152. return
  153. }
  154. ret.Data = assets
  155. }
  156. func setFileAnnotation(c *gin.Context) {
  157. ret := gulu.Ret.NewResult()
  158. defer c.JSON(http.StatusOK, ret)
  159. arg, ok := util.JsonArg(c, ret)
  160. if !ok {
  161. return
  162. }
  163. p := arg["path"].(string)
  164. p = strings.ReplaceAll(p, "%23", "#")
  165. data := arg["data"].(string)
  166. writePath, err := resolveFileAnnotationAbsPath(p)
  167. if nil != err {
  168. ret.Code = -1
  169. ret.Msg = err.Error()
  170. return
  171. }
  172. if err := filelock.WriteFile(writePath, []byte(data)); nil != err {
  173. ret.Code = -1
  174. ret.Msg = err.Error()
  175. return
  176. }
  177. model.IncSync()
  178. }
  179. func getFileAnnotation(c *gin.Context) {
  180. ret := gulu.Ret.NewResult()
  181. defer c.JSON(http.StatusOK, ret)
  182. arg, ok := util.JsonArg(c, ret)
  183. if !ok {
  184. return
  185. }
  186. p := arg["path"].(string)
  187. p = strings.ReplaceAll(p, "%23", "#")
  188. readPath, err := resolveFileAnnotationAbsPath(p)
  189. if nil != err {
  190. ret.Code = -1
  191. ret.Msg = err.Error()
  192. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  193. return
  194. }
  195. if !filelock.IsExist(readPath) {
  196. ret.Code = 1
  197. return
  198. }
  199. data, err := filelock.ReadFile(readPath)
  200. if nil != err {
  201. ret.Code = -1
  202. ret.Msg = err.Error()
  203. return
  204. }
  205. ret.Data = map[string]interface{}{
  206. "data": string(data),
  207. }
  208. }
  209. func resolveFileAnnotationAbsPath(assetRelPath string) (ret string, err error) {
  210. filePath := strings.TrimSuffix(assetRelPath, ".sya")
  211. absPath, err := model.GetAssetAbsPath(filePath)
  212. if nil != err {
  213. return
  214. }
  215. dir := filepath.Dir(absPath)
  216. base := filepath.Base(assetRelPath)
  217. ret = filepath.Join(dir, base)
  218. return
  219. }
  220. func removeUnusedAsset(c *gin.Context) {
  221. ret := gulu.Ret.NewResult()
  222. defer c.JSON(http.StatusOK, ret)
  223. arg, ok := util.JsonArg(c, ret)
  224. if !ok {
  225. return
  226. }
  227. p := arg["path"].(string)
  228. asset := model.RemoveUnusedAsset(p)
  229. ret.Data = map[string]interface{}{
  230. "path": asset,
  231. }
  232. }
  233. func removeUnusedAssets(c *gin.Context) {
  234. ret := gulu.Ret.NewResult()
  235. defer c.JSON(http.StatusOK, ret)
  236. paths := model.RemoveUnusedAssets()
  237. ret.Data = map[string]interface{}{
  238. "paths": paths,
  239. }
  240. }
  241. func getUnusedAssets(c *gin.Context) {
  242. ret := gulu.Ret.NewResult()
  243. defer c.JSON(http.StatusOK, ret)
  244. unusedAssets := model.UnusedAssets()
  245. ret.Data = map[string]interface{}{
  246. "unusedAssets": unusedAssets,
  247. }
  248. }
  249. func getMissingAssets(c *gin.Context) {
  250. ret := gulu.Ret.NewResult()
  251. defer c.JSON(http.StatusOK, ret)
  252. missingAssets := model.MissingAssets()
  253. ret.Data = map[string]interface{}{
  254. "missingAssets": missingAssets,
  255. }
  256. }
  257. func resolveAssetPath(c *gin.Context) {
  258. ret := gulu.Ret.NewResult()
  259. defer c.JSON(http.StatusOK, ret)
  260. arg, ok := util.JsonArg(c, ret)
  261. if !ok {
  262. return
  263. }
  264. path := arg["path"].(string)
  265. p, err := model.GetAssetAbsPath(path)
  266. if nil != err {
  267. ret.Code = -1
  268. ret.Msg = err.Error()
  269. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  270. return
  271. }
  272. ret.Data = p
  273. return
  274. }
  275. func uploadCloud(c *gin.Context) {
  276. ret := gulu.Ret.NewResult()
  277. defer c.JSON(http.StatusOK, ret)
  278. arg, ok := util.JsonArg(c, ret)
  279. if !ok {
  280. return
  281. }
  282. rootID := arg["id"].(string)
  283. count, err := model.UploadAssets2Cloud(rootID)
  284. if nil != err {
  285. ret.Code = -1
  286. ret.Msg = err.Error()
  287. ret.Data = map[string]interface{}{"closeTimeout": 3000}
  288. return
  289. }
  290. util.PushMsg(fmt.Sprintf(model.Conf.Language(41), count), 3000)
  291. }
  292. func insertLocalAssets(c *gin.Context) {
  293. ret := gulu.Ret.NewResult()
  294. defer c.JSON(http.StatusOK, ret)
  295. arg, ok := util.JsonArg(c, ret)
  296. if !ok {
  297. return
  298. }
  299. assetPathsArg := arg["assetPaths"].([]interface{})
  300. var assetPaths []string
  301. for _, pathArg := range assetPathsArg {
  302. assetPaths = append(assetPaths, pathArg.(string))
  303. }
  304. isUpload := true
  305. isUploadArg := arg["isUpload"]
  306. if nil != isUploadArg {
  307. isUpload = isUploadArg.(bool)
  308. }
  309. id := arg["id"].(string)
  310. succMap, err := model.InsertLocalAssets(id, assetPaths, isUpload)
  311. if nil != err {
  312. ret.Code = -1
  313. ret.Msg = err.Error()
  314. return
  315. }
  316. ret.Data = map[string]interface{}{
  317. "succMap": succMap,
  318. }
  319. }