From e7f855ca51f62db04c778c97687c75b1db545d9c Mon Sep 17 00:00:00 2001 From: ogomez-at-wiris Date: Thu, 24 Feb 2022 07:39:07 +0100 Subject: [PATCH] feat: Send pack files over multistream --- restclient_snippets/get_single_pack.http | 2 ++ src/interfaces/pack.ts | 1 + src/pack_api.ts | 20 ++++++++++++++-- src/services/pack_service.ts | 30 ++++++++++++++++++++---- 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 restclient_snippets/get_single_pack.http diff --git a/restclient_snippets/get_single_pack.http b/restclient_snippets/get_single_pack.http new file mode 100644 index 0000000..89d85f1 --- /dev/null +++ b/restclient_snippets/get_single_pack.http @@ -0,0 +1,2 @@ +# careful, this request will return a stream of all the files contained in the pack! +GET http://localhost:6060/getpack?name=default diff --git a/src/interfaces/pack.ts b/src/interfaces/pack.ts index e3f00c3..6f101e6 100644 --- a/src/interfaces/pack.ts +++ b/src/interfaces/pack.ts @@ -2,5 +2,6 @@ interface Pack { name: string; size: number; bpm: string; + files: string[]; } export default Pack; diff --git a/src/pack_api.ts b/src/pack_api.ts index 5001dfb..eea29d6 100644 --- a/src/pack_api.ts +++ b/src/pack_api.ts @@ -1,6 +1,8 @@ import { Request, Response, NextFunction } from "express"; import Pack from "./interfaces/pack"; import PackService from "./services/pack_service"; +import fs from "fs"; +const multistream = require("multistream"); export default class PackAPI { public static getPacks = async ( @@ -20,11 +22,25 @@ export default class PackAPI { ) => { if (!req.query.name) { return res.status(500).json({ - message: 'pack name not specified', + message: "pack name not specified", }); } + + const foundPack = PackService.findByName(req.query.name.toString()); + if (!foundPack) { + return res.status(500).json({ + message: "Pack does not exist", + }); + } + + // Create streams of all files in one line because fuck you I can + const streams = foundPack.files.map((file) => fs.createReadStream(file)); + + // Send the files to the client... + new multistream(streams).pipe(process.stdout); + console.log("files sent"); return res.status(200).json({ - message: 'ok', + message: "ok", }); }; } diff --git a/src/services/pack_service.ts b/src/services/pack_service.ts index e012599..e4f97a9 100644 --- a/src/services/pack_service.ts +++ b/src/services/pack_service.ts @@ -12,10 +12,13 @@ export default class PackService { public static getInitialList = async (): Promise => { let packs = await fs.readdirSync(PACK_BASE_PATH); - PackService.packList = await Promise.all(packs.map((pack) => PackService.getPackInfo(pack))) + PackService.packList = await Promise.all( + packs.map((pack) => PackService.getPackInfo(pack)) + ); }; - public static getPackInfo = async(packName: string): Promise => { + public static getPackInfo = async (packName: string): Promise => { + let files: string[] = []; const name = packName; let size = 0; let bpm: string = ""; @@ -31,21 +34,38 @@ export default class PackService { const promises = await Promise.all( packFiles.map(async (file) => { size += await fs.statSync(file.path).size; + + files.push(file.path); + if (file.name === "bpm.txt") { bpm = await fs.readFileSync(file.path).toString(); } }) ); - if (bpm === '') { - console.log('pack without bpm found', name); + if (bpm === "") { + console.log("pack without bpm found", name); } + const currentPack: Pack = { name: name, size: size, bpm: bpm, + files: files, }; - + return currentPack; }; + + public static findByName = (name: string): Pack | undefined => { + for (let i = 0; i < PackService.packList.length; i++) { + let pack = PackService.packList[i]; + + if (pack.name === name) { + return pack; + } + } + + return undefined; + }; }