🔧 Some refactoring of import endpoints in facade

This commit is contained in:
2021-05-09 23:40:19 -07:00
parent d35f4b50b4
commit cd3d4e272d
5 changed files with 67 additions and 54 deletions

View File

@@ -28,16 +28,5 @@ export const greet = async (
},
fileObjects,
);
const reader = await ndjsonStream(fo).getReader();
reader.read().then(function process({ done, value }) {
if (done) {
console.log("done");
return;
}
console.log(value);
return reader.read().then(process);
});
// return JSON.stringify(fo);
};

View File

@@ -1,52 +1,42 @@
import router from "../router";
import { default as paginate } from "express-paginate";
import { walkFolder, extractArchive, getCovers } from "../../utils/fs.utils";
import { IExtractionOptions } from "../../interfaces/folder.interface";
import { Request, Response } from "express";
import _ from "lodash";
import through2 from "through2";
import { Readable } from "stream";
import H from "highland";
import intoStream from "into-stream";
const ndjson = require("ndjson");
import axios from "axios";
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
typeof req.body.extractionOptions === "object"
? req.body.extractionOptions
: {};
const foo = await getCovers(
req.body.extractionOptions,
req.body.walkedFolders,
);
const stream = new Readable({
objectMode: true,
highWaterMark: 1,
read() {},
});
const ndjsonStream = through2(
{ objectMode: true, highWaterMark: 1 },
(data, enc, cb) => {
cb(null, JSON.stringify(data) + "\n");
},
);
// Through pipe we do a double addressing, our reading stream goes through the transformation
// to finally go through the stream response..
stream.pipe(ndjsonStream).pipe(res);
stream.push(foo);
stream.push(null);
// return res.json({
// foo,
// });
axios
.request({
url: "http://localhost:3853/api/import/getComicCovers",
method: "POST",
data: {
extractionOptions: req.body.extractionOptions,
walkedFolders: req.body.walkedFolders,
},
})
.then((data) => data)
.catch((error) => error);
});
router.route("/walkFolder").post(async (req: Request, res: Response) => {
const basePathToWalk =
typeof req.query.basePathToWalk === "string"
? req.query.basePathToWalk
: "";
const results = await walkFolder(basePathToWalk);
res.json(results);
typeof req.body.basePathToWalk === "string" ? req.body.basePathToWalk : "";
axios
.request({
method: "POST",
data: {
basePathToWalk,
},
})
.then((data) => data)
.catch((error) => error);
});
export default router;

View File

@@ -37,6 +37,7 @@ const Walk = require("@root/walk");
const fse = require("fs-extra");
import { default as unzipper } from "unzipper";
import _ from "lodash";
import { createReadStream, createWriteStream } from "fs";
const { writeFile, readFile } = require("fs").promises;
import path from "path";
@@ -130,7 +131,7 @@ export const unrar = async (
fileSize: file.fileHeader.packSize,
});
}
resolve(comicBookCoverFiles);
resolve(_.flatten(comicBookCoverFiles));
} catch (error) {
resolve({
message: `${error}`,
@@ -196,7 +197,7 @@ export const unzip = async (
return new Promise(async (resolve, reject) => {
logger.info("");
resolve(extractedFiles);
resolve(_.flatten(extractedFiles));
});
};
@@ -241,7 +242,7 @@ export const getCovers = async (
const extractedDataPromises = map(walkedFolders, async (folder) => {
return await extractArchive(options, folder);
});
return Promise.all(extractedDataPromises).then((data) => data);
return Promise.all(extractedDataPromises).then((data) => _.flatten(data));
case "single":
return await extractArchive(options, walkedFolders[0]);
default: