mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-30 13:28:48 +01:00
🎨 OCR 未启用时不保存 ocr-texts.json https://github.com/siyuan-note/siyuan/issues/11171
This commit is contained in:
parent
1e53010b4d
commit
af694f6301
5 changed files with 114 additions and 120 deletions
|
|
@ -2,13 +2,9 @@ package model
|
|||
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/88250/go-humanize"
|
||||
"github.com/88250/gulu"
|
||||
"github.com/siyuan-note/filelock"
|
||||
"github.com/siyuan-note/logging"
|
||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||
|
|
@ -40,19 +36,14 @@ func autoOCRAssets() {
|
|||
text := util.Tesseract(assetAbsPath)
|
||||
p := strings.TrimPrefix(assetAbsPath, assetsPath)
|
||||
p = "assets" + filepath.ToSlash(p)
|
||||
util.AssetsTextsLock.Lock()
|
||||
util.AssetsTexts[p] = text
|
||||
util.AssetsTextsLock.Unlock()
|
||||
if "" != text {
|
||||
util.AssetsTextsChanged.Store(true)
|
||||
}
|
||||
util.SetAssetText(p, text)
|
||||
if 7 <= i { // 一次任务中最多处理 7 张图片,防止长时间占用系统资源
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cleanNotExistAssetsTexts()
|
||||
util.CleanNotExistAssetsTexts()
|
||||
|
||||
// 刷新 OCR 结果到数据库
|
||||
util.NodeOCRQueueLock.Lock()
|
||||
|
|
@ -63,27 +54,6 @@ func autoOCRAssets() {
|
|||
util.NodeOCRQueue = nil
|
||||
}
|
||||
|
||||
func cleanNotExistAssetsTexts() {
|
||||
util.AssetsTextsLock.Lock()
|
||||
defer util.AssetsTextsLock.Unlock()
|
||||
|
||||
assetsPath := util.GetDataAssetsAbsPath()
|
||||
var toRemoves []string
|
||||
for asset, _ := range util.AssetsTexts {
|
||||
assetAbsPath := strings.TrimPrefix(asset, "assets")
|
||||
assetAbsPath = filepath.Join(assetsPath, assetAbsPath)
|
||||
if !filelock.IsExist(assetAbsPath) {
|
||||
toRemoves = append(toRemoves, asset)
|
||||
}
|
||||
}
|
||||
|
||||
for _, asset := range toRemoves {
|
||||
delete(util.AssetsTexts, asset)
|
||||
util.AssetsTextsChanged.Store(true)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func getUnOCRAssetsAbsPaths() (ret []string) {
|
||||
var assetsPaths []string
|
||||
assets := cache.GetAssets()
|
||||
|
|
@ -95,9 +65,8 @@ func getUnOCRAssetsAbsPaths() (ret []string) {
|
|||
}
|
||||
|
||||
assetsPath := util.GetDataAssetsAbsPath()
|
||||
assetsTextsTmp := util.AssetsTexts
|
||||
for _, assetPath := range assetsPaths {
|
||||
if _, ok := assetsTextsTmp[assetPath]; ok {
|
||||
if util.ExistsAssetText(assetPath) {
|
||||
continue
|
||||
}
|
||||
absPath := filepath.Join(assetsPath, strings.TrimPrefix(assetPath, "assets"))
|
||||
|
|
@ -107,66 +76,5 @@ func getUnOCRAssetsAbsPaths() (ret []string) {
|
|||
}
|
||||
|
||||
func FlushAssetsTextsJob() {
|
||||
SaveAssetsTexts()
|
||||
}
|
||||
|
||||
func LoadAssetsTexts() {
|
||||
assetsPath := util.GetDataAssetsAbsPath()
|
||||
assetsTextsPath := filepath.Join(assetsPath, "ocr-texts.json")
|
||||
if !filelock.IsExist(assetsTextsPath) {
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
data, err := filelock.ReadFile(assetsTextsPath)
|
||||
if nil != err {
|
||||
logging.LogErrorf("read assets texts failed: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
util.AssetsTextsLock.Lock()
|
||||
if err = gulu.JSON.UnmarshalJSON(data, &util.AssetsTexts); nil != err {
|
||||
logging.LogErrorf("unmarshal assets texts failed: %s", err)
|
||||
if err = filelock.Remove(assetsTextsPath); nil != err {
|
||||
logging.LogErrorf("removed corrupted assets texts failed: %s", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
util.AssetsTextsLock.Unlock()
|
||||
debug.FreeOSMemory()
|
||||
|
||||
if elapsed := time.Since(start).Seconds(); 2 < elapsed {
|
||||
logging.LogWarnf("read assets texts [%s] to [%s], elapsed [%.2fs]", humanize.BytesCustomCeil(uint64(len(data)), 2), assetsTextsPath, elapsed)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SaveAssetsTexts() {
|
||||
if !util.AssetsTextsChanged.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
util.AssetsTextsLock.Lock()
|
||||
data, err := gulu.JSON.MarshalIndentJSON(util.AssetsTexts, "", " ")
|
||||
if nil != err {
|
||||
logging.LogErrorf("marshal assets texts failed: %s", err)
|
||||
return
|
||||
}
|
||||
util.AssetsTextsLock.Unlock()
|
||||
|
||||
assetsPath := util.GetDataAssetsAbsPath()
|
||||
assetsTextsPath := filepath.Join(assetsPath, "ocr-texts.json")
|
||||
if err = filelock.WriteFile(assetsTextsPath, data); nil != err {
|
||||
logging.LogErrorf("write assets texts failed: %s", err)
|
||||
return
|
||||
}
|
||||
debug.FreeOSMemory()
|
||||
|
||||
if elapsed := time.Since(start).Seconds(); 2 < elapsed {
|
||||
logging.LogWarnf("save assets texts [size=%s] to [%s], elapsed [%.2fs]", humanize.BytesCustomCeil(uint64(len(data)), 2), assetsTextsPath, elapsed)
|
||||
}
|
||||
|
||||
util.AssetsTextsChanged.Store(false)
|
||||
util.SaveAssetsTexts()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue