🔧 Refactoring uncompression methods.
This commit is contained in:
@@ -1,2 +1,2 @@
|
|||||||
export const COMICS_DIRECTORY = "/comics";
|
export const COMICS_DIRECTORY = "./comics";
|
||||||
export const USERDATA_DIRECTORY = "/userdata";
|
export const USERDATA_DIRECTORY = "./userdata";
|
||||||
@@ -56,12 +56,12 @@ export default class ApiService extends Service {
|
|||||||
{
|
{
|
||||||
path: "/userdata",
|
path: "/userdata",
|
||||||
use: [
|
use: [
|
||||||
ApiGateway.serveStatic(path.resolve("/userdata")),
|
ApiGateway.serveStatic(path.resolve("./userdata")),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/comics",
|
path: "/comics",
|
||||||
use: [ApiGateway.serveStatic(path.resolve("/comics"))],
|
use: [ApiGateway.serveStatic(path.resolve("./comics"))],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: "/logs",
|
path: "/logs",
|
||||||
|
|||||||
@@ -95,9 +95,12 @@ export default class ImportService extends Service {
|
|||||||
uncompressFullArchive: {
|
uncompressFullArchive: {
|
||||||
rest: "POST /uncompressFullArchive",
|
rest: "POST /uncompressFullArchive",
|
||||||
params: {},
|
params: {},
|
||||||
handler: async (ctx: Context<{ filePath: string }>) => {
|
handler: async (
|
||||||
|
ctx: Context<{ filePath: string; options: any }>
|
||||||
|
) => {
|
||||||
return await uncompressEntireArchive(
|
return await uncompressEntireArchive(
|
||||||
ctx.params.filePath
|
ctx.params.filePath,
|
||||||
|
ctx.params.options
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -302,13 +305,13 @@ export default class ImportService extends Service {
|
|||||||
async handler(
|
async handler(
|
||||||
ctx: Context<{
|
ctx: Context<{
|
||||||
bundleId: String;
|
bundleId: String;
|
||||||
comicObjectId: String,
|
comicObjectId: String;
|
||||||
name: String;
|
name: String;
|
||||||
size: Number;
|
size: Number;
|
||||||
type: String;
|
type: String;
|
||||||
}>
|
}>
|
||||||
) {
|
) {
|
||||||
console.log(JSON.stringify(ctx.params, null, 2))
|
console.log(JSON.stringify(ctx.params, null, 2));
|
||||||
const comicObjectId = new ObjectId(
|
const comicObjectId = new ObjectId(
|
||||||
ctx.params.comicObjectId
|
ctx.params.comicObjectId
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -63,6 +63,12 @@ interface RarFile {
|
|||||||
|
|
||||||
const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar";
|
const UNRAR_BIN_PATH = process.env.UNRAR_BIN_PATH || "/usr/local/bin/unrar";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
* @param {string} filePath
|
||||||
|
* @returns {any}
|
||||||
|
*/
|
||||||
export const extractComicInfoXMLFromRar = async (
|
export const extractComicInfoXMLFromRar = async (
|
||||||
filePath: string
|
filePath: string
|
||||||
): Promise<any> => {
|
): Promise<any> => {
|
||||||
@@ -77,7 +83,9 @@ export const extractComicInfoXMLFromRar = async (
|
|||||||
};
|
};
|
||||||
const { fileNameWithoutExtension, extension } =
|
const { fileNameWithoutExtension, extension } =
|
||||||
getFileConstituents(filePath);
|
getFileConstituents(filePath);
|
||||||
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${sanitize(fileNameWithoutExtension)}`;
|
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${sanitize(
|
||||||
|
fileNameWithoutExtension
|
||||||
|
)}`;
|
||||||
await createDirectory(directoryOptions, targetDirectory);
|
await createDirectory(directoryOptions, targetDirectory);
|
||||||
|
|
||||||
const archive = new Unrar({
|
const archive = new Unrar({
|
||||||
@@ -207,7 +215,9 @@ export const extractComicInfoXMLFromZip = async (
|
|||||||
};
|
};
|
||||||
const { fileNameWithoutExtension, extension } =
|
const { fileNameWithoutExtension, extension } =
|
||||||
getFileConstituents(filePath);
|
getFileConstituents(filePath);
|
||||||
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${sanitize(fileNameWithoutExtension)}`;
|
const targetDirectory = `${USERDATA_DIRECTORY}/covers/${sanitize(
|
||||||
|
fileNameWithoutExtension
|
||||||
|
)}`;
|
||||||
await createDirectory(directoryOptions, targetDirectory);
|
await createDirectory(directoryOptions, targetDirectory);
|
||||||
|
|
||||||
let filesToWriteToDisk = { coverFile: null, comicInfoXML: null };
|
let filesToWriteToDisk = { coverFile: null, comicInfoXML: null };
|
||||||
@@ -357,33 +367,46 @@ export const extractFromArchive = async (filePath: string) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uncompressEntireArchive = async (filePath: string) => {
|
/**
|
||||||
|
* Proxy method that calls uncompression on a .zip or a .rar archive and optionally resizes the images contained therein
|
||||||
|
* @param {string} filePath
|
||||||
|
* @param {any} options
|
||||||
|
* @returns {Promise} A promise containing the contents of the uncompressed archive.
|
||||||
|
*/
|
||||||
|
export const uncompressEntireArchive = async (
|
||||||
|
filePath: string,
|
||||||
|
options: any
|
||||||
|
) => {
|
||||||
const { extension } = getFileConstituents(filePath);
|
const { extension } = getFileConstituents(filePath);
|
||||||
console.log(extension);
|
|
||||||
switch (extension) {
|
switch (extension) {
|
||||||
case ".cbz":
|
case ".cbz":
|
||||||
case ".cb7":
|
case ".cb7":
|
||||||
return await uncompressZipArchive(filePath);
|
return await uncompressZipArchive(filePath, options);
|
||||||
case ".cbr":
|
case ".cbr":
|
||||||
return await uncompressRarArchive(filePath);
|
return await uncompressRarArchive(filePath, options);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uncompressZipArchive = async (filePath: string) => {
|
/**
|
||||||
|
* Method that uncompresses a .zip file
|
||||||
|
* @param {string} filePath
|
||||||
|
* @param {any} options
|
||||||
|
* @returns {any}
|
||||||
|
*/
|
||||||
|
export const uncompressZipArchive = async (filePath: string, options: any) => {
|
||||||
// Create the target directory
|
// Create the target directory
|
||||||
const directoryOptions = {
|
const directoryOptions = {
|
||||||
mode: 0o2775,
|
mode: 0o2775,
|
||||||
};
|
};
|
||||||
const { fileNameWithoutExtension, extension } =
|
const { fileNameWithoutExtension } = getFileConstituents(filePath);
|
||||||
getFileConstituents(filePath);
|
|
||||||
const targetDirectory = `${USERDATA_DIRECTORY}/expanded/${fileNameWithoutExtension}`;
|
const targetDirectory = `${USERDATA_DIRECTORY}/expanded/${fileNameWithoutExtension}`;
|
||||||
await createDirectory(directoryOptions, targetDirectory);
|
await createDirectory(directoryOptions, targetDirectory);
|
||||||
await p7zip.extract(filePath, targetDirectory, [], "", false);
|
await p7zip.extract(filePath, targetDirectory, [], "", false);
|
||||||
|
|
||||||
return await resizeImageDirectory(targetDirectory);
|
return await resizeImageDirectory(targetDirectory, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const uncompressRarArchive = async (filePath: string) => {
|
export const uncompressRarArchive = async (filePath: string, options: any) => {
|
||||||
// Create the target directory
|
// Create the target directory
|
||||||
const directoryOptions = {
|
const directoryOptions = {
|
||||||
mode: 0o2775,
|
mode: 0o2775,
|
||||||
@@ -421,10 +444,15 @@ export const uncompressRarArchive = async (filePath: string) => {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
return Promise.all(extractionPromises);
|
|
||||||
|
await Promise.all(extractionPromises);
|
||||||
|
return await resizeImageDirectory(targetDirectory, options);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const resizeImageDirectory = async (directoryPath: string) => {
|
export const resizeImageDirectory = async (
|
||||||
|
directoryPath: string,
|
||||||
|
options: any
|
||||||
|
) => {
|
||||||
const files = await walkFolder(directoryPath, [
|
const files = await walkFolder(directoryPath, [
|
||||||
".jpg",
|
".jpg",
|
||||||
".jpeg",
|
".jpeg",
|
||||||
@@ -435,37 +463,36 @@ export const resizeImageDirectory = async (directoryPath: string) => {
|
|||||||
]);
|
]);
|
||||||
const resizePromises = [];
|
const resizePromises = [];
|
||||||
map(files, (file) => {
|
map(files, (file) => {
|
||||||
resizePromises.push(
|
resizePromises.push(resizeImage(directoryPath, file, options));
|
||||||
new Promise((resolve, reject) => {
|
|
||||||
const sharpResizeInstance = sharp().resize(275).toFormat("png");
|
|
||||||
const resizedStream = createReadStream(
|
|
||||||
`${directoryPath}/${file.name}${file.extension}`
|
|
||||||
);
|
|
||||||
if (
|
|
||||||
fse.existsSync(
|
|
||||||
`${directoryPath}/${file.name}${file.extension}`
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
resizedStream
|
|
||||||
.pipe(sharpResizeInstance)
|
|
||||||
.toFile(
|
|
||||||
`${directoryPath}/${file.name}_275px${file.extension}`
|
|
||||||
)
|
|
||||||
.then((data) => {
|
|
||||||
console.log(
|
|
||||||
`Resized image ${JSON.stringify(data, null, 4)}`
|
|
||||||
);
|
|
||||||
fse.unlink(
|
|
||||||
`${directoryPath}/${file.name}${file.extension}`
|
|
||||||
);
|
|
||||||
resolve(
|
|
||||||
`${directoryPath}/${file.name}_275px${file.extension}`
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return await Promise.all(resizePromises);
|
return await Promise.all(resizePromises);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const resizeImage = (directoryPath: string, file: any, options: any) => {
|
||||||
|
const { baseWidth } = options.imageResizeOptions;
|
||||||
|
const sharpResizeInstance = sharp().resize(baseWidth).toFormat("png");
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const resizedStream = createReadStream(
|
||||||
|
`${directoryPath}/${file.name}${file.extension}`
|
||||||
|
);
|
||||||
|
if (fse.existsSync(`${directoryPath}/${file.name}${file.extension}`)) {
|
||||||
|
resizedStream
|
||||||
|
.pipe(sharpResizeInstance)
|
||||||
|
.toFile(
|
||||||
|
`${directoryPath}/${file.name}_${baseWidth}px${file.extension}`
|
||||||
|
)
|
||||||
|
.then((data) => {
|
||||||
|
console.log(
|
||||||
|
`Resized image ${JSON.stringify(data, null, 4)}`
|
||||||
|
);
|
||||||
|
fse.unlink(
|
||||||
|
`${directoryPath}/${file.name}${file.extension}`
|
||||||
|
);
|
||||||
|
resolve(
|
||||||
|
`${directoryPath}/${file.name}_${baseWidth}px${file.extension}`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user