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

5c82ccf4352c3be6465036250823ffa04ce9a5b8

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +26 -16Showing whitespace changes
src/endpoints/chats.js+13 -16
@@ -9,7 +9,14 @@ import { sync as writeFileAtomicSync } from 'write-file-atomic';
9import _ from 'lodash';9import _ from 'lodash';
1010
11import { jsonParser, urlencodedParser } from '../express-common.js';11import { jsonParser, urlencodedParser } from '../express-common.js';
12import { getConfigValue, humanizedISO8601DateTime, tryParse, generateTimestamp, removeOldBackups } from '../util.js';12import {
13 getConfigValue,
14 humanizedISO8601DateTime,
15 tryParse,
16 generateTimestamp,
17 removeOldBackups,
18 formatBytes,
19} from '../util.js';
1320
14const isBackupDisabled = getConfigValue('disableChatBackup', false);21const isBackupDisabled = getConfigValue('disableChatBackup', false);
15const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1));22const maxTotalChatBackups = Number(getConfigValue('maxTotalChatBackups', -1));
@@ -46,6 +53,9 @@ function backupChat(directory, name, chat) {
46 }53 }
47}54}
4855
56/**
57 * @type {Map<string, import('lodash').DebouncedFunc<function(string, string, string): void>>}
58 */
49const backupFunctions = new Map();59const backupFunctions = new Map();
5060
51/**61/**
@@ -57,20 +67,7 @@ function getBackupFunction(handle) {
57 if (!backupFunctions.has(handle)) {67 if (!backupFunctions.has(handle)) {
58 backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true }));68 backupFunctions.set(handle, _.throttle(backupChat, throttleInterval, { leading: true, trailing: true }));
59 }69 }
60 return backupFunctions.get(handle);70 return backupFunctions.get(handle) || (() => {});
61}
62
63/**
64 * Formats a byte size into a human-readable string with units
65 * @param {number} bytes - The size in bytes to format
66 * @returns {string} The formatted string (e.g., "1.5 MB")
67 */
68function formatBytes(bytes) {
69 if (bytes === 0) return '0 B';
70 const k = 1024;
71 const sizes = ['B', 'KB', 'MB', 'GB'];
72 const i = Math.floor(Math.log(bytes) / Math.log(k));
73 return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
74}71}
7572
76/**73/**
@@ -710,7 +707,7 @@ router.post('/search', jsonParser, function (request, response) {
710 }707 }
711708
712 // Sort by last message date descending709 // Sort by last message date descending
713 results.sort((a, b) => new Date(b.last_mes) - new Date(a.last_mes));710 results.sort((a, b) => new Date(b.last_mes).getTime() - new Date(a.last_mes).getTime());
714 return response.send(results);711 return response.send(results);
715712
716 } catch (error) {713 } catch (error) {
src/util.js+13 -0
@@ -143,6 +143,19 @@ export function getHexString(length) {
143}143}
144144
145/**145/**
146 * Formats a byte size into a human-readable string with units
147 * @param {number} bytes - The size in bytes to format
148 * @returns {string} The formatted string (e.g., "1.5 MB")
149 */
150export function formatBytes(bytes) {
151 if (bytes === 0) return '0 B';
152 const k = 1024;
153 const sizes = ['B', 'KB', 'MB', 'GB'];
154 const i = Math.floor(Math.log(bytes) / Math.log(k));
155 return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
156}
157
158/**
146 * Extracts a file with given extension from an ArrayBuffer containing a ZIP archive.159 * Extracts a file with given extension from an ArrayBuffer containing a ZIP archive.
147 * @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive160 * @param {ArrayBuffer} archiveBuffer Buffer containing a ZIP archive
148 * @param {string} fileExtension File extension to look for161 * @param {string} fileExtension File extension to look for