| 213 | } |
| 214 | |
| 215 | func downloadKey(w http.ResponseWriter, kv *KV, store map[string]ValueDesc, key string) { |
| 216 | if store[key].value == nil { |
| 217 | http.Error(w, "value not found", http.StatusNotFound) |
| 218 | return |
| 219 | } |
| 220 | |
| 221 | val := store[key] |
| 222 | |
| 223 | c := kv.GetCodec(store[key].CodecID) |
| 224 | if c == nil { |
| 225 | http.Error(w, "codec not found", http.StatusNotFound) |
| 226 | return |
| 227 | } |
| 228 | |
| 229 | encoded, err := c.Encode(val.value) |
| 230 | if err != nil { |
| 231 | http.Error(w, fmt.Sprintf("failed to encode: %v", err), http.StatusInternalServerError) |
| 232 | return |
| 233 | } |
| 234 | |
| 235 | w.Header().Add("content-type", "application/octet-stream") |
| 236 | // Set content-length so that client knows whether it has received full response or not. |
| 237 | w.Header().Add("content-length", strconv.Itoa(len(encoded))) |
| 238 | w.Header().Add("content-disposition", fmt.Sprintf("attachment; filename=%d-%s", val.Version, key)) |
| 239 | w.WriteHeader(200) |
| 240 | |
| 241 | // Ignore errors, we cannot do anything about them. |
| 242 | _, _ = w.Write(encoded) |
| 243 | } |
| 244 | |
| 245 | //go:embed status.gohtml |
| 246 | var defaultPageContent string |