From 4e2d8b779827627da6efccfc08f75a6a9d2e20d7 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Tue, 18 Oct 2022 22:36:20 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E6=B8=85=E7=90=86=E8=B5=84=E6=BA=90?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8E=E5=86=8D=E6=AC=A1=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=9B=B8=E5=90=8C=E6=96=87=E4=BB=B6=E5=BC=82=E5=B8=B8=20Fix=20?= =?UTF-8?q?https://github.com/siyuan-note/siyuan/issues/6260?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/assets.go | 15 +++++++++++++-- kernel/sql/aseet.go | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/kernel/model/assets.go b/kernel/model/assets.go index 1f6fdb148..2246ee79d 100644 --- a/kernel/model/assets.go +++ b/kernel/model/assets.go @@ -405,15 +405,21 @@ func RemoveUnusedAssets() (ret []string) { return } + var hashes []string for _, p := range unusedAssets { historyPath := filepath.Join(historyDir, p) if p = filepath.Join(util.DataDir, p); gulu.File.IsExist(p) { if err = gulu.File.Copy(p, historyPath); nil != err { return } + + hash, _ := util.GetEtag(p) + hashes = append(hashes, hash) } } + sql.DeleteAssetsByHashes(hashes) + for _, unusedAsset := range unusedAssets { if unusedAsset = filepath.Join(util.DataDir, unusedAsset); gulu.File.IsExist(unusedAsset) { if err := os.RemoveAll(unusedAsset); nil != err { @@ -444,8 +450,13 @@ func RemoveUnusedAsset(p string) (ret string) { newP := strings.TrimPrefix(p, util.DataDir) historyPath := filepath.Join(historyDir, newP) - if err = gulu.File.Copy(p, historyPath); nil != err { - return + if gulu.File.IsExist(p) { + if err = gulu.File.Copy(p, historyPath); nil != err { + return + } + + hash, _ := util.GetEtag(p) + sql.DeleteAssetsByHashes([]string{hash}) } if err = os.RemoveAll(p); nil != err { diff --git a/kernel/sql/aseet.go b/kernel/sql/aseet.go index 33e33e0a8..e83c17111 100644 --- a/kernel/sql/aseet.go +++ b/kernel/sql/aseet.go @@ -17,9 +17,7 @@ package sql import ( - "crypto/sha256" "database/sql" - "fmt" "path/filepath" "strings" @@ -75,7 +73,7 @@ func docTitleImgAsset(root *ast.Node) *Asset { absPath := filepath.Join(util.DataDir, p) if hash, err = util.GetEtag(absPath); nil != err { logging.LogErrorf("read asset [%s] data failed: %s", absPath, err) - hash = fmt.Sprintf("%x", sha256.Sum256([]byte(gulu.Rand.String(7)))) + return nil } name, _ := util.LastID(p) asset := &Asset{ @@ -94,6 +92,17 @@ func docTitleImgAsset(root *ast.Node) *Asset { return nil } +func DeleteAssetsByHashes(hashes []string) { + sqlStmt := "DELETE FROM assets WHERE hash IN ('" + strings.Join(hashes, "','") + "') OR hash = ''" + tx, err := BeginTx() + if nil != err { + return + } + execStmtTx(tx, sqlStmt) + + CommitTx(tx) +} + func QueryAssetByHash(hash string) (ret *Asset) { sqlStmt := "SELECT * FROM assets WHERE hash = ?" row := queryRow(sqlStmt, hash)