refactor: 大部分文件处理部分

This commit is contained in:
手瓜一十雪 committed 2025-01-13 20:30:08 +08:00
1 parent 7f3dbe0552
commit ec0c2e8c33
12 files changed
+98 -106

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import { Peer } from '@/core';
import { LRUCache } from './lru-cache';
import { randomUUID } from 'crypto';
interface FileUUIDData {
peer: Peer;
modelId?: string;
fileId?: string;
msgId?: string;
elementId?: string;
fileUUID?: string;
}
class FileUUIDManager {
private cache: LRUCache<string, FileUUIDData>;
constructor(capacity: number) {
this.cache = new LRUCache<string, FileUUIDData>(capacity);
}
public encode(data: FileUUIDData, endString: string = "", customUUID?: string): string {
const uuid = customUUID ? customUUID : randomUUID().replace(/-/g, '') + endString;
this.cache.put(uuid, data);
return uuid;
}
public decode(uuid: string): FileUUIDData | undefined {
return this.cache.get(uuid);
}
}
export class FileNapCatOneBotUUIDWrap {
private manager: FileUUIDManager;
constructor(capacity: number = 100) {
this.manager = new FileUUIDManager(capacity);
}
public encodeModelId(peer: Peer, modelId: string, fileId: string, fileUUID: string = "", endString: string = "", customUUID?: string): string {
return this.manager.encode({ peer, modelId, fileId, fileUUID }, endString, customUUID);
}
public decodeModelId(uuid: string): FileUUIDData | undefined {
return this.manager.decode(uuid);
}
public encode(peer: Peer, msgId: string, elementId: string, fileUUID: string = "", customUUID?: string): string {
return this.manager.encode({ peer, msgId, elementId, fileUUID }, "", customUUID);
}
public decode(uuid: string): FileUUIDData | undefined {
return this.manager.decode(uuid);
}
}
export const FileNapCatOneBotUUID = new FileNapCatOneBotUUIDWrap();