🎨 Avoid creating empty ocr-texts.json when OCR is disabled (#16597)

- Skip file creation when OCR is disabled, file doesn't exist, and assetsTexts is empty
- Fix potential deadlock by releasing lock when marshal fails
This commit is contained in:
Jeffrey Chen 2025-12-16 10:46:38 +08:00 committed by GitHub
parent d391809442
commit e7914f99f6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -109,16 +109,24 @@ func SaveAssetsTexts() {
start := time.Now()
assetsPath := GetDataAssetsAbsPath()
assetsTextsPath := filepath.Join(assetsPath, "ocr-texts.json")
assetsTextsLock.Lock()
// OCR 功能未开启且 ocr-texts.json 不存在时,如果 assetsTexts 为空则不创建文件
if !TesseractEnabled && !filelock.IsExist(assetsTextsPath) && 0 == len(assetsTexts) {
assetsTextsLock.Unlock()
assetsTextsChanged.Store(false)
return
}
data, err := gulu.JSON.MarshalIndentJSON(assetsTexts, "", " ")
if err != nil {
logging.LogErrorf("marshal assets texts failed: %s", err)
assetsTextsLock.Unlock()
return
}
assetsTextsLock.Unlock()
assetsPath := GetDataAssetsAbsPath()
assetsTextsPath := filepath.Join(assetsPath, "ocr-texts.json")
if err = filelock.WriteFile(assetsTextsPath, data); err != nil {
logging.LogErrorf("write assets texts failed: %s", err)
return