Update downloading

This commit is contained in:
2021-12-27 23:34:46 +03:00
parent d41c1268d0
commit 5177fb1331
5 changed files with 61 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import { Context, Telegraf, Markup } from 'telegraf';
import { BotState } from '@/bots/manager';
import { BotState, Cache } from '@/bots/manager';
import env from '@/config';
@@ -11,6 +11,7 @@ import * as CallbackData from "./callback_data";
import * as BookLibrary from "./services/book_library";
import { CachedMessage, getBookCache } from './services/book_cache';
import { getBookCacheBuffer } from './services/book_cache_buffer';
import { download } from './services/downloader';
import { formatBook, formatAuthor, formatSequence } from './format';
import { getPaginatedMessage, registerPaginationCommand } from './utils';
import { getRandomKeyboard } from './keyboard';
@@ -109,10 +110,17 @@ export async function createApprovedBot(token: string, state: BotState): Promise
let cache: CachedMessage;
if (state.privileged) {
if (state.cache === Cache.ORIGINAL) {
cache = await getBookCache(parseInt(id), format);
} else {
} else if (state.cache === Cache.BUFFER) {
cache = await getBookCacheBuffer(parseInt(id), format);
} else {
const book = await BookLibrary.getBookById(parseInt(id));
const data = await download(book.source.id, book.remote_id, format);
ctx.telegram.sendDocument(ctx.message.chat.id, data, {
reply_to_message_id: ctx.message.message_id
})
return;
}
ctx.telegram.copyMessage(ctx.message.chat.id, cache.chat_id, cache.message_id, {

View File

@@ -40,6 +40,19 @@ export interface Book extends AuthorBook {
}
export interface Source {
id: number;
name: string;
}
export interface DetailBook extends Book {
source: Source;
remote_id: number;
is_deleted: boolean;
}
export interface Author {
id: number;
last_name: string;
@@ -76,6 +89,11 @@ async function _makeRequest<T>(url: string, searchParams?: string | Record<strin
}
export async function getBookById(book_id: number): Promise<DetailBook> {
return _makeRequest<DetailBook>(`/api/v1/books/${book_id}`);
}
export async function searchByBookName(query: string, page: number): Promise<Page<Book>> {
return _makeRequest<Page<Book>>(`/api/v1/books/search/${query}`, {
page: page,

View File

@@ -0,0 +1,23 @@
import got from 'got';
import env from '@/config';
export interface DownloadedFile {
source: Buffer;
filename: string;
}
export async function download(source_id: number, remote_id: number, file_type: string): Promise<DownloadedFile> {
const response = await got<string>(`${env.DOWNLOADER_URL}/download/${source_id}/${remote_id}/${file_type}`, {
headers: {
'Authorization': env.DOWNLOADER_API_KEY,
},
});
return {
source: response.rawBody,
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
}
}

View File

@@ -8,12 +8,17 @@ import env from '@/config';
import getBot, { BotStatuses } from './factory/index';
import { Server } from 'http';
export enum Cache {
ORIGINAL = "original",
BUFFER = "buffer",
NO_CACHE = "no_cache"
}
export interface BotState {
id: number;
token: string;
status: BotStatuses;
privileged: boolean;
cache: Cache;
created_time: string;
}

View File

@@ -12,5 +12,7 @@ export default cleanEnv(process.env, {
CACHE_SERVER_URL: str(),
CACHE_SERVER_API_KEY: str(),
BUFFER_SERVER_URL: str(),
BUFFER_SERVER_API_KEY: str()
BUFFER_SERVER_API_KEY: str(),
DOWNLOADER_URL: str(),
DOWNLOADER_API_KEY: str(),
});