2026-04-20 20:43:42 +00:00
|
|
|
import fs from 'node:fs';
|
2026-05-21 23:35:50 +00:00
|
|
|
import * as fsPromises from 'node:fs/promises';
|
2026-04-20 20:43:42 +00:00
|
|
|
import path from 'node:path';
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
import type { SiprouterStorage } from './storage.ts';
|
|
|
|
|
|
2026-04-20 20:43:42 +00:00
|
|
|
export interface IFaxBoxConfig {
|
|
|
|
|
id: string;
|
|
|
|
|
enabled: boolean;
|
|
|
|
|
maxMessages?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface IFaxMessage {
|
|
|
|
|
id: string;
|
|
|
|
|
boxId: string;
|
|
|
|
|
callerNumber?: string;
|
|
|
|
|
timestamp: number;
|
|
|
|
|
fileName: string;
|
2026-05-21 23:35:50 +00:00
|
|
|
objectKey?: string;
|
2026-04-20 20:43:42 +00:00
|
|
|
completionCode?: number | null;
|
|
|
|
|
completionLabel?: string | null;
|
|
|
|
|
pageCount?: number;
|
|
|
|
|
bitRate?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class FaxBoxManager {
|
|
|
|
|
private boxes = new Map<string, IFaxBoxConfig>();
|
2026-05-21 23:35:50 +00:00
|
|
|
private messagesByBox = new Map<string, IFaxMessage[]>();
|
2026-04-20 20:43:42 +00:00
|
|
|
private readonly basePath: string;
|
|
|
|
|
private readonly log: (msg: string) => void;
|
2026-05-21 23:35:50 +00:00
|
|
|
private readonly storage: SiprouterStorage;
|
2026-04-20 20:43:42 +00:00
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
constructor(log: (msg: string) => void, storageArg: SiprouterStorage) {
|
2026-04-20 20:43:42 +00:00
|
|
|
this.basePath = path.join(process.cwd(), '.nogit', 'fax', 'inboxes');
|
|
|
|
|
this.log = log;
|
2026-05-21 23:35:50 +00:00
|
|
|
this.storage = storageArg;
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
async init(faxBoxConfigs: IFaxBoxConfig[]): Promise<void> {
|
2026-04-20 20:43:42 +00:00
|
|
|
this.boxes.clear();
|
|
|
|
|
|
|
|
|
|
for (const cfg of faxBoxConfigs) {
|
|
|
|
|
cfg.enabled ??= true;
|
|
|
|
|
cfg.maxMessages ??= 50;
|
|
|
|
|
this.boxes.set(cfg.id, cfg);
|
2026-05-21 23:35:50 +00:00
|
|
|
this.messagesByBox.set(cfg.id, await this.loadMessages(cfg.id));
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
await fsPromises.mkdir(this.basePath, { recursive: true });
|
2026-04-20 20:43:42 +00:00
|
|
|
this.log(`[faxbox] initialized ${this.boxes.size} fax box(es)`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getBox(boxId: string): IFaxBoxConfig | null {
|
|
|
|
|
return this.boxes.get(boxId) ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getBoxDir(boxId: string): string {
|
|
|
|
|
return path.join(this.basePath, boxId);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
async prepareOutboundFaxFile(filePathArg: string): Promise<string> {
|
|
|
|
|
const localPath = path.isAbsolute(filePathArg) ? filePathArg : path.join(process.cwd(), filePathArg);
|
|
|
|
|
await fsPromises.access(localPath);
|
|
|
|
|
return localPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async addMessage(
|
2026-04-20 20:43:42 +00:00
|
|
|
boxId: string,
|
|
|
|
|
info: {
|
|
|
|
|
callerNumber?: string;
|
|
|
|
|
fileName: string;
|
|
|
|
|
completionCode?: number | null;
|
|
|
|
|
completionLabel?: string | null;
|
|
|
|
|
pageCount?: number;
|
|
|
|
|
bitRate?: number;
|
|
|
|
|
},
|
2026-05-21 23:35:50 +00:00
|
|
|
): Promise<void> {
|
|
|
|
|
const id = crypto.randomUUID();
|
|
|
|
|
const localPath = path.isAbsolute(info.fileName) ? info.fileName : path.join(process.cwd(), info.fileName);
|
|
|
|
|
const objectKey = await this.storage.putFileObject(`fax/inboxes/${boxId}/${id}.tif`, localPath);
|
|
|
|
|
|
2026-04-20 20:43:42 +00:00
|
|
|
const msg: IFaxMessage = {
|
2026-05-21 23:35:50 +00:00
|
|
|
id,
|
2026-04-20 20:43:42 +00:00
|
|
|
boxId,
|
|
|
|
|
callerNumber: info.callerNumber,
|
|
|
|
|
timestamp: Date.now(),
|
2026-05-21 23:35:50 +00:00
|
|
|
fileName: path.basename(localPath),
|
|
|
|
|
objectKey,
|
2026-04-20 20:43:42 +00:00
|
|
|
completionCode: info.completionCode ?? null,
|
|
|
|
|
completionLabel: info.completionLabel ?? null,
|
|
|
|
|
pageCount: info.pageCount,
|
|
|
|
|
bitRate: info.bitRate,
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
const messages = this.getMessages(boxId);
|
2026-04-20 20:43:42 +00:00
|
|
|
messages.unshift(msg);
|
2026-05-21 23:35:50 +00:00
|
|
|
await this.enforceLimit(boxId, messages);
|
|
|
|
|
await this.writeMessages(boxId, messages);
|
|
|
|
|
await fsPromises.rm(localPath, { force: true }).catch(() => {});
|
2026-04-20 20:43:42 +00:00
|
|
|
this.log(`[faxbox] saved fax ${msg.id} in box "${msg.boxId}" (${msg.fileName})`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMessages(boxId: string): IFaxMessage[] {
|
2026-05-21 23:35:50 +00:00
|
|
|
return [...(this.messagesByBox.get(boxId) || [])];
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMessage(boxId: string, messageId: string): IFaxMessage | null {
|
2026-05-21 23:35:50 +00:00
|
|
|
const messages = this.messagesByBox.get(boxId) || [];
|
|
|
|
|
return messages.find((m) => m.id === messageId) ?? null;
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
async getMessageFilePath(boxId: string, messageId: string): Promise<string | null> {
|
2026-04-20 20:43:42 +00:00
|
|
|
const msg = this.getMessage(boxId, messageId);
|
|
|
|
|
if (!msg) return null;
|
2026-05-21 23:35:50 +00:00
|
|
|
if (msg.objectKey) {
|
|
|
|
|
return await this.storage.getObjectAsCachedFile(msg.objectKey, msg.fileName);
|
|
|
|
|
}
|
2026-04-20 20:43:42 +00:00
|
|
|
const filePath = path.join(this.getBoxDir(boxId), msg.fileName);
|
|
|
|
|
return fs.existsSync(filePath) ? filePath : null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
async deleteMessage(boxId: string, messageId: string): Promise<boolean> {
|
|
|
|
|
const messages = this.messagesByBox.get(boxId) || [];
|
2026-04-20 20:43:42 +00:00
|
|
|
const idx = messages.findIndex((m) => m.id === messageId);
|
|
|
|
|
if (idx === -1) return false;
|
|
|
|
|
|
|
|
|
|
const msg = messages[idx];
|
2026-05-21 23:35:50 +00:00
|
|
|
await this.storage.removeObject(msg.objectKey);
|
|
|
|
|
if (!msg.objectKey) {
|
|
|
|
|
await fsPromises.rm(path.join(this.getBoxDir(boxId), msg.fileName), { force: true }).catch(() => {});
|
|
|
|
|
}
|
2026-04-20 20:43:42 +00:00
|
|
|
|
|
|
|
|
messages.splice(idx, 1);
|
2026-05-21 23:35:50 +00:00
|
|
|
await this.writeMessages(boxId, messages);
|
2026-04-20 20:43:42 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
private async enforceLimit(boxId: string, messages: IFaxMessage[]): Promise<void> {
|
|
|
|
|
const box = this.boxes.get(boxId);
|
|
|
|
|
const maxMessages = box?.maxMessages ?? 50;
|
|
|
|
|
while (messages.length > maxMessages) {
|
|
|
|
|
const old = messages.pop()!;
|
|
|
|
|
await this.storage.removeObject(old.objectKey);
|
|
|
|
|
if (!old.objectKey) {
|
|
|
|
|
await fsPromises.rm(path.join(this.getBoxDir(boxId), old.fileName), { force: true }).catch(() => {});
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
private async loadMessages(boxId: string): Promise<IFaxMessage[]> {
|
|
|
|
|
const storedMessages = await this.storage.getFaxMessages(boxId);
|
|
|
|
|
if (storedMessages.length) return await this.ensureMessageObjects(boxId, storedMessages);
|
|
|
|
|
|
|
|
|
|
const filePath = path.join(this.getBoxDir(boxId), 'messages.json');
|
2026-04-20 20:43:42 +00:00
|
|
|
try {
|
|
|
|
|
if (!fs.existsSync(filePath)) return [];
|
2026-05-21 23:35:50 +00:00
|
|
|
const raw = await fsPromises.readFile(filePath, 'utf8');
|
|
|
|
|
const legacyMessages = await this.ensureMessageObjects(boxId, JSON.parse(raw) as IFaxMessage[]);
|
|
|
|
|
await this.storage.writeFaxMessages(boxId, legacyMessages);
|
|
|
|
|
return legacyMessages;
|
2026-04-20 20:43:42 +00:00
|
|
|
} catch {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 23:35:50 +00:00
|
|
|
private async ensureMessageObjects(boxId: string, messages: IFaxMessage[]): Promise<IFaxMessage[]> {
|
|
|
|
|
let changed = false;
|
|
|
|
|
|
|
|
|
|
for (const msg of messages) {
|
|
|
|
|
if (!msg.id) {
|
|
|
|
|
msg.id = crypto.randomUUID();
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
if (msg.objectKey) continue;
|
|
|
|
|
|
|
|
|
|
const localPath = path.isAbsolute(msg.fileName) ? msg.fileName : path.join(this.getBoxDir(boxId), msg.fileName);
|
|
|
|
|
if (!fs.existsSync(localPath)) continue;
|
|
|
|
|
|
|
|
|
|
const extension = path.extname(localPath) || '.tif';
|
|
|
|
|
msg.objectKey = await this.storage.putFileObject(`fax/inboxes/${boxId}/${msg.id}${extension}`, localPath);
|
|
|
|
|
msg.fileName = path.basename(localPath);
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
|
await this.storage.writeFaxMessages(boxId, messages);
|
|
|
|
|
this.log(`[faxbox] migrated legacy messages for box "${boxId}" to smartbucket`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return messages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async writeMessages(boxId: string, messages: IFaxMessage[]): Promise<void> {
|
|
|
|
|
this.messagesByBox.set(boxId, [...messages]);
|
|
|
|
|
await this.storage.writeFaxMessages(boxId, messages);
|
2026-04-20 20:43:42 +00:00
|
|
|
}
|
|
|
|
|
}
|