Refactor endpoints/chats.js

1. Move formatBytes utility to util.js
2. Fix type error related to dates sorting
3. Add type to backupFunctions map
This commit is contained in:
Cohee
2024-12-12 14:07:37 +02:00
parent cf1b98e25d
commit 5c82ccf435
2 changed files with 26 additions and 16 deletions

View File

@@ -142,6 +142,19 @@ export function getHexString(length) {
return result;
}
/**
* Formats a byte size into a human-readable string with units
* @param {number} bytes - The size in bytes to format
* @returns {string} The formatted string (e.g., "1.5 MB")
*/
export function formatBytes(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
/**
* Extracts a file with given extension from an ArrayBuffer containing a ZIP archive.
* @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive