Skip to content

Commit e8841f6

Browse files
committed
Store type of Content-Type in cache dir
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 5524c02 commit e8841f6

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pkg/downloader/downloader.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Result struct {
4545
Status Status
4646
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
4747
LastModified string
48+
ContentType string
4849
ValidatedDigest bool
4950
}
5051

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

178179
if o.cacheDir == "" {
179-
if err := downloadHTTP(ctx, localPath, "", remote, o.description, o.expectedDigest); err != nil {
180+
if err := downloadHTTP(ctx, localPath, "", "", remote, o.description, o.expectedDigest); err != nil {
180181
return nil, err
181182
}
182183
res := &Result{
@@ -189,6 +190,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
189190
shad := cacheDirectoryPath(o.cacheDir, remote)
190191
shadData := filepath.Join(shad, "data")
191192
shadTime := filepath.Join(shad, "time")
193+
shadType := filepath.Join(shad, "type")
192194
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
193195
if err != nil {
194196
return nil, err
@@ -213,6 +215,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
213215
Status: StatusUsedCache,
214216
CachePath: shadData,
215217
LastModified: shadTime,
218+
ContentType: shadType,
216219
ValidatedDigest: o.expectedDigest != "",
217220
}
218221
return res, nil
@@ -227,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
227230
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
228231
return nil, err
229232
}
230-
if err := downloadHTTP(ctx, shadData, shadTime, remote, o.description, o.expectedDigest); err != nil {
233+
if err := downloadHTTP(ctx, shadData, shadTime, shadType, remote, o.description, o.expectedDigest); err != nil {
231234
return nil, err
232235
}
233236
// no need to pass the digest to copyLocal(), as we already verified the digest
@@ -243,6 +246,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
243246
Status: StatusDownloaded,
244247
CachePath: shadData,
245248
LastModified: shadTime,
249+
ContentType: shadType,
246250
ValidatedDigest: o.expectedDigest != "",
247251
}
248252
return res, nil
@@ -271,6 +275,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
271275
shad := cacheDirectoryPath(o.cacheDir, remote)
272276
shadData := filepath.Join(shad, "data")
273277
shadTime := filepath.Join(shad, "time")
278+
shadType := filepath.Join(shad, "type")
274279
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
275280
if err != nil {
276281
return nil, err
@@ -291,6 +296,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
291296
Status: StatusUsedCache,
292297
CachePath: shadData,
293298
LastModified: shadTime,
299+
ContentType: shadType,
294300
ValidatedDigest: o.expectedDigest != "",
295301
}
296302
return res, nil
@@ -300,6 +306,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
300306
// - "url" file contains the url
301307
// - "data" file contains the data
302308
// - "time" file contains the time (Last-Modified header)
309+
// - "type" file contains the type (Content-Type header)
303310
func cacheDirectoryPath(cacheDir, remote string) string {
304311
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
305312
}
@@ -477,7 +484,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
477484
return nil
478485
}
479486

480-
func downloadHTTP(ctx context.Context, localPath, lastModified, url, description string, expectedDigest digest.Digest) error {
487+
func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url, description string, expectedDigest digest.Digest) error {
481488
if localPath == "" {
482489
return fmt.Errorf("downloadHTTP: got empty localPath")
483490
}
@@ -502,6 +509,12 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, url, description
502509
return err
503510
}
504511
}
512+
if contentType != "" {
513+
ct := resp.Header.Get("Content-Type")
514+
if err := os.WriteFile(contentType, []byte(ct), 0o644); err != nil {
515+
return err
516+
}
517+
}
505518
defer resp.Body.Close()
506519
bar, err := progressbar.New(resp.ContentLength)
507520
if err != nil {

0 commit comments

Comments
 (0)