Fix downlaoding fail

This commit is contained in:
2022-02-09 14:41:34 +03:00
parent 69f8d492a2
commit d635578d23
2 changed files with 15 additions and 4 deletions

View File

@@ -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 });
}

View File

@@ -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?');