Optimize file uploading

This commit is contained in:
2022-02-06 14:25:00 +03:00
parent a1d8245572
commit 69f8d492a2
3 changed files with 40 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ export const SETTINGS_MESSAGE = 'Настройки:';
export const SEARCH_MESSAGE = 'Что ищем?';
export const BOOKS_NOT_FOUND = "Книги для не найдены.";
export const BOOKS_NOT_FOUND = "Книги не найдены.";
export const AUTHORS_NOT_FOUND = "Авторы не найдены.";

View File

@@ -54,25 +54,29 @@ export async function clearBookCache(bookId: number, fileType: string): Promise<
}
export interface DownloadedFile {
source: Buffer;
source: NodeJS.ReadableStream;
filename: string;
caption: string;
}
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: {
'Authorization': env.CACHE_SERVER_API_KEY,
},
});
const captionData = response.headers['x-caption-b64'];
return new Promise<DownloadedFile>((resolve, reject) => {
readStream.on("response", async response => {
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 {
source: response.rawBody,
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
caption: decode(captionData),
}
return resolve({
source: readStream,
filename: (response.headers['content-disposition'] || '').replaceAll('"', "").split('filename=')[1],
caption: decode(captionData),
})
});
});
}

View File

@@ -4,35 +4,43 @@ import env from '@/config';
export interface DownloadedFile {
source: Buffer;
source: NodeJS.ReadableStream;
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}`, {
const readStream = got.stream.get(`${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]
}
return new Promise<DownloadedFile>((resolve, reject) => {
readStream.on("response", async response => {
resolve({
source: readStream,
filename: (response.headers['content-disposition'] || '').split('filename=')[1]
});
});
});
}
export async function downloadImage(path: string) {
try {
const response = await got(path);
export async function downloadImage(path: string): Promise<NodeJS.ReadableStream | null> {
const readStream = got.stream.get(path, {throwHttpErrors: false});
if (response.statusCode === 200) {
return response.rawBody;
} else {
return null;
}
} catch {
return null;
}
return new Promise<NodeJS.ReadableStream | null>((resolve, reject) => {
readStream.on("response", async response => {
if (response.statusCode === 200) {
resolve(readStream);
} else {
resolve(null);
}
});
readStream.once("error", error => {
resolve(null);
})
});
}