chore: match uploads by stripping timestamp prefixes

This commit is contained in:
chris 2025-12-08 16:02:51 -05:00
parent cf575afc3f
commit 3a679eb03c

View File

@ -61,14 +61,25 @@ const isHeifBuffer = (buffer) => buffer && buffer.length >= 12 && HEIF_BRANDS.ha
function parseBaseName(doc) {
const raw = path.basename(doc.filename || doc.path || '', path.extname(doc.filename || doc.path || ''));
// Strip any trailing brace artifacts from older filenames
const cleaned = raw.endsWith('}') ? raw.slice(0, -1) : raw;
const match = raw.match(/^(.*?)(-md|-sm)?$/);
const base = match ? match[1] : raw;
return cleaned !== raw ? cleaned : base;
const cleaned = raw.replace(/[}]+$/, '');
const match = cleaned.match(/^(.*?)(-md|-sm)?$/);
return match ? match[1] : cleaned;
}
function deriveCandidateBases(baseName) {
const bases = new Set();
bases.add(baseName);
// If name starts with a leading timestamp and dash, also try the suffix (original filename)
const tsMatch = baseName.match(/^(\d{8,})-(.+)$/);
if (tsMatch) {
bases.add(tsMatch[2]);
}
return Array.from(bases);
}
function sourceCandidates(doc) {
const baseName = parseBaseName(doc);
const candidateBases = deriveCandidateBases(baseName);
const preferred = new Set();
const fromDocPath = doc.path ? doc.path.replace(/^\/+/, '') : '';
const fromDocFile = doc.filename ? path.join('uploads', doc.filename) : '';
@ -76,14 +87,16 @@ function sourceCandidates(doc) {
.filter(Boolean)
.forEach(rel => preferred.add(path.join(UPLOAD_DIR, rel.replace(/^uploads[\\/]/, ''))));
// Look for any common source extension using the base name
for (const ext of SOURCE_EXTS) {
preferred.add(path.join(UPLOAD_DIR, `${baseName}${ext}`));
}
// Also check variant-style names in case only a variant exists
for (const ext of SOURCE_EXTS) {
preferred.add(path.join(UPLOAD_DIR, `${baseName}-md${ext}`));
preferred.add(path.join(UPLOAD_DIR, `${baseName}-sm${ext}`));
// Look for any common source extension using candidate base names
for (const base of candidateBases) {
for (const ext of SOURCE_EXTS) {
preferred.add(path.join(UPLOAD_DIR, `${base}${ext}`));
}
// Also check variant-style names in case only a variant exists
for (const ext of SOURCE_EXTS) {
preferred.add(path.join(UPLOAD_DIR, `${base}-md${ext}`));
preferred.add(path.join(UPLOAD_DIR, `${base}-sm${ext}`));
}
}
return Array.from(preferred);
}