mirror of
https://github.com/flibusta-apps/book_bot.git
synced 2025-12-06 15:35:35 +01:00
Optimize file uploading
This commit is contained in:
@@ -9,7 +9,7 @@ export const SETTINGS_MESSAGE = 'Настройки:';
|
|||||||
|
|
||||||
export const SEARCH_MESSAGE = 'Что ищем?';
|
export const SEARCH_MESSAGE = 'Что ищем?';
|
||||||
|
|
||||||
export const BOOKS_NOT_FOUND = "Книги для не найдены.";
|
export const BOOKS_NOT_FOUND = "Книги не найдены.";
|
||||||
|
|
||||||
export const AUTHORS_NOT_FOUND = "Авторы не найдены.";
|
export const AUTHORS_NOT_FOUND = "Авторы не найдены.";
|
||||||
|
|
||||||
|
|||||||
@@ -54,25 +54,29 @@ export async function clearBookCache(bookId: number, fileType: string): Promise<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface DownloadedFile {
|
export interface DownloadedFile {
|
||||||
source: Buffer;
|
source: NodeJS.ReadableStream;
|
||||||
filename: string;
|
filename: string;
|
||||||
caption: string;
|
caption: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile> {
|
export async function downloadFromCache(bookId: number, fileType: string): Promise<DownloadedFile> {
|
||||||
const response = await got<string>(`${env.CACHE_SERVER_URL}/api/v1/download/${bookId}/${fileType}`, {
|
const readStream = got.stream.get(`${env.CACHE_SERVER_URL}/api/v1/download/${bookId}/${fileType}`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': env.CACHE_SERVER_API_KEY,
|
'Authorization': env.CACHE_SERVER_API_KEY,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return new Promise<DownloadedFile>((resolve, reject) => {
|
||||||
|
readStream.on("response", async response => {
|
||||||
const captionData = response.headers['x-caption-b64'];
|
const captionData = response.headers['x-caption-b64'];
|
||||||
|
|
||||||
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
if (captionData === undefined || Array.isArray(captionData)) throw Error('No caption?');
|
||||||
|
|
||||||
return {
|
return resolve({
|
||||||
source: response.rawBody,
|
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),
|
||||||
}
|
})
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,35 +4,43 @@ import env from '@/config';
|
|||||||
|
|
||||||
|
|
||||||
export interface DownloadedFile {
|
export interface DownloadedFile {
|
||||||
source: Buffer;
|
source: NodeJS.ReadableStream;
|
||||||
filename: string;
|
filename: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function download(source_id: number, remote_id: number, file_type: string): Promise<DownloadedFile> {
|
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}`, {
|
const readStream = got.stream.get(`${env.DOWNLOADER_URL}/download/${source_id}/${remote_id}/${file_type}`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': env.DOWNLOADER_API_KEY,
|
'Authorization': env.DOWNLOADER_API_KEY,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return new Promise<DownloadedFile>((resolve, reject) => {
|
||||||
source: response.rawBody,
|
readStream.on("response", async response => {
|
||||||
|
resolve({
|
||||||
|
source: readStream,
|
||||||
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
|
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
|
||||||
}
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export async function downloadImage(path: string) {
|
export async function downloadImage(path: string): Promise<NodeJS.ReadableStream | null> {
|
||||||
try {
|
const readStream = got.stream.get(path, {throwHttpErrors: false});
|
||||||
const response = await got(path);
|
|
||||||
|
|
||||||
|
return new Promise<NodeJS.ReadableStream | null>((resolve, reject) => {
|
||||||
|
readStream.on("response", async response => {
|
||||||
if (response.statusCode === 200) {
|
if (response.statusCode === 200) {
|
||||||
return response.rawBody;
|
resolve(readStream);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
resolve(null);
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
readStream.once("error", error => {
|
||||||
|
resolve(null);
|
||||||
|
})
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user