mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🐛 清理资源文件后再次上传相同文件异常 Fix https://github.com/siyuan-note/siyuan/issues/6260
This commit is contained in:
parent
4e7c898273
commit
4e2d8b7798
2 changed files with 25 additions and 5 deletions
|
|
@ -405,15 +405,21 @@ func RemoveUnusedAssets() (ret []string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hashes []string
|
||||||
for _, p := range unusedAssets {
|
for _, p := range unusedAssets {
|
||||||
historyPath := filepath.Join(historyDir, p)
|
historyPath := filepath.Join(historyDir, p)
|
||||||
if p = filepath.Join(util.DataDir, p); gulu.File.IsExist(p) {
|
if p = filepath.Join(util.DataDir, p); gulu.File.IsExist(p) {
|
||||||
if err = gulu.File.Copy(p, historyPath); nil != err {
|
if err = gulu.File.Copy(p, historyPath); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hash, _ := util.GetEtag(p)
|
||||||
|
hashes = append(hashes, hash)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sql.DeleteAssetsByHashes(hashes)
|
||||||
|
|
||||||
for _, unusedAsset := range unusedAssets {
|
for _, unusedAsset := range unusedAssets {
|
||||||
if unusedAsset = filepath.Join(util.DataDir, unusedAsset); gulu.File.IsExist(unusedAsset) {
|
if unusedAsset = filepath.Join(util.DataDir, unusedAsset); gulu.File.IsExist(unusedAsset) {
|
||||||
if err := os.RemoveAll(unusedAsset); nil != err {
|
if err := os.RemoveAll(unusedAsset); nil != err {
|
||||||
|
|
@ -444,8 +450,13 @@ func RemoveUnusedAsset(p string) (ret string) {
|
||||||
|
|
||||||
newP := strings.TrimPrefix(p, util.DataDir)
|
newP := strings.TrimPrefix(p, util.DataDir)
|
||||||
historyPath := filepath.Join(historyDir, newP)
|
historyPath := filepath.Join(historyDir, newP)
|
||||||
if err = gulu.File.Copy(p, historyPath); nil != err {
|
if gulu.File.IsExist(p) {
|
||||||
return
|
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 {
|
if err = os.RemoveAll(p); nil != err {
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,7 @@
|
||||||
package sql
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -75,7 +73,7 @@ func docTitleImgAsset(root *ast.Node) *Asset {
|
||||||
absPath := filepath.Join(util.DataDir, p)
|
absPath := filepath.Join(util.DataDir, p)
|
||||||
if hash, err = util.GetEtag(absPath); nil != err {
|
if hash, err = util.GetEtag(absPath); nil != err {
|
||||||
logging.LogErrorf("read asset [%s] data failed: %s", absPath, 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)
|
name, _ := util.LastID(p)
|
||||||
asset := &Asset{
|
asset := &Asset{
|
||||||
|
|
@ -94,6 +92,17 @@ func docTitleImgAsset(root *ast.Node) *Asset {
|
||||||
return nil
|
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) {
|
func QueryAssetByHash(hash string) (ret *Asset) {
|
||||||
sqlStmt := "SELECT * FROM assets WHERE hash = ?"
|
sqlStmt := "SELECT * FROM assets WHERE hash = ?"
|
||||||
row := queryRow(sqlStmt, hash)
|
row := queryRow(sqlStmt, hash)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue