🐛 清理资源文件后再次上传相同文件异常 Fix https://github.com/siyuan-note/siyuan/issues/6260

This commit is contained in:
Liang Ding 2022-10-18 22:36:20 +08:00
parent 4e7c898273
commit 4e2d8b7798
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
2 changed files with 25 additions and 5 deletions

View file

@ -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)