| 136 | const unit = match[2] || "mb"; |
| 137 | const multiplier = |
| 138 | unit === "gb" |
| 139 | ? 1024 * 1024 * 1024 |
| 140 | : unit === "mb" |
| 141 | ? 1024 * 1024 |
| 142 | : unit === "kb" |
| 143 | ? 1024 |
| 144 | : 1; |
| 145 | |
| 146 | const bytes = Math.round(value * multiplier); |
| 147 | if (!Number.isFinite(bytes) || bytes <= 0) return null; |
| 148 | return bytes; |
| 149 | } |
| 150 | |
| 151 | function normalizeMaxBytes(bytes: number | string | null | undefined): number { |
| 152 | const numeric = typeof bytes === "number" ? bytes : Number(bytes); |
| 153 | if (!Number.isFinite(numeric) || numeric <= 0) { |
| 154 | return DEFAULT_MAX_IMAGE_BYTES; |
| 155 | } |
| 156 | const rounded = Math.round(numeric); |
| 157 | if (rounded < MIN_ALLOWED_IMAGE_BYTES) { |
| 158 | return MIN_ALLOWED_IMAGE_BYTES; |
| 159 | } |
| 160 | if (rounded > MAX_ALLOWED_IMAGE_BYTES) { |
| 161 | return MAX_ALLOWED_IMAGE_BYTES; |
| 162 | } |
| 163 | return rounded; |
| 164 | } |