Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/provider/wikidata_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type WikidataProvider struct {
const (
wikidataAPI = "https://www.wikidata.org/w/api.php"
commonsFile = "https://commons.wikimedia.org/wiki/Special:FilePath/"
// Wikimedia rasterizes SVGs server-side when you append `?width=`. Asking
// for ~512px gives us a PNG large enough to downsize to our biggest output
// (xl=256px) without quality loss, but small enough to download fast. This
// means our image processor (libvips, compiled without rsvg on Alpine)
// only ever sees PNGs from this provider.
commonsThumbWidth = 512
)

func NewWikidataProvider(logger *zap.Logger) *WikidataProvider {
Expand Down Expand Up @@ -63,7 +69,10 @@ func (w *WikidataProvider) GetLogo(ctx context.Context, symbol, companyName stri
return nil, fmt.Errorf("wikidata P154 for %s: %w", entityID, err)
}

logoURL := commonsFile + url.PathEscape(filename)
// Wikimedia gives us a server-rasterized PNG when the file is an SVG and we
// append `?width=`. For non-SVG files the query param is ignored and we get
// the original. Either way, libvips downstream just sees a raster image.
logoURL := fmt.Sprintf("%s%s?width=%d", commonsFile, url.PathEscape(filename), commonsThumbWidth)
data, err := w.downloadImage(ctx, logoURL)
if err != nil {
return nil, fmt.Errorf("downloading %s: %w", logoURL, err)
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/wikidata_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func TestWikidataProvider_GetLogo_Success(t *testing.T) {
if !strings.Contains(result.OriginalURL, "Diageo") {
t.Errorf("OriginalURL should contain the filename: %q", result.OriginalURL)
}
if !strings.Contains(result.OriginalURL, "width=") {
t.Errorf("OriginalURL should ask Wikimedia to rasterize SVG → PNG via width=: %q", result.OriginalURL)
}
if result.Source != "wikidata:Q161140" {
t.Errorf("Source = %q", result.Source)
}
Expand Down
Loading