mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Fix sending timeout
This commit is contained in:
@@ -67,9 +67,12 @@ export async function downloadFromCache(bookId: number, fileType: string): Promi
|
|||||||
});
|
});
|
||||||
|
|
||||||
return new Promise<DownloadedFile | null>((resolve, reject) => {
|
return new Promise<DownloadedFile | null>((resolve, reject) => {
|
||||||
readStream.on("response", async (response: Response) => {
|
let timeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
const resolver = async (response: Response) => {
|
||||||
if (response.statusCode !== 200) {
|
if (response.statusCode !== 200) {
|
||||||
resolve(null);
|
resolve(null);
|
||||||
|
if (timeout) clearTimeout(timeout);
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,11 +80,20 @@ export async function downloadFromCache(bookId: number, fileType: string): Promi
|
|||||||
|
|
||||||
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
||||||
|
|
||||||
|
if (timeout) clearTimeout(timeout);
|
||||||
|
|
||||||
return resolve({
|
return resolve({
|
||||||
source: readStream,
|
source: readStream,
|
||||||
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
|
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
|
||||||
caption: decode(captionData),
|
caption: decode(captionData),
|
||||||
})
|
})
|
||||||
});
|
}
|
||||||
|
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
readStream.off("response", resolver);
|
||||||
|
resolve(null);
|
||||||
|
}, 60_000);
|
||||||
|
|
||||||
|
readStream.on("response", resolver);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user