Skip to content

Read body into memory first #223

@digitalghost-dev

Description

@digitalghost-dev

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))

Metadata

Metadata

Assignees

No one assigned

    Labels

    improvementImprovement to an existing feature.

    Projects

    Status

    Building

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions