️ Added MIMEtype key to Comic model

This commit is contained in:
2023-03-23 22:55:10 -04:00
parent 7b59f5f94d
commit a19d1c9e9f
3 changed files with 29 additions and 22 deletions

View File

@@ -63,7 +63,8 @@ interface RarFile {
}
const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar";
// errors array
const errors = [];
/**
* Method that extracts comicInfo.xml file from a .rar archive, if one exists.
* Also extracts the first image in the listing, which is assumed to be the cover.
@@ -71,7 +72,8 @@ const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar";
* @returns {any}
*/
export const extractComicInfoXMLFromRar = async (
filePath: string
filePath: string,
mimeType: string,
): Promise<any> => {
try {
// Create the target directory
@@ -185,6 +187,7 @@ export const extractComicInfoXMLFromRar = async (
extension,
containedIn: targetDirectory,
fileSize: fse.statSync(filePath).size,
mimeType,
cover: {
filePath: path.relative(
process.cwd(),
@@ -205,7 +208,8 @@ export const extractComicInfoXMLFromRar = async (
};
export const extractComicInfoXMLFromZip = async (
filePath: string
filePath: string,
mimeType: string,
): Promise<any> => {
try {
// Create the target directory
@@ -250,9 +254,7 @@ export const extractComicInfoXMLFromZip = async (
// Push the first file (cover) to our extraction target
extractionTargets.push(files[0].name);
filesToWriteToDisk.coverFile = path.basename(files[0].name);
console.log(
`sanitized or not, here I am: ${filesToWriteToDisk.coverFile}`
);
if (!isEmpty(comicInfoXMLFileObject)) {
filesToWriteToDisk.comicInfoXML = comicInfoXMLFileObject[0].name;
extractionTargets.push(filesToWriteToDisk.comicInfoXML);
@@ -323,6 +325,7 @@ export const extractComicInfoXMLFromZip = async (
filePath,
name: fileNameWithoutExtension,
extension,
mimeType,
containedIn: targetDirectory,
fileSize: fse.statSync(filePath).size,
cover: {
@@ -347,20 +350,17 @@ export const extractComicInfoXMLFromZip = async (
export const extractFromArchive = async (filePath: string) => {
console.info(`Unrar is located at: ${UNRAR_BIN_PATH}`);
console.info(`p7zip is located at: ${process.env.SEVENZ_BINARY_PATH}`);
const { extension } = getFileConstituents(filePath);
console.log(
`Detected file type is ${extension}, looking for comicinfo.xml...`
);
const mimeType = await getMimeType(filePath);
console.log(`File has the following mime-type: ${mimeType}`);
switch (mimeType) {
case "application/x-7z-compressed; charset=binary":
case "application/zip; charset=binary":
const cbzResult = await extractComicInfoXMLFromZip(filePath);
const cbzResult = await extractComicInfoXMLFromZip(filePath, mimeType);
return Object.assign({}, ...cbzResult);
case "application/x-rar; charset=binary":
const cbrResult = await extractComicInfoXMLFromRar(filePath);
const cbrResult = await extractComicInfoXMLFromRar(filePath, mimeType);
return Object.assign({}, ...cbrResult);
default:
@@ -381,13 +381,20 @@ export const uncompressEntireArchive = async (
filePath: string,
options: any
) => {
const { extension } = getFileConstituents(filePath);
switch (extension) {
case ".cbz":
case ".cb7":
return await uncompressZipArchive(filePath, options);
case ".cbr":
return await uncompressRarArchive(filePath, options);
const mimeType = await getMimeType(filePath);
console.log(`File has the following mime-type: ${mimeType}`);
switch (mimeType) {
case "application/x-7z-compressed; charset=binary":
case "application/zip; charset=binary":
return await uncompressZipArchive(filePath, {
...options,
mimeType,
});
case "application/x-rar; charset=binary":
return await uncompressRarArchive(filePath, {
...options,
mimeType,
});
}
};