mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix downlaoding fail
This commit is contained in:
@@ -8,6 +8,12 @@ import { BotState, Cache } from '@/bots/manager';
|
||||
async function _sendFile(ctx: Context, state: BotState, chatId: number, id: number, format: string) {
|
||||
const sendWithDownloadFromChannel = async () => {
|
||||
const data = await downloadFromCache(id, format);
|
||||
|
||||
if (data === null) {
|
||||
await ctx.reply("Ошибка скачивания книги. Попробуйте позже");
|
||||
return
|
||||
}
|
||||
|
||||
await ctx.telegram.sendDocument(chatId, { source: data.source, filename: data.filename }, { caption: data.caption });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import got from 'got';
|
||||
import got, { Response } from 'got';
|
||||
import { decode } from 'js-base64';
|
||||
|
||||
import env from '@/config';
|
||||
@@ -59,15 +59,20 @@ export interface DownloadedFile {
|
||||
caption: string;
|
||||
}
|
||||
|
||||
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile> {
|
||||
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile | null> {
|
||||
const readStream = got.stream.get(`${env.CACHE_SERVER_URL}/api/v1/download/${bookId}/${fileType}`, {
|
||||
headers: {
|
||||
'Authorization': env.CACHE_SERVER_API_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
return new Promise<DownloadedFile>((resolve, reject) => {
|
||||
readStream.on("response", async response => {
|
||||
return new Promise<DownloadedFile | null>((resolve, reject) => {
|
||||
readStream.on("response", async (response: Response) => {
|
||||
if (response.statusCode !== 200) {
|
||||
resolve(null);
|
||||
return
|
||||
}
|
||||
|
||||
const captionData = response.headers['x-caption-b64'];
|
||||
|
||||
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
||||
|
||||
Reference in New Issue
Block a user