-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileHandler.js
More file actions
565 lines (482 loc) Β· 17.9 KB
/
FileHandler.js
File metadata and controls
565 lines (482 loc) Β· 17.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
const fs = require('fs');
const https = require('https');
const os = require('os');
const path = require('path');
/**
* File Handler - Universal file processing and management for Telegram
* Handles document, video, audio, and sticker files with Claude integration
* Extends existing ImageHandler and VoiceMessageHandler functionality
*/
class FileHandler {
constructor(bot, sessionManager, activityIndicator, mainBot) {
this.bot = bot;
this.sessionManager = sessionManager;
this.activityIndicator = activityIndicator;
this.mainBot = mainBot; // Reference to main bot for concat mode access
// Supported file types with size limits (in bytes)
this.supportedTypes = {
document: {
maxSize: 50 * 1024 * 1024, // 50MB
extensions: ['.pdf', '.doc', '.docx', '.txt', '.md', '.json', '.xml', '.csv', '.xlsx', '.xls', '.ppt', '.pptx'],
description: 'Documents'
},
video: {
maxSize: 100 * 1024 * 1024, // 100MB
extensions: ['.mp4', '.avi', '.mov', '.wmv', '.flv', '.webm', '.mkv'],
description: 'Videos'
},
audio: {
maxSize: 50 * 1024 * 1024, // 50MB
extensions: ['.mp3', '.wav', '.aac', '.flac', '.m4a', '.ogg'],
description: 'Audio files'
},
animation: {
maxSize: 20 * 1024 * 1024, // 20MB
extensions: ['.gif', '.mp4'],
description: 'Animations/GIFs'
},
sticker: {
maxSize: 5 * 1024 * 1024, // 5MB
extensions: ['.webp', '.tgs'],
description: 'Stickers'
}
};
this.tempFiles = new Map(); // Track temp files for cleanup
}
/**
* Handle document message with optional caption
*/
async handleDocumentMessage(msg, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
const document = msg.document;
const caption = msg.caption || '';
console.log(`[User ${userId}] Document message: "${document.file_name}" (${document.file_size} bytes) with caption: "${caption}"`);
return await this.processFileMessage(msg, 'document', document, caption, processUserMessageCallback);
}
/**
* Handle video message with optional caption
*/
async handleVideoMessage(msg, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
const video = msg.video;
const caption = msg.caption || '';
console.log(`[User ${userId}] Video message: ${video.width}x${video.height} (${video.file_size} bytes) with caption: "${caption}"`);
return await this.processFileMessage(msg, 'video', video, caption, processUserMessageCallback);
}
/**
* Handle audio message with optional caption
*/
async handleAudioMessage(msg, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
const audio = msg.audio;
const caption = msg.caption || '';
console.log(`[User ${userId}] Audio message: "${audio.title || 'Unknown'}" (${audio.file_size} bytes) with caption: "${caption}"`);
return await this.processFileMessage(msg, 'audio', audio, caption, processUserMessageCallback);
}
/**
* Handle animation/GIF message with optional caption
*/
async handleAnimationMessage(msg, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
const animation = msg.animation;
const caption = msg.caption || '';
console.log(`[User ${userId}] Animation message: ${animation.width}x${animation.height} (${animation.file_size} bytes) with caption: "${caption}"`);
return await this.processFileMessage(msg, 'animation', animation, caption, processUserMessageCallback);
}
/**
* Handle sticker message
*/
async handleStickerMessage(msg, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
const sticker = msg.sticker;
console.log(`[User ${userId}] Sticker message: ${sticker.emoji || 'No emoji'} (${sticker.file_size} bytes)`);
return await this.processFileMessage(msg, 'sticker', sticker, sticker.emoji || '', processUserMessageCallback);
}
/**
* Universal file processing method
*/
async processFileMessage(msg, fileType, fileObject, caption, processUserMessageCallback) {
const userId = msg.from.id;
const chatId = msg.chat.id;
let filePath = null;
try {
// Validate file type and size
const validation = this.validateFile(fileType, fileObject);
if (!validation.valid) {
await this.mainBot.safeSendMessage(chatId, `β **File Not Supported**\n\n${validation.error}`);
return;
}
console.log(`[User ${userId}] Processing ${fileType}: ${fileObject.file_id}`);
// Download the file to temp directory
filePath = await this.downloadFile(fileObject.file_id, fileType, userId, fileObject);
console.log(`[User ${userId}] Downloaded ${fileType} to temp: ${filePath}`);
// Create message for Claude with file path and caption
let message = this.createFileMessage(fileType, fileObject, filePath, caption);
console.log(`[User ${userId}] Sending to Claude: "${message}"`);
// Check if concat mode is enabled
if (this.mainBot && this.mainBot.getConcatModeStatus(userId)) {
// Add file to buffer
const bufferSize = await this.mainBot.addToMessageBuffer(userId, {
type: fileType,
content: caption,
filePath: filePath,
fileInfo: {
name: fileObject.file_name || `${fileType}_file`,
size: fileObject.file_size,
mimeType: fileObject.mime_type
}
});
const fileEmoji = this.getFileEmoji(fileType);
await this.mainBot.safeSendMessage(chatId,
`${fileEmoji} **${this.supportedTypes[fileType].description.slice(0, -1)} Added to Buffer**\n\n` +
`π **File:** ${fileObject.file_name || 'Unknown'}\n` +
`${caption ? `π **Caption:** ${caption}\n` : ''}` +
`\nπ **Buffer:** ${bufferSize} message${bufferSize > 1 ? 's' : ''}`, {
reply_markup: this.mainBot.keyboardHandlers.createReplyKeyboard(userId)
}
);
return;
}
// Process message with temp file cleanup tracking
await this.processWithCleanupTracking(message, userId, chatId, filePath, processUserMessageCallback);
} catch (error) {
console.error(`[User ${userId}] Error processing ${fileType}:`, error);
// Clean up temp file on error
if (filePath && fs.existsSync(filePath)) {
try {
fs.unlinkSync(filePath);
this.tempFiles.delete(filePath);
console.log(`[User ${userId}] Cleaned up temp ${fileType} on error: ${filePath}`);
} catch (cleanupError) {
console.error(`[User ${userId}] Failed to cleanup temp ${fileType}:`, cleanupError);
}
}
await this.sessionManager.sendError(chatId, error);
}
}
/**
* Validate file type and size
*/
validateFile(fileType, fileObject) {
const typeConfig = this.supportedTypes[fileType];
if (!typeConfig) {
return { valid: false, error: `Unsupported file type: ${fileType}` };
}
// Check file size
if (fileObject.file_size > typeConfig.maxSize) {
const maxSizeMB = Math.round(typeConfig.maxSize / (1024 * 1024));
const fileSizeMB = Math.round(fileObject.file_size / (1024 * 1024));
return {
valid: false,
error: `File too large: ${fileSizeMB}MB (max ${maxSizeMB}MB for ${typeConfig.description.toLowerCase()})`
};
}
// Check file extension if available
if (fileObject.file_name) {
const extension = path.extname(fileObject.file_name).toLowerCase();
if (extension && !typeConfig.extensions.includes(extension)) {
return {
valid: false,
error: `Unsupported file extension: ${extension}\n\nSupported: ${typeConfig.extensions.join(', ')}`
};
}
}
return { valid: true };
}
/**
* Create appropriate message for Claude based on file type
*/
createFileMessage(fileType, fileObject, filePath, caption) {
const fileName = fileObject.file_name || `${fileType}_file`;
const fileSize = Math.round(fileObject.file_size / 1024); // KB
let message = '';
if (caption.trim()) {
message = `${caption.trim()}\n\n`;
}
switch (fileType) {
case 'document':
message += `Document file: ${filePath}\nFilename: ${fileName}\nSize: ${fileSize}KB`;
if (fileObject.mime_type) {
message += `\nMIME Type: ${fileObject.mime_type}`;
}
break;
case 'video':
message += `Video file: ${filePath}\nFilename: ${fileName}\nSize: ${fileSize}KB`;
if (fileObject.width && fileObject.height) {
message += `\nDimensions: ${fileObject.width}x${fileObject.height}`;
}
if (fileObject.duration) {
message += `\nDuration: ${fileObject.duration}s`;
}
break;
case 'audio':
message += `Audio file: ${filePath}\nFilename: ${fileName}\nSize: ${fileSize}KB`;
if (fileObject.duration) {
message += `\nDuration: ${fileObject.duration}s`;
}
if (fileObject.title) {
message += `\nTitle: ${fileObject.title}`;
}
if (fileObject.performer) {
message += `\nPerformer: ${fileObject.performer}`;
}
break;
case 'animation':
message += `Animation/GIF file: ${filePath}\nFilename: ${fileName}\nSize: ${fileSize}KB`;
if (fileObject.width && fileObject.height) {
message += `\nDimensions: ${fileObject.width}x${fileObject.height}`;
}
break;
case 'sticker':
message += `Sticker file: ${filePath}\nSize: ${fileSize}KB`;
if (fileObject.emoji) {
message += `\nEmoji: ${fileObject.emoji}`;
}
if (fileObject.set_name) {
message += `\nSticker Set: ${fileObject.set_name}`;
}
break;
default:
message += `File: ${filePath}\nFilename: ${fileName}\nSize: ${fileSize}KB`;
}
if (!caption.trim()) {
message = `Please analyze this ${fileType}: ${message}`;
}
return message;
}
/**
* Get appropriate emoji for file type
*/
getFileEmoji(fileType) {
const emojis = {
document: 'π',
video: 'π₯',
audio: 'π΅',
animation: 'ποΈ',
sticker: 'π¨'
};
return emojis[fileType] || 'π';
}
/**
* Download file from Telegram servers to temp directory
*/
async downloadFile(fileId, fileType, userId, fileObject) {
try {
// Get file info from Telegram
const file = await this.bot.getFile(fileId);
const fileUrl = `https://api.telegram.org/file/bot${this.bot.token}/${file.file_path}`;
// Use system temp directory
const tempDir = os.tmpdir();
// Generate unique filename in temp directory
const timestamp = Date.now();
const originalExtension = path.extname(file.file_path) ||
(fileObject.file_name ? path.extname(fileObject.file_name) : '') ||
this.getDefaultExtension(fileType, fileObject);
const filename = `telegram_${fileType}_${userId}_${timestamp}${originalExtension}`;
const filePath = path.join(tempDir, filename);
console.log(`Downloading ${fileType} from: ${fileUrl}`);
console.log(`Saving to temp file: ${filePath}`);
// Download the file
await this.downloadFromUrl(fileUrl, filePath);
// Track temp file for cleanup
this.tempFiles.set(filePath, {
userId,
type: fileType,
createdAt: Date.now()
});
return filePath;
} catch (error) {
console.error(`Error downloading ${fileType}:`, error);
throw new Error(`Failed to download ${fileType}: ${error.message}`);
}
}
/**
* Get default file extension based on type and MIME type
*/
getDefaultExtension(fileType, fileObject) {
if (fileObject.mime_type) {
const mimeExtensions = {
'application/pdf': '.pdf',
'text/plain': '.txt',
'application/json': '.json',
'video/mp4': '.mp4',
'audio/mpeg': '.mp3',
'audio/ogg': '.ogg',
'image/gif': '.gif',
'image/webp': '.webp'
};
if (mimeExtensions[fileObject.mime_type]) {
return mimeExtensions[fileObject.mime_type];
}
}
// Fallback extensions
const fallbacks = {
document: '.txt',
video: '.mp4',
audio: '.mp3',
animation: '.gif',
sticker: '.webp'
};
return fallbacks[fileType] || '';
}
/**
* Download file from URL to local path
*/
downloadFromUrl(url, filePath) {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(filePath);
https.get(url, (response) => {
if (response.statusCode !== 200) {
reject(new Error(`HTTP ${response.statusCode}: ${response.statusMessage}`));
return;
}
response.pipe(file);
file.on('finish', () => {
file.close();
resolve();
});
file.on('error', (error) => {
fs.unlink(filePath, () => {}); // Delete partial file
reject(error);
});
}).on('error', (error) => {
reject(error);
});
});
}
/**
* Process file message with temp file cleanup tracking
*/
async processWithCleanupTracking(text, userId, chatId, tempFilePath, processUserMessageCallback) {
// Get or create user session first
let session = this.sessionManager.getUserSession(userId);
if (!session) {
// First message - create new session
console.log(`[FileHandler] Creating new session for user ${userId}`);
session = await this.sessionManager.createUserSession(userId, chatId);
// Send session init message
const sessionInitText = 'π **New Session Started**\n\n' +
'Ready to process your requests with Claude CLI stream-json mode.\n\n' +
'π Session continuity with ID tracking\n' +
'π‘οΈ Auto-permissions enabled\n' +
'π Live TodoWrite updates active\n' +
'π Universal file analysis ready\n' +
'π Documents β’ π₯ Videos β’ π΅ Audio β’ ποΈ Animations β’ π¨ Stickers\n\n' +
'π‘ Use /end to close this session\n' +
'π Use /sessions to view history';
await this.sessionManager.safeSendMessage(chatId, sessionInitText);
}
// Store temp file path in session for cleanup after Claude completes
if (!session.tempFilePaths) {
session.tempFilePaths = [];
}
session.tempFilePaths.push(tempFilePath);
console.log(`[User ${userId}] Stored temp file path in session: ${tempFilePath}`);
try {
// Use the callback to process the message
await processUserMessageCallback(text, userId, chatId);
// Note: Temp files will be cleaned up in SessionManager when Claude completes
} catch (error) {
// Clean up temp file immediately on error during setup
if (tempFilePath && fs.existsSync(tempFilePath)) {
try {
fs.unlinkSync(tempFilePath);
this.tempFiles.delete(tempFilePath);
console.log(`[User ${userId}] Cleaned up temp file on setup error: ${tempFilePath}`);
} catch (cleanupError) {
console.error(`[User ${userId}] Failed to cleanup temp file:`, cleanupError);
}
}
// Remove temp file path from session
if (session && session.tempFilePaths) {
const index = session.tempFilePaths.indexOf(tempFilePath);
if (index > -1) {
session.tempFilePaths.splice(index, 1);
}
}
// Re-throw the error to maintain error handling flow
throw error;
}
}
/**
* Clean up temporary files (called by SessionManager)
*/
static cleanupTempFiles(session, userId) {
if (session && session.tempFilePaths) {
for (const filePath of session.tempFilePaths) {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
console.log(`[User ${userId}] Cleaned up temp file after Claude completion: ${filePath}`);
}
} catch (error) {
console.error(`[User ${userId}] Failed to cleanup temp file:`, error);
}
}
// Clear temp file paths from session
session.tempFilePaths = [];
}
}
/**
* Clean up old temporary files (maintenance task)
*/
cleanupOldTempFiles(maxAgeHours = 24) {
const maxAge = maxAgeHours * 60 * 60 * 1000; // Convert to milliseconds
const now = Date.now();
let cleanedCount = 0;
for (const [filePath, info] of this.tempFiles.entries()) {
if (now - info.createdAt > maxAge) {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
cleanedCount++;
}
} catch (error) {
console.error(`Failed to cleanup old temp file ${filePath}:`, error);
}
this.tempFiles.delete(filePath);
}
}
if (cleanedCount > 0) {
console.log(`π§Ή Cleaned up ${cleanedCount} old temporary files`);
}
}
/**
* Get handler statistics
*/
getStats() {
return {
handlerType: 'FileHandler',
tempDirectory: os.tmpdir(),
trackedTempFiles: this.tempFiles.size,
supportedTypes: Object.keys(this.supportedTypes)
};
}
/**
* Cleanup resources
*/
cleanup() {
// Clean up all tracked temp files
let cleanedCount = 0;
for (const [filePath] of this.tempFiles.entries()) {
try {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
cleanedCount++;
}
} catch (error) {
console.error(`Failed to cleanup temp file on shutdown:`, error);
}
}
this.tempFiles.clear();
if (cleanedCount > 0) {
console.log(`π§Ή FileHandler cleanup: removed ${cleanedCount} temp files`);
}
}
}
module.exports = FileHandler;