Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

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

hampus-fluxer5f627b5b0b72
diff --git a/fluxer_api/src/unfurler/resolvers/WikipediaResolver.ts b/fluxer_api/src/unfurler/resolvers/WikipediaResolver.ts
index 308ce0118..16ef74f0b 100644
--- a/fluxer_api/src/unfurler/resolvers/WikipediaResolver.ts
+++ b/fluxer_api/src/unfurler/resolvers/WikipediaResolver.ts
@@ -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}`;
+ }
}