refactor: enhance download management by validating URLs and adding file size handling

This commit is contained in:
Moyasee
2026-01-24 19:38:00 +02:00
parent fb1380356e
commit eea7148108
8 changed files with 153 additions and 46 deletions

View File

@@ -51,6 +51,25 @@ export const formatBytes = (bytes: number): string => {
return `${Math.trunc(formatedByte * 10) / 10} ${FORMAT[base]}`;
};
export const parseBytes = (sizeString: string | null): number | null => {
if (!sizeString) return null;
const regex = /^([\d.,]+)\s*([A-Za-z]+)$/;
const match = regex.exec(sizeString.trim());
if (!match) return null;
const value = Number.parseFloat(match[1].replaceAll(",", "."));
const unit = match[2].toUpperCase();
if (Number.isNaN(value)) return null;
const unitIndex = FORMAT.indexOf(unit);
if (unitIndex === -1) return null;
const byteKBase = 1024;
return Math.round(value * Math.pow(byteKBase, unitIndex));
};
export const formatBytesToMbps = (bytesPerSecond: number): string => {
const bitsPerSecond = bytesPerSecond * 8;
const mbps = bitsPerSecond / (1024 * 1024);