@@ -45,6 +45,7 @@ type Result struct {
45
45
Status Status
46
46
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
47
47
LastModified string
48
+ ContentType string
48
49
ValidatedDigest bool
49
50
}
50
51
@@ -176,7 +177,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
176
177
}
177
178
178
179
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 {
180
181
return nil , err
181
182
}
182
183
res := & Result {
@@ -189,6 +190,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
189
190
shad := cacheDirectoryPath (o .cacheDir , remote )
190
191
shadData := filepath .Join (shad , "data" )
191
192
shadTime := filepath .Join (shad , "time" )
193
+ shadType := filepath .Join (shad , "type" )
192
194
shadDigest , err := cacheDigestPath (shad , o .expectedDigest )
193
195
if err != nil {
194
196
return nil , err
@@ -213,6 +215,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
213
215
Status : StatusUsedCache ,
214
216
CachePath : shadData ,
215
217
LastModified : shadTime ,
218
+ ContentType : shadType ,
216
219
ValidatedDigest : o .expectedDigest != "" ,
217
220
}
218
221
return res , nil
@@ -227,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
227
230
if err := os .WriteFile (shadURL , []byte (remote ), 0o644 ); err != nil {
228
231
return nil , err
229
232
}
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 {
231
234
return nil , err
232
235
}
233
236
// 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,
243
246
Status : StatusDownloaded ,
244
247
CachePath : shadData ,
245
248
LastModified : shadTime ,
249
+ ContentType : shadType ,
246
250
ValidatedDigest : o .expectedDigest != "" ,
247
251
}
248
252
return res , nil
@@ -271,6 +275,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
271
275
shad := cacheDirectoryPath (o .cacheDir , remote )
272
276
shadData := filepath .Join (shad , "data" )
273
277
shadTime := filepath .Join (shad , "time" )
278
+ shadType := filepath .Join (shad , "type" )
274
279
shadDigest , err := cacheDigestPath (shad , o .expectedDigest )
275
280
if err != nil {
276
281
return nil , err
@@ -291,6 +296,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
291
296
Status : StatusUsedCache ,
292
297
CachePath : shadData ,
293
298
LastModified : shadTime ,
299
+ ContentType : shadType ,
294
300
ValidatedDigest : o .expectedDigest != "" ,
295
301
}
296
302
return res , nil
@@ -300,6 +306,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
300
306
// - "url" file contains the url
301
307
// - "data" file contains the data
302
308
// - "time" file contains the time (Last-Modified header)
309
+ // - "type" file contains the type (Content-Type header)
303
310
func cacheDirectoryPath (cacheDir , remote string ) string {
304
311
return filepath .Join (cacheDir , "download" , "by-url-sha256" , fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (remote ))))
305
312
}
@@ -477,7 +484,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
477
484
return nil
478
485
}
479
486
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 {
481
488
if localPath == "" {
482
489
return fmt .Errorf ("downloadHTTP: got empty localPath" )
483
490
}
@@ -502,6 +509,12 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, url, description
502
509
return err
503
510
}
504
511
}
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
+ }
505
518
defer resp .Body .Close ()
506
519
bar , err := progressbar .New (resp .ContentLength )
507
520
if err != nil {
0 commit comments