feat: extend file type classification and implement computeHash

- Add sticker (webp) and video_note to classification mapping and file limits
- Handle unknown mime or caption fallback to document
- Implement computeHash utility using Bun.CryptoHasher for sha256 checksums
- Update and add comprehensive tests in file.test.ts

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-05-18 19:38:12 +07:00
co-authored by Claude Opus 4.7
parent 22c2133b24
commit 4e805689c1
2 changed files with 31 additions and 2 deletions
+11 -1
View File
@@ -5,12 +5,16 @@ const FILE_TYPES: Record<string, number> = {
audio: 200 * 1024 * 1024, // 200MB
voice: 200 * 1024 * 1024, // 200MB
animation: 2 * 1024 * 1024 * 1024, // 2GB
sticker: 10 * 1024 * 1024, // 10MB
video_note: 2 * 1024 * 1024 * 1024, // 2GB
};
export const getFileType = (mime: string | null, caption?: string): string => {
const mimeUpper = mime?.split('/')[0]?.toLowerCase();
const captionLower = caption?.toLowerCase();
if (mime?.toLowerCase() === 'image/webp' || captionLower?.includes('sticker')) return 'sticker';
if (captionLower?.includes('video_note')) return 'video_note';
if (mimeUpper === 'video') return 'video';
if (mimeUpper === 'audio') return 'audio';
if (mimeUpper === 'document') return 'document';
@@ -18,7 +22,7 @@ export const getFileType = (mime: string | null, caption?: string): string => {
if (captionLower?.includes('voice')) return 'voice';
if (captionLower?.includes('animation')) return 'animation';
return mimeUpper || 'document';
return mimeUpper === 'application' ? 'application' : 'document';
};
export const checkFileSize = (sizeBytes: number, fileType: string): boolean => {
@@ -95,3 +99,9 @@ export const extractMimeType = (msg: any, request: any): string => {
'application/octet-stream'
);
};
export const computeHash = (buffer: Buffer): string => {
const hasher = new Bun.CryptoHasher('sha256');
hasher.update(buffer);
return hasher.digest('hex');
};