| 346 | } |
| 347 | |
| 348 | func unitConversion(sizeWithUnit string) (int64, error) { |
| 349 | // Empty size is expected for closed indices (ES returns null → unmarshals to "") |
| 350 | if sizeWithUnit == "" { |
| 351 | return 0, nil |
| 352 | } |
| 353 | |
| 354 | sizeWithUnit = strings.ToLower(sizeWithUnit) |
| 355 | sizeRe := regexp.MustCompile("([0-9.]+)([kmgtpe]?b)") |
| 356 | match := sizeRe.FindSubmatch([]byte(sizeWithUnit)) |
| 357 | |
| 358 | // Non-empty string that doesn't match expected format is unexpected |
| 359 | if len(match) < 3 { |
| 360 | return 0, errors.Errorf("unexpected size format: %q", sizeWithUnit) |
| 361 | } |
| 362 | |
| 363 | unit := string(match[2]) |
| 364 | |
| 365 | size, err := strconv.ParseFloat(string(match[1]), 64) |
| 366 | if err != nil { |
| 367 | return 0, err |
| 368 | } |
| 369 | |
| 370 | switch unit { |
| 371 | case "kb": |
| 372 | size *= 1024 |
| 373 | case "mb": |
| 374 | size *= 1024 * 1024 |
| 375 | case "gb": |
| 376 | size *= 1024 * 1024 * 1024 |
| 377 | case "tb": |
| 378 | size *= 1024 * 1024 * 1024 * 1024 |
| 379 | case "pb": |
| 380 | size *= 1024 * 1024 * 1024 * 1024 * 1024 |
| 381 | case "eb": |
| 382 | size *= 1024 * 1024 * 1024 * 1024 * 1024 * 1024 |
| 383 | default: |
| 384 | // For "b" (bytes) or any other unit, keep the size as-is |
| 385 | } |
| 386 | |
| 387 | return int64(size), nil |
| 388 | } |
| 389 | |
| 390 | func readBytesAndClose(anyResp any) ([]byte, error) { |
| 391 | var body io.ReadCloser |