From 1451f7821172fb155e836c23e23db2355725d28d Mon Sep 17 00:00:00 2001 From: siiky Date: Wed, 3 Jul 2024 17:22:21 +0100 Subject: [PATCH] fix: make sure to remove only failed PDF417 scans --- lib/poller.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/poller.js b/lib/poller.js index b3bba26b..65418df4 100644 --- a/lib/poller.js +++ b/lib/poller.js @@ -144,20 +144,32 @@ const readdirRec = rootPath => .then(_.flatten) const stat = path => fs.stat(path).then(_.set('path', path)) +const pathComponents = p => path.normalize(p).split(path.sep) // @see lib/customers.js:updateIdCardData() const cleanOldFailedPDF417Scans = () => { + const matcher = (c, pat) => typeof pat === 'function' ? pat(c) : c === pat + const PDF417ScanPathPattern = _.concat( + pathComponents(OPERATOR_DATA_DIR), + ["id-operator", s => /* customerid*/ true, "idcarddata", fname => path.extname(fname) === 'jpg'] + ) + const isPDF417Scan = entry => { + entry = pathComponents(entry.path) + return entry.length === PDF417ScanPathPattern.length + && _.isMatchWith(matcher, PDF417ScanPathPattern, pathComponents(entry.path)) + } + let old = new Date() old.setDate(old.getDate() - 2) // 2 days ago old = old.getTime() - /* NOTE: Small caveat to mtime: last time the file was written to. */ + /* NOTE: Small caveat to mtime: last time the file was written to. */ const isOld = filestat => filestat.mtimeMs < old readdirRec(path.join(OPERATOR_DATA_DIR, 'id-operator')) .then(entries => Promise.all( entries - .filter(entry => entry.isFile()) + .filter(entry => entry.isFile() && isPDF417Scan(entry)) .map(entry => stat(entry.path)) )) .then(filestats => Promise.all(