Skip to content

Commit

Permalink
refactor: consume async iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
Myphz committed Jan 2, 2024
1 parent 8c58d6f commit e9191cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
9 changes: 3 additions & 6 deletions src/routes/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FastifyPluginCallback } from "fastify";
import multipart from "@fastify/multipart";
import { getFileDownload, getFileLink, uploadFiles } from "../telegram";
import { readFile } from "fs/promises";
import { filesGeneratorToArray } from "../utils/array";

export const FilesAPI: FastifyPluginCallback = async (fastify, _, done) => {
await fastify.register(multipart, {
Expand All @@ -14,11 +14,8 @@ export const FilesAPI: FastifyPluginCallback = async (fastify, _, done) => {
});

fastify.post("/upload", async (req, res) => {
// saveRequestFiles saves files in tmp directory.
// This is to avoid working with req.files(), which is an async iterator... and a pain to work with.
// Files from saveRequestFiles() have a useless toBuffer method, so it needs to be replaced.
console.log("Saving file to disk...");
const files = (await req.saveRequestFiles()).map((file) => ({ ...file, toBuffer: () => readFile(file.filepath) }));
console.log("Loading files...");
const files = await filesGeneratorToArray(req.files());
console.log(`Start upload of ${files.length} files...`);
const ret = await uploadFiles(files);

Expand Down
10 changes: 4 additions & 6 deletions src/telegram/telegram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import {
TRIPS_BOT_API_TOKEN,
} from "../constants";
import { throwError } from "../utils/throw";
import { MultipartFile } from "@fastify/multipart";
import { splitArrayIntoChunks } from "../utils/array";
import { LoadedFile, splitArrayIntoChunks } from "../utils/array";

// Disable warning
process.env["NTBA_FIX_350"] = "1";

const bot = new TelegramBot(TRIPS_BOT_API_TOKEN, { filepath: false });

async function uploadFileGroup(files: MultipartFile[], media: InputMediaPhoto[]) {
async function uploadFileGroup(files: LoadedFile[], media: InputMediaPhoto[]) {
return (await bot.sendMediaGroup(CHAT_ID, media)).reduce(
(prev, curr, i) => ({
...prev,
Expand All @@ -24,12 +23,11 @@ async function uploadFileGroup(files: MultipartFile[], media: InputMediaPhoto[])
);
}

export async function uploadFiles(files: MultipartFile[]) {
export async function uploadFiles(files: LoadedFile[]) {
let ret: Record<string, string> = {};
console.log("Reading files from disk...");
// This library has incorrect typescript typings... InputMediaDocument missing
const media = (await Promise.all(
files.map(async (file) => ({ media: await file.toBuffer(), type: "document" })),
files.map(async (file) => ({ media: file.content, type: "document" })),
)) as unknown as InputMediaPhoto[];
const fileChunks = splitArrayIntoChunks(files, TELEGRAM_MAX_PHOTOS_PER_MESSAGE);
const mediaChunks = splitArrayIntoChunks(media, TELEGRAM_MAX_PHOTOS_PER_MESSAGE);
Expand Down
12 changes: 12 additions & 0 deletions src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { MultipartFile } from "@fastify/multipart";

export type LoadedFile = MultipartFile & { content: Buffer };

export function splitArrayIntoChunks<T>(arr: T[], chunkSize: number): T[][] {
const result: T[][] = [];

Expand All @@ -7,3 +11,11 @@ export function splitArrayIntoChunks<T>(arr: T[], chunkSize: number): T[][] {

return result;
}

export async function filesGeneratorToArray(files: AsyncIterableIterator<MultipartFile>) {
const out: LoadedFile[] = [];
for await (const file of files) {
out.push({ ...file, content: await file.toBuffer() });
}
return out;
}

0 comments on commit e9191cd

Please sign in to comment.