From 5def16e5427fcb027566d86df28be6f8407682d2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 9 Feb 2024 14:29:12 +0100 Subject: [PATCH] Improve finding windows exe icon --- profile/binmeta/find_windows.go | 49 +++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/profile/binmeta/find_windows.go b/profile/binmeta/find_windows.go index b00cb166..deeeb235 100644 --- a/profile/binmeta/find_windows.go +++ b/profile/binmeta/find_windows.go @@ -63,29 +63,6 @@ func getIconAndNamefromRSS(ctx context.Context, binPath string) (png []byte, nam // return true // }) - // Get first icon. - var ( - icon *winres.Icon - iconErr error - ) - rss.WalkType(winres.RT_GROUP_ICON, func(resID winres.Identifier, langID uint16, _ []byte) bool { - icon, iconErr = rss.GetIconTranslation(resID, langID) - return iconErr != nil - }) - if iconErr != nil { - return nil, "", fmt.Errorf("failed to get icon: %w", err) - } - // Convert icon. - icoBuf := &bytes.Buffer{} - err = icon.SaveICO(icoBuf) - if err != nil { - return nil, "", fmt.Errorf("failed to save ico: %w", err) - } - png, err = ConvertICOtoPNG(icoBuf.Bytes()) - if err != nil { - return nil, "", fmt.Errorf("failed to convert ico to png: %w", err) - } - // Get name from version record. var ( versionInfo *version.Info @@ -111,5 +88,31 @@ func getIconAndNamefromRSS(ctx context.Context, binPath string) (png []byte, nam }) name = cleanFileDescription(name) + // Get first icon. + var ( + icon *winres.Icon + iconErr error + ) + rss.WalkType(winres.RT_GROUP_ICON, func(resID winres.Identifier, langID uint16, _ []byte) bool { + icon, iconErr = rss.GetIconTranslation(resID, langID) + return iconErr != nil + }) + if iconErr != nil { + return nil, name, fmt.Errorf("failed to get icon: %w", err) + } + if icon == nil { + return nil, name, errors.New("no icon in resources") + } + // Convert icon, if it exists. + icoBuf := &bytes.Buffer{} + err = icon.SaveICO(icoBuf) + if err != nil { + return nil, name, fmt.Errorf("failed to save ico: %w", err) + } + png, err = ConvertICOtoPNG(icoBuf.Bytes()) + if err != nil { + return nil, name, fmt.Errorf("failed to convert ico to png: %w", err) + } + return png, name, nil }