💽 mongo import endpoint
This commit is contained in:
@@ -2,10 +2,9 @@ const mongoose = require("mongoose");
|
||||
const paginate = require("mongoose-paginate");
|
||||
|
||||
const ComicSchema = mongoose.Schema({
|
||||
name: String,
|
||||
type: String,
|
||||
import: {
|
||||
importStatus: {
|
||||
isImported: Boolean,
|
||||
tagged: Boolean,
|
||||
matchedResult: {
|
||||
score: String,
|
||||
},
|
||||
@@ -14,28 +13,21 @@ const ComicSchema = mongoose.Schema({
|
||||
tags: [],
|
||||
},
|
||||
|
||||
comicStructure: {
|
||||
cover: {
|
||||
thumb: String,
|
||||
medium: String,
|
||||
large: String,
|
||||
},
|
||||
collection: {
|
||||
publishDate: String,
|
||||
type: String, // issue, series, trade paperback
|
||||
metadata: {
|
||||
publisher: String,
|
||||
issueNumber: String,
|
||||
description: String,
|
||||
synopsis: String,
|
||||
team: {
|
||||
writer: String,
|
||||
inker: String,
|
||||
penciler: String,
|
||||
colorist: String,
|
||||
},
|
||||
},
|
||||
},
|
||||
comicInfo: {
|
||||
blackAndWhite: String,
|
||||
characters: [String],
|
||||
count: String,
|
||||
genre: String,
|
||||
manga: String,
|
||||
month: String,
|
||||
number: String,
|
||||
pageCount: String,
|
||||
pages: [],
|
||||
publisher: String,
|
||||
summary: String,
|
||||
title: String,
|
||||
writer: String,
|
||||
year: String,
|
||||
},
|
||||
sourcedMetadata: {
|
||||
comicvine: {},
|
||||
@@ -43,13 +35,14 @@ const ComicSchema = mongoose.Schema({
|
||||
gcd: {},
|
||||
},
|
||||
rawFileDetails: {
|
||||
fileName: String,
|
||||
name: String,
|
||||
path: String,
|
||||
extension: String,
|
||||
fileSize: Number,
|
||||
},
|
||||
acquisition: {
|
||||
wanted: Boolean,
|
||||
release: {},
|
||||
directconnect: {},
|
||||
torrent: {
|
||||
sourceApplication: String,
|
||||
magnet: String,
|
||||
|
||||
19
package-lock.json
generated
19
package-lock.json
generated
@@ -7778,6 +7778,11 @@
|
||||
"sparse-bitfield": "^3.0.3"
|
||||
}
|
||||
},
|
||||
"sax": {
|
||||
"version": "1.2.4",
|
||||
"resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz",
|
||||
"integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
|
||||
},
|
||||
"saxes": {
|
||||
"version": "3.1.11",
|
||||
"resolved": "https://registry.npmjs.org/saxes/-/saxes-3.1.11.tgz",
|
||||
@@ -9440,6 +9445,20 @@
|
||||
"integrity": "sha512-A5CUptxDsvxKJEU3yO6DuWBSJz/qizqzJKOMIfUJHETbBw/sFaDxgd6fxm1ewUaM0jZ444Fc5vC5ROYurg/4Pw==",
|
||||
"dev": true
|
||||
},
|
||||
"xml2js": {
|
||||
"version": "0.4.23",
|
||||
"resolved": "https://registry.npmjs.org/xml2js/-/xml2js-0.4.23.tgz",
|
||||
"integrity": "sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==",
|
||||
"requires": {
|
||||
"sax": ">=0.6.0",
|
||||
"xmlbuilder": "~11.0.0"
|
||||
}
|
||||
},
|
||||
"xmlbuilder": {
|
||||
"version": "11.0.1",
|
||||
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz",
|
||||
"integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA=="
|
||||
},
|
||||
"xmlchars": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz",
|
||||
|
||||
@@ -54,7 +54,8 @@
|
||||
"socket.io": "^4.1.1",
|
||||
"socket.io-stream": "^0.9.1",
|
||||
"typescript": "^3.8.3",
|
||||
"unzipper": "^0.10.11"
|
||||
"unzipper": "^0.10.11",
|
||||
"xml2js": "^0.4.23"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 10.x.x"
|
||||
|
||||
@@ -22,6 +22,20 @@ export default class ApiService extends Service {
|
||||
// Access to any actions in all services under "/api" URL
|
||||
"**",
|
||||
],
|
||||
cors: {
|
||||
origin: "*",
|
||||
methods: [
|
||||
"GET",
|
||||
"OPTIONS",
|
||||
"POST",
|
||||
"PUT",
|
||||
"DELETE",
|
||||
],
|
||||
allowedHeaders: ["*"],
|
||||
exposedHeaders: [],
|
||||
credentials: false,
|
||||
maxAge: 3600,
|
||||
},
|
||||
use: [],
|
||||
mergeParams: true,
|
||||
autoAliases: true,
|
||||
@@ -59,7 +73,6 @@ export default class ApiService extends Service {
|
||||
logRequestParams: null,
|
||||
// Logging the response data. Set to any log level to enable it. E.g. "info"
|
||||
logResponseData: null,
|
||||
// Serve assets from "public" folder
|
||||
assets: {
|
||||
folder: "public",
|
||||
// Options to `server-static` module
|
||||
@@ -105,10 +118,10 @@ export default class ApiService extends Service {
|
||||
folder
|
||||
);
|
||||
|
||||
client.emit("comicBookCoverMetadata", {
|
||||
data: comicBookCoverMetadata,
|
||||
status: "Done!",
|
||||
});
|
||||
client.emit(
|
||||
"comicBookCoverMetadata",
|
||||
comicBookCoverMetadata
|
||||
);
|
||||
});
|
||||
|
||||
case "single":
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Context, Service, ServiceBroker, ServiceSchema } from "moleculer";
|
||||
import { DbMixin } from "../mixins/db.mixin";
|
||||
import Comic from "../models/comic.model";
|
||||
import { walkFolder } from "../utils/uncompression.utils";
|
||||
import { convertXMLToJSON } from "../utils/xml.utils";
|
||||
|
||||
export default class ProductsService extends Service {
|
||||
// @ts-ignore
|
||||
@@ -41,6 +42,31 @@ export default class ProductsService extends Service {
|
||||
);
|
||||
},
|
||||
},
|
||||
convertXMLToJSON: {
|
||||
rest: "POST /convertXmlToJson",
|
||||
params: {},
|
||||
async handler(ctx: Context<{}>) {
|
||||
return convertXMLToJSON("lagos");
|
||||
},
|
||||
},
|
||||
rawImportToDB: {
|
||||
rest: "POST /rawImportToDB",
|
||||
params: { payload: "object" },
|
||||
async handler(ctx: Context<{ payload: object }>) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Comic.create(
|
||||
ctx.params.payload,
|
||||
(error, data) => {
|
||||
if (data) {
|
||||
resolve(data);
|
||||
} else if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: {},
|
||||
},
|
||||
|
||||
24
utils/comicinfo.xml
Normal file
24
utils/comicinfo.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0"?>
|
||||
<ComicInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||
<Title>Title of the Book</Title>
|
||||
<Summary>A description of the book</Summary>
|
||||
<Number>1</Number>
|
||||
<Count>3</Count>
|
||||
<Year>2010</Year>
|
||||
<Month>4</Month>
|
||||
<Writer>Author name</Writer>
|
||||
<Publisher>self</Publisher>
|
||||
<Genre>educational</Genre>
|
||||
<BlackAndWhite>No</BlackAndWhite>
|
||||
<Manga>No</Manga>
|
||||
<Characters>Superman</Characters>
|
||||
<PageCount>5</PageCount>
|
||||
<Pages>
|
||||
<Page Image="0" Type="FrontCover" ImageSize="139382" ImageWidth="774" ImageHeight="1024" />
|
||||
<Page Image="2" ImageSize="125736" ImageWidth="797" ImageHeight="1024" />
|
||||
<Page Image="1" ImageSize="127937" ImageWidth="797" ImageHeight="1024" />
|
||||
<Page Image="4" ImageSize="160902" ImageWidth="804" ImageHeight="1024" />
|
||||
<Page Image="3" ImageSize="211181" ImageWidth="804" ImageHeight="1024" />
|
||||
</Pages>
|
||||
</ComicInfo>
|
||||
@@ -205,12 +205,15 @@ export const unzip = async (
|
||||
}
|
||||
if (fileName !== "" && entry.type !== "Directory") {
|
||||
logger.info(`Attempting to write ${fileName}`);
|
||||
entry.pipe(createWriteStream(paths.targetPath + "/" + fileName));
|
||||
extractedFiles.push({
|
||||
name: fileName,
|
||||
fileSize: size,
|
||||
path: paths.targetPath,
|
||||
});
|
||||
entry
|
||||
.pipe(createWriteStream(paths.targetPath + "/" + fileName))
|
||||
.on("finish", () => {
|
||||
extractedFiles.push({
|
||||
name: fileName,
|
||||
fileSize: size,
|
||||
path: paths.targetPath,
|
||||
});
|
||||
});
|
||||
}
|
||||
entry.autodrain();
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import {
|
||||
IFolderData,
|
||||
} from "../interfaces/folder.interface";
|
||||
const Validator = require("fastest-validator");
|
||||
import { logger } from "./logger.utils";
|
||||
|
||||
export const validateComicBookMetadata = (
|
||||
comicBookMetadataObject: IExtractedComicBookCoverFile
|
||||
): boolean => {
|
||||
console.log(comicBookMetadataObject);
|
||||
const validator = new Validator();
|
||||
const sch = {
|
||||
name: { type: "string" },
|
||||
@@ -18,5 +18,13 @@ export const validateComicBookMetadata = (
|
||||
path: { type: "string" },
|
||||
};
|
||||
const check = validator.compile(sch);
|
||||
if (check(comicBookMetadataObject)) {
|
||||
logger.info(`Valid comic book metadata: ${comicBookMetadataObject}`);
|
||||
} else {
|
||||
logger.error(
|
||||
`Comic book metadata was invalid:
|
||||
${comicBookMetadataObject}`
|
||||
);
|
||||
}
|
||||
return check(comicBookMetadataObject);
|
||||
};
|
||||
|
||||
48
utils/xml.utils.ts
Normal file
48
utils/xml.utils.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2021 Rishi Ghan
|
||||
*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2021 Rishi Ghan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
import xml2js from "xml2js";
|
||||
import fs from "fs";
|
||||
import { logger } from "../utils/logger.utils";
|
||||
|
||||
export const convertXMLToJSON = (xmlPayload) => {
|
||||
const parser = new xml2js.Parser({
|
||||
mergeAttrs: true,
|
||||
trim: true,
|
||||
normalizeTags: true,
|
||||
});
|
||||
const xml = fs.readFileSync(__dirname + "/comicinfo.xml", "utf8");
|
||||
return parser
|
||||
.parseStringPromise(xml)
|
||||
.then((result) => {
|
||||
return result;
|
||||
})
|
||||
.catch((error) => {
|
||||
logger.error(error);
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user