Skip to content

Commit

Permalink
Add helper download.InMemory for downloading files without writing to…
Browse files Browse the repository at this point in the history
… disk

Currently we have separate code that downloads the release-info.json file
and separate code that downloads the signed bundle hashes file, both  the
files are downloaded in memory this helper will unify that code and we'll
use this helper to download both of these files

It sets the http User-Agent to `crc/<version>` which is currently used for
downloading the file release-info.json
  • Loading branch information
anjannath authored and praveenkumar committed Jun 9, 2023
1 parent deaf7e3 commit 51510f8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package download
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -84,6 +86,30 @@ func Download(uri, destination string, mode os.FileMode, sha256sum []byte) (stri
return filename, nil
}

// Download takes an URL and the User-Agent string to use in the http request
// it takes http.RoundTripper as the third argument to be used by the client
func InMemory(url, userAgent string, transport http.RoundTripper) (io.ReadCloser, error) {
client := grab.NewClient()
client.HTTPClient = &http.Client{Transport: transport}

httpReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create http request: %w", err)
}
httpReq.Header.Set("User-Agent", userAgent)

grabReq, err := grab.NewRequest("", "")
if err != nil {
return nil, fmt.Errorf("failed to create grab request: %w", err)
}
grabReq.HTTPRequest = httpReq
// do not write the downloaded file to disk
grabReq.NoStore = true

rsp := client.Do(grabReq)
return rsp.Open()
}

type RemoteFile struct {
URI string
sha256sum string
Expand Down

0 comments on commit 51510f8

Please sign in to comment.