|
9 | 9 | "context" |
10 | 10 | "errors" |
11 | 11 | "fmt" |
| 12 | + "net/http" |
12 | 13 | "path" |
13 | 14 | "time" |
14 | 15 |
|
@@ -115,6 +116,11 @@ func (c *Cache) Get(ctx context.Context, profileID string) (cache.BootAsset, err |
115 | 116 | return nil, fmt.Errorf("error getting object stat for object %q: %w", key, err) |
116 | 117 | } |
117 | 118 |
|
| 119 | + // we need to fix metadata if it's not present |
| 120 | + if !hasRequiredMetadata(stat.Metadata) { |
| 121 | + return nil, cache.ErrCacheNotFound |
| 122 | + } |
| 123 | + |
118 | 124 | redirect, err := c.s3cli.PresignedGetObject(ctx, c.bucketName, key, expires, nil) |
119 | 125 | if err != nil { |
120 | 126 | var minioErr minio.ErrorResponse |
@@ -148,15 +154,17 @@ func (c *Cache) Get(ctx context.Context, profileID string) (cache.BootAsset, err |
148 | 154 | } |
149 | 155 |
|
150 | 156 | // Put uploads the boot asset to the registry. |
151 | | -func (c *Cache) Put(ctx context.Context, profileID string, asset cache.BootAsset) error { |
| 157 | +func (c *Cache) Put(ctx context.Context, profileID string, asset cache.BootAsset, filename string) error { |
152 | 158 | key := path.Join(prefix, profileID) |
153 | 159 |
|
154 | 160 | data, err := asset.Reader() |
155 | 161 | if err != nil { |
156 | 162 | return fmt.Errorf("error getting reader for asset from profile %q: %w", profileID, err) |
157 | 163 | } |
158 | 164 |
|
159 | | - stat, err := c.s3cli.PutObject(ctx, c.bucketName, key, data, asset.Size(), minio.PutObjectOptions{}) |
| 165 | + stat, err := c.s3cli.PutObject(ctx, c.bucketName, key, data, asset.Size(), minio.PutObjectOptions{ |
| 166 | + ContentDisposition: fmt.Sprintf(`attachment; filename="%s"`, filename), |
| 167 | + }) |
160 | 168 | if err != nil { |
161 | 169 | var minioErr minio.ErrorResponse |
162 | 170 | if errors.As(err, &minioErr) { |
@@ -184,9 +192,21 @@ func (c *Cache) Put(ctx context.Context, profileID string, asset cache.BootAsset |
184 | 192 | return fmt.Errorf("error creating reference asset for profile %q: %w", profileID, err) |
185 | 193 | } |
186 | 194 |
|
187 | | - if err := c.signingCache.Put(ctx, profileID, ref); err != nil { |
| 195 | + if err := c.signingCache.Put(ctx, profileID, ref, filename); err != nil { |
188 | 196 | return fmt.Errorf("error putting asset reference for profile %q to registry: %w", profileID, err) |
189 | 197 | } |
190 | 198 |
|
191 | 199 | return nil |
192 | 200 | } |
| 201 | + |
| 202 | +func hasRequiredMetadata(metadata http.Header) bool { |
| 203 | + for _, key := range []string{ |
| 204 | + "Content-Disposition", |
| 205 | + } { |
| 206 | + if metadata.Get(key) == "" { |
| 207 | + return false |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + return true |
| 212 | +} |
0 commit comments