fix: make sure to remove only failed PDF417 scans

This commit is contained in:
siiky 2024-07-03 17:22:21 +01:00
parent 393e149b91
commit 1451f78211

View file

@ -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(