Move database work off the main thread on Node.js 24 and improve message handling and runtime diagnostics. Preserve migration, restart, and concurrency coverage with the async SQLite driver.
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import type { MessageElement } from '@snowluma/protocol/events';
|
|
import type { MediaStore } from './media-store';
|
|
import type { JsonObject } from './types';
|
|
|
|
export type MediaType = 'image' | 'record' | 'video';
|
|
|
|
export class MediaIndexer {
|
|
constructor(private readonly mediaStore: MediaStore) {}
|
|
|
|
async remember(
|
|
mediaType: MediaType,
|
|
element: MessageElement,
|
|
data: JsonObject,
|
|
isGroup: boolean,
|
|
sessionId: number,
|
|
): Promise<void> {
|
|
const url = typeof data.url === 'string' ? data.url : '';
|
|
const file = typeof data.file === 'string' ? data.file : '';
|
|
|
|
if (mediaType === 'image') {
|
|
await this.mediaStore.rememberImage({
|
|
file: file || element.fileId || '',
|
|
url,
|
|
fileSize: element.fileSize ?? 0,
|
|
fileName: element.fileId ?? '',
|
|
subType: element.subType ?? 0,
|
|
summary: element.summary ?? '',
|
|
imageUrl: element.imageUrl ?? '',
|
|
isGroup,
|
|
sessionId,
|
|
md5Hex: element.md5Hex,
|
|
sha1Hex: element.sha1Hex,
|
|
width: element.width,
|
|
height: element.height,
|
|
picFormat: element.picFormat,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (mediaType === 'record') {
|
|
await this.mediaStore.rememberRecord({
|
|
file: file || element.fileName || element.fileId || '',
|
|
fileId: element.fileId ?? '',
|
|
url,
|
|
fileSize: element.fileSize ?? 0,
|
|
fileName: element.fileName ?? '',
|
|
duration: element.duration ?? 0,
|
|
fileHash: element.fileHash ?? '',
|
|
mediaNode: element.mediaNode,
|
|
isGroup,
|
|
sessionId,
|
|
md5Hex: element.md5Hex,
|
|
sha1Hex: element.sha1Hex,
|
|
voiceFormat: element.voiceFormat,
|
|
});
|
|
return;
|
|
}
|
|
|
|
// video
|
|
await this.mediaStore.rememberVideo({
|
|
file: file || element.fileName || element.fileId || '',
|
|
fileId: element.fileId ?? '',
|
|
url,
|
|
fileSize: element.fileSize ?? 0,
|
|
fileName: element.fileName ?? '',
|
|
duration: element.duration ?? 0,
|
|
fileHash: element.fileHash ?? '',
|
|
mediaNode: element.mediaNode,
|
|
isGroup,
|
|
sessionId,
|
|
md5Hex: element.md5Hex,
|
|
sha1Hex: element.sha1Hex,
|
|
width: element.width,
|
|
height: element.height,
|
|
videoFormat: element.videoFormat,
|
|
});
|
|
}
|
|
}
|