Skip to content

Commit

Permalink
Delete invalid .traineddata files in cache per #753 (#757)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica authored May 11, 2023
1 parent 205cfe0 commit 6d8e9fa
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/worker-script/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ let api = null;
let latestJob;
let adapter = {};
let params = defaultParams;
let cachePathWorker;
let cacheMethodWorker;

const load = async ({ workerId, jobId, payload: { options: { corePath, logging } } }, res) => {
setLogging(logging);
Expand Down Expand Up @@ -75,6 +77,10 @@ const loadLanguage = async ({
},
},
res) => {
// Remember cache options for later, as cache may be deleted if `initialize` fails
cachePathWorker = cachePath;
cacheMethodWorker = cacheMethod;

const loadAndGunzipFile = async (_lang) => {
const lang = typeof _lang === 'string' ? _lang : _lang.code;
const readCache = ['refresh', 'none'].includes(cacheMethod)
Expand Down Expand Up @@ -202,6 +208,14 @@ const initialize = async ({
api = new TessModule.TessBaseAPI();
const status = api.Init(null, langs, oem);
if (status === -1) {
// Cache is deleted if initialization fails to avoid keeping bad data in cache
// This assumes that initialization failing only occurs due to bad .traineddata,
// this should be refined if other reasons for init failing are encountered.
if (['write', 'refresh', undefined].includes(cacheMethodWorker)) {
const langsArr = langs.split('+');
const delCachePromise = langsArr.map((lang) => adapter.deleteCache(`${cachePathWorker || '.'}/${lang}.traineddata`));
await Promise.all(delCachePromise);
}
res.reject('initialization failed');
}
params = defaultParams;
Expand Down

0 comments on commit 6d8e9fa

Please sign in to comment.