asset.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 sql
  17. import (
  18. "database/sql"
  19. "path/filepath"
  20. "strings"
  21. "github.com/siyuan-note/filelock"
  22. "github.com/88250/lute/ast"
  23. "github.com/siyuan-note/logging"
  24. "github.com/siyuan-note/siyuan/kernel/treenode"
  25. "github.com/siyuan-note/siyuan/kernel/util"
  26. )
  27. type Asset struct {
  28. ID string
  29. BlockID string
  30. RootID string
  31. Box string
  32. DocPath string
  33. Path string
  34. Name string
  35. Title string
  36. Hash string
  37. }
  38. func docTagSpans(n *ast.Node) (ret []*Span) {
  39. if tagsVal := n.IALAttr("tags"); "" != tagsVal {
  40. tags := strings.Split(tagsVal, ",")
  41. for _, tag := range tags {
  42. markdown := "#" + tag + "#"
  43. span := &Span{
  44. ID: ast.NewNodeID(),
  45. BlockID: n.ID,
  46. RootID: n.ID,
  47. Box: n.Box,
  48. Path: n.Path,
  49. Content: tag,
  50. Markdown: markdown,
  51. Type: "tag",
  52. IAL: "",
  53. }
  54. ret = append(ret, span)
  55. }
  56. }
  57. return
  58. }
  59. func docTitleImgAsset(root *ast.Node, boxLocalPath, docDirLocalPath string) *Asset {
  60. if p := treenode.GetDocTitleImgPath(root); "" != p {
  61. if !util.IsAssetLinkDest([]byte(p)) {
  62. return nil
  63. }
  64. var hash string
  65. var err error
  66. if lp := assetLocalPath(p, boxLocalPath, docDirLocalPath); "" != lp {
  67. hash, err = util.GetEtag(lp)
  68. if nil != err {
  69. logging.LogErrorf("calc asset [%s] hash failed: %s", lp, err)
  70. return nil
  71. }
  72. }
  73. name, _ := util.LastID(p)
  74. asset := &Asset{
  75. ID: ast.NewNodeID(),
  76. BlockID: root.ID,
  77. RootID: root.ID,
  78. Box: root.Box,
  79. DocPath: root.Path,
  80. Path: p,
  81. Name: name,
  82. Title: "title-img",
  83. Hash: hash,
  84. }
  85. return asset
  86. }
  87. return nil
  88. }
  89. func deleteAssetsByHashes(tx *sql.Tx, hashes []string) (err error) {
  90. sqlStmt := "DELETE FROM assets WHERE hash IN ('" + strings.Join(hashes, "','") + "') OR hash = ''"
  91. err = execStmtTx(tx, sqlStmt)
  92. return
  93. }
  94. func QueryAssetByHash(hash string) (ret *Asset) {
  95. sqlStmt := "SELECT * FROM assets WHERE hash = ?"
  96. row := queryRow(sqlStmt, hash)
  97. var asset Asset
  98. if err := row.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); nil != err {
  99. if sql.ErrNoRows != err {
  100. logging.LogErrorf("query scan field failed: %s", err)
  101. }
  102. return
  103. }
  104. ret = &asset
  105. return
  106. }
  107. func scanAssetRows(rows *sql.Rows) (ret *Asset) {
  108. var asset Asset
  109. if err := rows.Scan(&asset.ID, &asset.BlockID, &asset.RootID, &asset.Box, &asset.DocPath, &asset.Path, &asset.Name, &asset.Title, &asset.Hash); nil != err {
  110. logging.LogErrorf("query scan field failed: %s", err)
  111. return
  112. }
  113. ret = &asset
  114. return
  115. }
  116. func assetLocalPath(linkDest, boxLocalPath, docDirLocalPath string) (ret string) {
  117. ret = filepath.Join(docDirLocalPath, linkDest)
  118. if filelock.IsExist(ret) {
  119. return
  120. }
  121. ret = filepath.Join(boxLocalPath, linkDest)
  122. if filelock.IsExist(ret) {
  123. return
  124. }
  125. ret = filepath.Join(util.DataDir, linkDest)
  126. if filelock.IsExist(ret) {
  127. return
  128. }
  129. return ""
  130. }