fix authors?
This commit is contained in:
69
src/bot.ts
69
src/bot.ts
@@ -272,6 +272,7 @@ export class QuixoticBot {
|
||||
|
||||
// Public method for external API calls
|
||||
public async sendAudioFile(chatId: number, audioUrl: string, title: string, performer?: string, thumbnail?: string): Promise<void> {
|
||||
logger.debug(`sendAudioFile called with performer: ${performer}, thumbnail: ${thumbnail}`);
|
||||
return this.sendAudioFileInternal(chatId, audioUrl, title, performer, thumbnail);
|
||||
}
|
||||
|
||||
@@ -279,7 +280,8 @@ export class QuixoticBot {
|
||||
try {
|
||||
logger.telegram('Sending audio', `${title} to chat ${chatId}`);
|
||||
logger.debug(`File source: ${audioUrlOrPath}`);
|
||||
logger.debug(`Thumbnail: ${thumbnail}`);
|
||||
logger.debug(`Performer: ${performer || 'Not provided'}`);
|
||||
logger.debug(`Thumbnail: ${thumbnail || 'Not provided'}`);
|
||||
|
||||
// Check if it's a URL or local file path
|
||||
const isUrl = audioUrlOrPath.startsWith('http');
|
||||
@@ -318,25 +320,70 @@ export class QuixoticBot {
|
||||
let thumbnailPath: string | undefined;
|
||||
if (thumbnail && thumbnail.startsWith('http')) {
|
||||
try {
|
||||
logger.debug('Downloading thumbnail...');
|
||||
logger.debug(`Downloading thumbnail from: ${thumbnail}`);
|
||||
const thumbnailFilename = `thumb_${Date.now()}.jpg`;
|
||||
thumbnailPath = path.join(process.cwd(), 'downloads', thumbnailFilename);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const file = fs.createWriteStream(thumbnailPath);
|
||||
https.get(thumbnail, (response: any) => {
|
||||
response.pipe(file);
|
||||
file.on('finish', () => {
|
||||
const file = fs.createWriteStream(thumbnailPath!);
|
||||
|
||||
// Handle both http and https
|
||||
const protocol = thumbnail.startsWith('https') ? https : require('http');
|
||||
|
||||
const request = protocol.get(thumbnail, (response: any) => {
|
||||
// Follow redirects
|
||||
if (response.statusCode === 301 || response.statusCode === 302) {
|
||||
const redirectUrl = response.headers.location;
|
||||
logger.debug(`Following redirect to: ${redirectUrl}`);
|
||||
file.close();
|
||||
resolve();
|
||||
});
|
||||
}).on('error', (err: any) => {
|
||||
fs.unlink(thumbnailPath, () => {});
|
||||
fs.unlink(thumbnailPath!, () => {});
|
||||
|
||||
const redirectProtocol = redirectUrl.startsWith('https') ? https : require('http');
|
||||
redirectProtocol.get(redirectUrl, (redirectResponse: any) => {
|
||||
redirectResponse.pipe(file);
|
||||
file.on('finish', () => {
|
||||
file.close();
|
||||
logger.success(`Thumbnail downloaded: ${thumbnailPath} (${fs.statSync(thumbnailPath!).size} bytes)`);
|
||||
resolve();
|
||||
});
|
||||
}).on('error', (err: any) => {
|
||||
file.close();
|
||||
fs.unlink(thumbnailPath!, () => {});
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
response.pipe(file);
|
||||
file.on('finish', () => {
|
||||
file.close();
|
||||
const fileSize = fs.statSync(thumbnailPath!).size;
|
||||
logger.success(`Thumbnail downloaded: ${thumbnailPath} (${fileSize} bytes)`);
|
||||
|
||||
// Check if file is valid (at least 1KB)
|
||||
if (fileSize < 1000) {
|
||||
logger.warn('Thumbnail file too small, may be invalid');
|
||||
fs.unlink(thumbnailPath!, () => {});
|
||||
thumbnailPath = undefined;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
request.on('error', (err: any) => {
|
||||
file.close();
|
||||
fs.unlink(thumbnailPath!, () => {});
|
||||
reject(err);
|
||||
});
|
||||
|
||||
// Set timeout for thumbnail download
|
||||
request.setTimeout(10000, () => {
|
||||
request.destroy();
|
||||
file.close();
|
||||
fs.unlink(thumbnailPath!, () => {});
|
||||
reject(new Error('Thumbnail download timeout'));
|
||||
});
|
||||
});
|
||||
|
||||
logger.success(`Thumbnail downloaded: ${thumbnailPath}`);
|
||||
} catch (thumbError: any) {
|
||||
logger.warn('Failed to download thumbnail:', thumbError.message);
|
||||
thumbnailPath = undefined;
|
||||
|
||||
Reference in New Issue
Block a user