feat: Send pack files over multistream

master
ogomez-at-wiris 2022-02-24 07:39:07 +01:00
parent 65203fbabf
commit e7f855ca51
4 changed files with 46 additions and 7 deletions

View File

@ -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

View File

@ -2,5 +2,6 @@ interface Pack {
name: string;
size: number;
bpm: string;
files: string[];
}
export default Pack;

View File

@ -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",
});
};
}

View File

@ -12,10 +12,13 @@ export default class PackService {
public static getInitialList = async (): Promise<void> => {
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<Pack> => {
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;
};
}