Skip to content

Commit

Permalink
fix: Return size info when converting icon
Browse files Browse the repository at this point in the history
Closes develar#42
  • Loading branch information
0xjac committed Oct 24, 2021
1 parent c24d1b5 commit 356a652
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions pkg/icons/icon-converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,30 +118,38 @@ func writeUserError(error util.MessageError) error {
return util.WriteJsonToStdOut(MisConfigurationError{Message: error.Error(), Code: error.ErrorCode()})
}

func validateImageSize(file string, recommendedMinSize int) error {
func validateImageSize(file string, recommendedMinSize int) (int, error) {
firstFileBytes, err := fs.ReadFile(file, 512)
if err != nil {
return errors.WithStack(err)
return 0, errors.WithStack(err)
}

if IsIco(firstFileBytes) {
for _, size := range GetIcoSizes(firstFileBytes) {
if size.Width >= recommendedMinSize && size.Height >= recommendedMinSize {
return nil
if size.Width > size.Height {
return size.Width, nil
} else {
return size.Height, nil
}
}
}
} else {
config, err := DecodeImageConfig(file)
if err != nil {
return errors.WithStack(err)
return 0, errors.WithStack(err)
}

if config.Width >= recommendedMinSize && config.Height >= recommendedMinSize {
return nil
if config.Width > config.Height {
return config.Width, nil
} else {
return config.Height, nil
}
}
}

return NewImageSizeError(file, recommendedMinSize)
return 0, NewImageSizeError(file, recommendedMinSize)
}

func outputFormatToSingleFileExtension(outputFormat string) string {
Expand Down Expand Up @@ -177,14 +185,14 @@ func doConvertIcon(sourceFiles []string, roots []string, outputFormat string, ou
isOutputFormatIco := outputFormat == "ico"
if strings.HasSuffix(resolvedPath, outExt) {
if outputFormat != "icns" {
err = validateImageSize(resolvedPath, inputInfo.recommendedMinSize)
inputInfo.MaxIconSize, err = validateImageSize(resolvedPath, inputInfo.recommendedMinSize)
if err != nil {
return nil, errors.WithStack(err)
}
}

// size not required in this case
return []IconInfo{{File: resolvedPath}}, nil
return []IconInfo{{File: resolvedPath, Size: inputInfo.MaxIconSize}}, nil
}

if fileInfo.IsDir() {
Expand Down Expand Up @@ -287,7 +295,7 @@ func convertSingleFile(inputInfo *InputFileInfo, outFile string, outputFormat st
if err != nil {
return nil, errors.WithStack(err)
}
return []IconInfo{{File: outFile}}, err
return []IconInfo{{File: outFile, Size: inputInfo.MaxIconSize}}, err

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

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

0 comments on commit 356a652

Please sign in to comment.