🔩 Refactoring unzip yet again
This commit is contained in:
@@ -9,9 +9,9 @@ import {
|
||||
import { COMICBOOKINFO_SERVICE_URI } from "../constants/endpoints";
|
||||
|
||||
const http = rateLimiter(axios.create(), {
|
||||
maxRequests: 1,
|
||||
perMilliseconds: 1000,
|
||||
maxRPS: 1,
|
||||
maxRequests: 1,
|
||||
perMilliseconds: 1000,
|
||||
maxRPS: 1,
|
||||
});
|
||||
|
||||
export const comicinfoAPICall = (options) => async (dispatch) => {
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
export const COMICBOOKINFO_SERVICE_URI =
|
||||
"http://localhost:6050/api/comicbookinfo/";
|
||||
export const FOLDERUTIL_URI = "http://localhost:3000/walkfolder";
|
||||
export const FOLDERUTIL_URI = "http://localhost:8050/api/";
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import axios from "axios";
|
||||
import { IFolderData } from "../interfaces/comicinfo.interfaces";
|
||||
import { FOLDERUTIL_URI } from "../../constants/endpoints";
|
||||
import {
|
||||
IExtractComicBookCoverErrorResponse,
|
||||
IExtractedComicBookCoverFile,
|
||||
IExtractionOptions,
|
||||
} from "../../../server/interfaces/folder.interface";
|
||||
import { FS_API_BASE_URI } from "../../constants/endpoints";
|
||||
|
||||
export async function folderWalk(): Promise<Array<IFolderData>> {
|
||||
return axios
|
||||
.request<Array<IFolderData>>({
|
||||
url: FOLDERUTIL_URI,
|
||||
url: FS_API_BASE_URI + "/walkFolder",
|
||||
transformResponse: (r: string) => JSON.parse(r),
|
||||
})
|
||||
.then((response) => {
|
||||
@@ -14,6 +19,18 @@ export async function folderWalk(): Promise<Array<IFolderData>> {
|
||||
});
|
||||
}
|
||||
|
||||
export async function extractCoverFromComicBookArchive(
|
||||
options: IExtractionOptions,
|
||||
): Promise<
|
||||
| IExtractedComicBookCoverFile
|
||||
| IExtractedComicBookCoverFile[]
|
||||
| IExtractComicBookCoverErrorResponse
|
||||
> {
|
||||
return await axios.request({
|
||||
url: FS_API_BASE_URI + "/getComicCovers",
|
||||
});
|
||||
}
|
||||
|
||||
export async function foo() {
|
||||
return { as: "af" };
|
||||
}
|
||||
|
||||
@@ -9,11 +9,11 @@ import {
|
||||
} from "../../utils/fs.utils";
|
||||
import { Request, Response } from "express";
|
||||
|
||||
router.route("/getComicCovers").get(async (req: Request, res: Response) => {
|
||||
router.route("/getComicCovers").post(async (req: Request, res: Response) => {
|
||||
const options = {
|
||||
name: "Gyo v02 (2004) (Digital) (LostNerevarine-Empire)",
|
||||
name: "Beautiful Darkness (2014) (scan) (Anonymous) (resized)",
|
||||
extension: ".cbz",
|
||||
containedIn: "comics/ITOU Junji - Gyo",
|
||||
containedIn: "comics",
|
||||
isFile: true,
|
||||
isLink: false,
|
||||
};
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
IExtractionOptions,
|
||||
IFolderData,
|
||||
} from "../interfaces/folder.interface";
|
||||
import { WriteStream } from "node:fs";
|
||||
|
||||
export const unrar = async (
|
||||
extractionOptions: IExtractionOptions,
|
||||
@@ -123,7 +124,9 @@ export const extractMetadataFromImage = async (
|
||||
export const unzip = async (
|
||||
extractionOptions: IExtractionOptions,
|
||||
): Promise<
|
||||
IExtractedComicBookCoverFile[] | IExtractComicBookCoverErrorResponse
|
||||
| IExtractedComicBookCoverFile[]
|
||||
| IExtractedComicBookCoverFile
|
||||
| IExtractComicBookCoverErrorResponse
|
||||
> => {
|
||||
const extractedFiles: IExtractedComicBookCoverFile[] = [];
|
||||
const directoryOptions = {
|
||||
@@ -136,12 +139,15 @@ export const unzip = async (
|
||||
"/" +
|
||||
extractionOptions.folderDetails.name;
|
||||
|
||||
const zip = createReadStream(
|
||||
const inputFilePath =
|
||||
extractionOptions.folderDetails.containedIn +
|
||||
"/" +
|
||||
extractionOptions.folderDetails.name +
|
||||
extractionOptions.folderDetails.extension,
|
||||
).pipe(unzipper.Parse({ forceStream: true }));
|
||||
"/" +
|
||||
extractionOptions.folderDetails.name +
|
||||
extractionOptions.folderDetails.extension;
|
||||
|
||||
const zip = createReadStream(inputFilePath).pipe(
|
||||
unzipper.Parse({ forceStream: true }),
|
||||
);
|
||||
|
||||
try {
|
||||
await fse.ensureDir(targetPath, directoryOptions);
|
||||
@@ -150,48 +156,36 @@ export const unzip = async (
|
||||
logger.error(`${error} Couldn't create directory.`);
|
||||
}
|
||||
|
||||
for await (const entry of zip) {
|
||||
const fileName = explodePath(entry.path).fileName;
|
||||
const size = entry.vars.uncompressedSize;
|
||||
if (fileName !== "" && entry.type !== "Directory") {
|
||||
extractedFiles.push({
|
||||
name: fileName,
|
||||
fileSize: size,
|
||||
path: targetPath,
|
||||
});
|
||||
}
|
||||
entry.autodrain();
|
||||
}
|
||||
|
||||
switch (extractionOptions.extractTarget) {
|
||||
case "all":
|
||||
for await (const entry of zip) {
|
||||
const fileName = explodePath(entry.path).fileName;
|
||||
const size = entry.vars.uncompressedSize;
|
||||
|
||||
if (fileName !== "") {
|
||||
entry.pipe(createWriteStream(targetPath + "/" + fileName));
|
||||
}
|
||||
extractedFiles.push({
|
||||
name: fileName,
|
||||
fileSize: size,
|
||||
path: targetPath,
|
||||
});
|
||||
const writeableFileName = explodePath(entry.path).fileName;
|
||||
entry.pipe(createWriteStream(targetPath + "/" + writeableFileName));
|
||||
entry.autodrain();
|
||||
}
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
logger.info("");
|
||||
resolve(extractedFiles);
|
||||
});
|
||||
|
||||
case "cover":
|
||||
for await (const entry of zip) {
|
||||
const fileName = explodePath(entry.path).fileName;
|
||||
|
||||
if (fileName !== "" && entry.type !== "Directory") {
|
||||
const size = entry.vars.uncompressedSize;
|
||||
entry.pipe(createWriteStream(targetPath + "/" + fileName));
|
||||
extractedFiles.push({
|
||||
name: fileName,
|
||||
fileSize: size,
|
||||
path: targetPath,
|
||||
});
|
||||
entry.autodrain();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new Promise(async (resolve, reject) => {
|
||||
logger.info("");
|
||||
resolve(extractedFiles);
|
||||
console.log(zip);
|
||||
createWriteStream(targetPath + "/" + extractedFiles[0].name);
|
||||
return new Promise((resolve, reject) => {
|
||||
resolve(extractedFiles[0]);
|
||||
});
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user