fix(api): avoid duplicate wikipedia embed thumbs (#51)

This commit is contained in:
hampus-fluxer 2026-01-06 04:51:36 +01:00 committed by GitHub
parent d33ccb119f
commit 5f627b5b0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -157,10 +157,29 @@ export class WikipediaResolver extends BaseResolver {
if (!url) return null;
try {
return new URL(url).href.replace(/\/$/, '');
const normalizedUrl = new URL(url);
this.normalizeWikipediaImagePath(normalizedUrl);
return normalizedUrl.href.replace(/\/$/, '');
} catch (error) {
Logger.debug({error, url}, 'Failed to normalize Wikipedia image URL');
return null;
}
}
private normalizeWikipediaImagePath(imageUrl: URL): void {
if (imageUrl.hostname !== 'upload.wikimedia.org' || !imageUrl.pathname.includes('/wikipedia/commons/thumb/')) {
return;
}
const segments = imageUrl.pathname.split('/');
const thumbIndex = segments.indexOf('thumb');
if (thumbIndex === -1 || segments.length <= thumbIndex + 2) {
return;
}
const normalizedSegments = [...segments.slice(0, thumbIndex), ...segments.slice(thumbIndex + 1, -1)];
const normalizedPath = normalizedSegments.join('/') || '/';
imageUrl.pathname = normalizedPath.startsWith('/') ? normalizedPath : `/${normalizedPath}`;
}
}