From 15cd499f564f2fdc8f66786f0b6d874c11dd42e8 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 16 Jan 2023 16:55:00 +0800 Subject: [PATCH] =?UTF-8?q?:arrow=5Fup:=20=E6=95=B0=E6=8D=AE=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E8=BF=81=E5=87=BA=E6=97=B6=E5=BF=BD=E7=95=A5=20`.tmp`?= =?UTF-8?q?=20=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=20https://github.com/si?= =?UTF-8?q?yuan-note/siyuan/issues/7087?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/util/ocr.go | 75 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/kernel/util/ocr.go b/kernel/util/ocr.go index 9c6b305c5..9eef498d5 100644 --- a/kernel/util/ocr.go +++ b/kernel/util/ocr.go @@ -105,34 +105,63 @@ func AutoOCRAssets() { } for { - assetsPath := GetDataAssetsAbsPath() - assets := getUnOCRAssetsAbsPaths() - - waitGroup := &sync.WaitGroup{} - lock := &sync.Mutex{} - p, _ := ants.NewPoolWithFunc(4, func(arg interface{}) { - defer waitGroup.Done() - - assetAbsPath := arg.(string) - text := Tesseract(assetAbsPath) - p := strings.TrimPrefix(assetAbsPath, assetsPath) - p = "assets" + filepath.ToSlash(p) - lock.Lock() - assetsTexts[p] = text - lock.Unlock() - assetsTextsChanged = true - }) - for _, assetAbsPath := range assets { - waitGroup.Add(1) - p.Invoke(assetAbsPath) - } - waitGroup.Wait() - p.Release() + autoOCRAssets() time.Sleep(7 * time.Second) } } +func autoOCRAssets() { + defer logging.Recover() + + assetsPath := GetDataAssetsAbsPath() + assets := getUnOCRAssetsAbsPaths() + + waitGroup := &sync.WaitGroup{} + p, _ := ants.NewPoolWithFunc(4, func(arg interface{}) { + defer waitGroup.Done() + + assetAbsPath := arg.(string) + text := Tesseract(assetAbsPath) + p := strings.TrimPrefix(assetAbsPath, assetsPath) + p = "assets" + filepath.ToSlash(p) + assetsTextsLock.Lock() + assetsTexts[p] = text + assetsTextsLock.Unlock() + assetsTextsChanged = true + }) + for _, assetAbsPath := range assets { + waitGroup.Add(1) + p.Invoke(assetAbsPath) + } + waitGroup.Wait() + p.Release() + + cleanNotFoundAssetsTexts() +} + +func cleanNotFoundAssetsTexts() { + tmp := assetsTexts + + assetsPath := GetDataAssetsAbsPath() + var toRemoves []string + for asset, _ := range tmp { + assetAbsPath := strings.TrimPrefix(asset, "assets") + assetAbsPath = filepath.Join(assetsPath, assetAbsPath) + if !gulu.File.IsExist(assetAbsPath) { + toRemoves = append(toRemoves, asset) + } + } + + assetsTextsLock.Lock() + for _, asset := range toRemoves { + delete(assetsTexts, asset) + assetsTextsChanged = true + } + assetsTextsLock.Unlock() + return +} + func getUnOCRAssetsAbsPaths() (ret []string) { assetsPath := GetDataAssetsAbsPath() var assetsPaths []string