Skip to content

Commit

Permalink
Quick fix for #1
Browse files Browse the repository at this point in the history
  • Loading branch information
scambier committed Jun 17, 2023
1 parent 65d3a5e commit 816eda6
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/src/ocr/ocr-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import {
workerTimeout,
} from '../globals'
import type { OcrOptions } from '../types'
import type { ocrLangs } from './ocr-langs'

/**
* Concatenates an array of langs to a single string to be passed to Tesseract
* e.g. ['fra', 'eng'] => 'eng+fra'
* The langs are sorted alphabetically because it's also used a cache key
* @param langs
* @returns
*/
function concatLangs (langs: Array<typeof ocrLangs[number]>): string {
return langs.sort().join('+')
}

class OCRWorker {
static #pool: OCRWorker[] = []
Expand Down Expand Up @@ -51,12 +63,12 @@ class OCRWorker {
}): Promise<{ text: string; langs: string }> {
return new Promise(async (resolve, reject) => {
this.#running = true
const langs = msg.options.langs.join('+')
const langs = concatLangs(msg.options.langs)

if (!this.#ready) {
await this.worker.load()
await this.worker.loadLanguage(langs)
await this.worker.initialize(msg.options.langs[0])
await this.worker.initialize(langs)
this.#ready = true
}

Expand Down Expand Up @@ -101,9 +113,9 @@ class OCRManager {
}

async #getImageText(file: TFile, options: OcrOptions): Promise<string> {
const optLangs = options.langs.sort().join('+')
const langs = concatLangs(options.langs)
// Get the text from the cache if it exists
const cache = await readCache(file, optLangs)
const cache = await readCache(file, langs)
if (cache) {
return cache.text ?? FAILED_TO_EXTRACT
}
Expand Down Expand Up @@ -132,12 +144,12 @@ class OCRManager {
.trim()

// Add it to the cache
await writeCache(cachePath.folder, cachePath.filename, text, optLangs)
await writeCache(cachePath.folder, cachePath.filename, text, langs)
resolve(text)
} catch (e) {
// In case of error (unreadable PDF or timeout) just add
// an empty string to the cache
await writeCache(cachePath.folder, cachePath.filename, '', optLangs)
await writeCache(cachePath.folder, cachePath.filename, '', langs)
resolve('')
}
})
Expand Down

0 comments on commit 816eda6

Please sign in to comment.