The current implementation of image.Decode() in the card command reads chunks from the network on demand as it parses the image format. This means a large image that downloads slowly could timeout during the decode phase, even though the connection was still alive and making progress.
Using io.ReadAll first to read all the data into memory allows image.Decode() to decode instantly without network delays.
limitedBody := io.LimitReader(resp.Body, 10*1024*1024)
bodyBytes, err := io.ReadAll(limitedBody)
if err != nil {
return "", fmt.Errorf("failed to read image data: %w", err)
}
img, _, err := image.Decode(bytes.NewReader(bodyBytes))
The current implementation of
image.Decode()in thecardcommand reads chunks from the network on demand as it parses the image format. This means a large image that downloads slowly could timeout during the decode phase, even though the connection was still alive and making progress.Using
io.ReadAllfirst to read all the data into memory allowsimage.Decode()to decode instantly without network delays.