Skip to content

Commit

Permalink
Store type of Content-Type in cache dir
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed May 27, 2024
1 parent 68c0877 commit 3fc2e78
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Result struct {
Status Status
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
LastModified string
ContentType string
ValidatedDigest bool
}

Expand Down Expand Up @@ -176,7 +177,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
}

if o.cacheDir == "" {
if err := downloadHTTP(ctx, localPath, "", remote, o.description, o.expectedDigest); err != nil {
if err := downloadHTTP(ctx, localPath, "", "", remote, o.description, o.expectedDigest); err != nil {
return nil, err
}
res := &Result{
Expand All @@ -189,6 +190,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
shad := cacheDirectoryPath(o.cacheDir, remote)
shadData := filepath.Join(shad, "data")
shadTime := filepath.Join(shad, "time")
shadType := filepath.Join(shad, "type")
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
if err != nil {
return nil, err
Expand All @@ -213,6 +215,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
Status: StatusUsedCache,
CachePath: shadData,
LastModified: shadTime,
ContentType: shadType,
ValidatedDigest: o.expectedDigest != "",
}
return res, nil
Expand All @@ -227,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
return nil, err
}
if err := downloadHTTP(ctx, shadData, shadTime, remote, o.description, o.expectedDigest); err != nil {
if err := downloadHTTP(ctx, shadData, shadTime, shadType, remote, o.description, o.expectedDigest); err != nil {
return nil, err
}
// no need to pass the digest to copyLocal(), as we already verified the digest
Expand All @@ -243,6 +246,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
Status: StatusDownloaded,
CachePath: shadData,
LastModified: shadTime,
ContentType: shadType,
ValidatedDigest: o.expectedDigest != "",
}
return res, nil
Expand Down Expand Up @@ -271,6 +275,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
shad := cacheDirectoryPath(o.cacheDir, remote)
shadData := filepath.Join(shad, "data")
shadTime := filepath.Join(shad, "time")
shadType := filepath.Join(shad, "type")
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
if err != nil {
return nil, err
Expand All @@ -291,6 +296,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
Status: StatusUsedCache,
CachePath: shadData,
LastModified: shadTime,
ContentType: shadType,
ValidatedDigest: o.expectedDigest != "",
}
return res, nil
Expand All @@ -300,6 +306,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
// - "url" file contains the url
// - "data" file contains the data
// - "time" file contains the time (Last-Modified header)
// - "type" file contains the type (Content-Type header)
func cacheDirectoryPath(cacheDir, remote string) string {
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
}
Expand Down Expand Up @@ -477,7 +484,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
return nil
}

func downloadHTTP(ctx context.Context, localPath, lastModified, url, description string, expectedDigest digest.Digest) error {
func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url, description string, expectedDigest digest.Digest) error {
if localPath == "" {
return fmt.Errorf("downloadHTTP: got empty localPath")
}
Expand All @@ -502,6 +509,12 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, url, description
return err
}
}
if contentType != "" {
ct := resp.Header.Get("Content-Type")
if err := os.WriteFile(contentType, []byte(ct), 0o644); err != nil {
return err
}
}
defer resp.Body.Close()
bar, err := progressbar.New(resp.ContentLength)
if err != nil {
Expand Down

0 comments on commit 3fc2e78

Please sign in to comment.