chore: match uploads by stripping timestamp prefixes
This commit is contained in:
parent
cf575afc3f
commit
3a679eb03c
@ -61,14 +61,25 @@ const isHeifBuffer = (buffer) => buffer && buffer.length >= 12 && HEIF_BRANDS.ha
|
|||||||
function parseBaseName(doc) {
|
function parseBaseName(doc) {
|
||||||
const raw = path.basename(doc.filename || doc.path || '', path.extname(doc.filename || doc.path || ''));
|
const raw = path.basename(doc.filename || doc.path || '', path.extname(doc.filename || doc.path || ''));
|
||||||
// Strip any trailing brace artifacts from older filenames
|
// Strip any trailing brace artifacts from older filenames
|
||||||
const cleaned = raw.endsWith('}') ? raw.slice(0, -1) : raw;
|
const cleaned = raw.replace(/[}]+$/, '');
|
||||||
const match = raw.match(/^(.*?)(-md|-sm)?$/);
|
const match = cleaned.match(/^(.*?)(-md|-sm)?$/);
|
||||||
const base = match ? match[1] : raw;
|
return match ? match[1] : cleaned;
|
||||||
return cleaned !== raw ? cleaned : base;
|
}
|
||||||
|
|
||||||
|
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) {
|
function sourceCandidates(doc) {
|
||||||
const baseName = parseBaseName(doc);
|
const baseName = parseBaseName(doc);
|
||||||
|
const candidateBases = deriveCandidateBases(baseName);
|
||||||
const preferred = new Set();
|
const preferred = new Set();
|
||||||
const fromDocPath = doc.path ? doc.path.replace(/^\/+/, '') : '';
|
const fromDocPath = doc.path ? doc.path.replace(/^\/+/, '') : '';
|
||||||
const fromDocFile = doc.filename ? path.join('uploads', doc.filename) : '';
|
const fromDocFile = doc.filename ? path.join('uploads', doc.filename) : '';
|
||||||
@ -76,14 +87,16 @@ function sourceCandidates(doc) {
|
|||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
.forEach(rel => preferred.add(path.join(UPLOAD_DIR, rel.replace(/^uploads[\\/]/, ''))));
|
.forEach(rel => preferred.add(path.join(UPLOAD_DIR, rel.replace(/^uploads[\\/]/, ''))));
|
||||||
|
|
||||||
// Look for any common source extension using the base name
|
// Look for any common source extension using candidate base names
|
||||||
for (const ext of SOURCE_EXTS) {
|
for (const base of candidateBases) {
|
||||||
preferred.add(path.join(UPLOAD_DIR, `${baseName}${ext}`));
|
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) {
|
// Also check variant-style names in case only a variant exists
|
||||||
preferred.add(path.join(UPLOAD_DIR, `${baseName}-md${ext}`));
|
for (const ext of SOURCE_EXTS) {
|
||||||
preferred.add(path.join(UPLOAD_DIR, `${baseName}-sm${ext}`));
|
preferred.add(path.join(UPLOAD_DIR, `${base}-md${ext}`));
|
||||||
|
preferred.add(path.join(UPLOAD_DIR, `${base}-sm${ext}`));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Array.from(preferred);
|
return Array.from(preferred);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user