Skip to content

Commit c92c3a2

Browse files
authored
fix: Return size info when converting icon (#71)
Closes #42
1 parent 23e701c commit c92c3a2

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

pkg/icons/icon-converter.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,38 @@ func writeUserError(error util.MessageError) error {
118118
return util.WriteJsonToStdOut(MisConfigurationError{Message: error.Error(), Code: error.ErrorCode()})
119119
}
120120

121-
func validateImageSize(file string, recommendedMinSize int) error {
121+
func validateImageSize(file string, recommendedMinSize int) (int, error) {
122122
firstFileBytes, err := fs.ReadFile(file, 512)
123123
if err != nil {
124-
return errors.WithStack(err)
124+
return 0, errors.WithStack(err)
125125
}
126126

127127
if IsIco(firstFileBytes) {
128128
for _, size := range GetIcoSizes(firstFileBytes) {
129129
if size.Width >= recommendedMinSize && size.Height >= recommendedMinSize {
130-
return nil
130+
if size.Width > size.Height {
131+
return size.Width, nil
132+
} else {
133+
return size.Height, nil
134+
}
131135
}
132136
}
133137
} else {
134138
config, err := DecodeImageConfig(file)
135139
if err != nil {
136-
return errors.WithStack(err)
140+
return 0, errors.WithStack(err)
137141
}
138142

139143
if config.Width >= recommendedMinSize && config.Height >= recommendedMinSize {
140-
return nil
144+
if config.Width > config.Height {
145+
return config.Width, nil
146+
} else {
147+
return config.Height, nil
148+
}
141149
}
142150
}
143151

144-
return NewImageSizeError(file, recommendedMinSize)
152+
return 0, NewImageSizeError(file, recommendedMinSize)
145153
}
146154

147155
func outputFormatToSingleFileExtension(outputFormat string) string {
@@ -177,14 +185,14 @@ func doConvertIcon(sourceFiles []string, roots []string, outputFormat string, ou
177185
isOutputFormatIco := outputFormat == "ico"
178186
if strings.HasSuffix(resolvedPath, outExt) {
179187
if outputFormat != "icns" {
180-
err = validateImageSize(resolvedPath, inputInfo.recommendedMinSize)
188+
inputInfo.MaxIconSize, err = validateImageSize(resolvedPath, inputInfo.recommendedMinSize)
181189
if err != nil {
182190
return nil, errors.WithStack(err)
183191
}
184192
}
185193

186194
// size not required in this case
187-
return []IconInfo{{File: resolvedPath}}, nil
195+
return []IconInfo{{File: resolvedPath, Size: inputInfo.MaxIconSize}}, nil
188196
}
189197

190198
if fileInfo.IsDir() {
@@ -287,7 +295,7 @@ func convertSingleFile(inputInfo *InputFileInfo, outFile string, outputFormat st
287295
if err != nil {
288296
return nil, errors.WithStack(err)
289297
}
290-
return []IconInfo{{File: outFile}}, err
298+
return []IconInfo{{File: outFile, Size: inputInfo.MaxIconSize}}, err
291299

292300
case "ico":
293301
maxImage, err := inputInfo.GetMaxImage()
@@ -299,7 +307,7 @@ func convertSingleFile(inputInfo *InputFileInfo, outFile string, outputFormat st
299307
if err != nil {
300308
return nil, errors.WithStack(err)
301309
}
302-
return []IconInfo{{File: outFile}}, nil
310+
return []IconInfo{{File: outFile, Size: inputInfo.MaxIconSize}}, nil
303311

304312
default:
305313
return nil, errors.Errorf("unknown output format %s", outputFormat)

0 commit comments

Comments
 (0)