Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 93 additions & 17 deletions src/handlers/comics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats from "src/CommonFormats.ts";

import {
createTar,
createTarGzip,
createTarGzipStream,
parseTar,
parseTarGzip,
type TarFileItem,
} from "nanotar";
import JSZip from "jszip";

const image_list = ["png","jpg","webp","bmp","tiff","gif"];
const archives_list = ["zip","cbz","tar","cbt"];

class comicsHandler implements FormatHandler {

Expand All @@ -22,6 +32,17 @@ class comicsHandler implements FormatHandler {
CommonFormats.GIF.supported("gif", true, true),

CommonFormats.ZIP.supported("zip", true, true),
{
name: "Tape Archive",
format: "tar",
extension: "tar",
mime: "application/x-tar",
from: true,
to: true,
internal: "tar",
category: ["archive"],
},

{
name: "Comic Book Archive (ZIP)",
format: "cbz",
Expand All @@ -30,6 +51,17 @@ class comicsHandler implements FormatHandler {
from: true,
to: true,
internal: "cbz",
category: ["archive"],
},
{
name: "Comic Book Archive (TAR)",
format: "cbt",
extension: "cbt",
mime: "application/vnd.comicbook+tar",
from: true,
to: true,
internal: "cbt",
category: ["archive"],
},
];

Expand All @@ -43,16 +75,17 @@ class comicsHandler implements FormatHandler {
): Promise<FileData[]> {
const outputFiles: FileData[] = [];

// Some code copied from wad.ts
// Base name for imgs -> archive
const baseName = inputFiles[0].name.replace("_0."+inputFormat.extension,"."+inputFormat.extension).split(".").slice(0, -1).join(".");

// Single-gif catching
if (inputFormat.internal === "gif" && (archives_list.includes(outputFormat.internal)) && inputFiles.length === 1) {
throw new Error("User probably intends for an archive of video/gif frames; abort.");
}

// Pack a zip/cbz with code copied from wad.ts
if ((image_list.includes(inputFormat.internal)) && (outputFormat.internal === "cbz" || outputFormat.internal === "zip")) {
if (inputFormat.internal === "gif" && outputFormat.internal === "cbz" && inputFiles.length === 1) {
throw new Error("User probably intends for a zip of video/gif frames; abort.");
}

const zip = new JSZip();

// Determine the archive name
const baseName = inputFiles[0].name.replace("_0."+inputFormat.extension,"."+inputFormat.extension).split(".").slice(0, -1).join(".");

// Add files to archive
let iterations = 0;
Expand All @@ -69,7 +102,7 @@ class comicsHandler implements FormatHandler {
const output = await zip.generateAsync({ type: "uint8array" });
outputFiles.push({ bytes: output, name: baseName + "." + outputFormat.extension });
}
// Some code copied from lzh.ts
// Unpack a zip/cbz with code copied from lzh.ts
else if ((inputFormat.internal === "cbz" || inputFormat.internal === "zip") && (image_list.includes(outputFormat.internal))) {
for (const file of inputFiles) {
const zip = new JSZip();
Expand All @@ -78,19 +111,19 @@ class comicsHandler implements FormatHandler {
// Extract all files from ZIP
for (const [filename, zipEntry] of Object.entries(zip.files)) {
if (!zipEntry.dir) {
if (filename.endsWith(outputFormat.extension)) {
if (inputFormat.internal === "cbz" && filename.endsWith(".xml")) {
// Ignore .xml files in comic book archives.
}
else if (filename.endsWith("."+outputFormat.extension) === false) {
throw new Error("Archive contains multiple file types; abort.");
}
else {
const data = await zipEntry.async("uint8array");
outputFiles.push({
name: filename,
bytes: data,
bytes: data
});
}
else if (inputFormat.internal === "cbz" && filename.endsWith(".xml")) {
// Do nothing. This is an exception to the rule.
}
else {
throw new Error("Archive contains multiple file types; abort.");
}
}
}
}
Expand All @@ -100,6 +133,49 @@ class comicsHandler implements FormatHandler {
throw new Error("No applicable files to unzip found.");
}
}
// Pack a cbt with code from tar.ts
else if (image_list.includes(inputFormat.internal) && outputFormat.internal === "cbt") {
const bytes = createTar(
inputFiles.map(file => ({ name: "Page "+inputFiles.indexOf(file)+"."+inputFormat.extension, data: file.bytes })),
{},
);
outputFiles.push({ bytes: bytes, name: baseName + "." + outputFormat.extension });
}
// Unpack a tar/cbt with code from tar.ts
else if ((inputFormat.internal === "cbt" || inputFormat.internal === "tar") && image_list.includes(outputFormat.internal)) {
for (const inputFile of inputFiles) {
const files = parseTar(inputFile.bytes);

for (const file of files) {
if (inputFormat.internal === "cbt" && file.name.endsWith(".xml")) {
// Ignore .xml files in comic book archives.
}
else if (file.name.endsWith("."+outputFormat.extension) === false) {
throw new Error("Archive contains multiple file types; abort.");
}
else if (!file.data) {
throw new Error("Undefined data type; abort.");
}
else {
outputFiles.push({
name: file.name,
bytes: file.data
});
}
}
}

// Throw error if empty
if (outputFiles.length === 0) {
throw new Error("No applicable files to unpack found.");
}
}
// Renaming interchangeable formats. Note that any valid "comic book" archive can be guaranteed as a valid standard archive, but not every valid archive can be a valid comic book archive. Thus, we only allow renaming from comic book to non-comic book formats.
else if ((inputFormat.internal === "cbz" && outputFormat.internal === "zip") || (inputFormat.internal === "cbt" && outputFormat.internal === "tar")) {
for (const file of inputFiles) {
outputFiles.push({ bytes: file.bytes, name: file.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension });
}
}
else {
throw new Error("Invalid input-output.");
}
Expand Down
33 changes: 27 additions & 6 deletions src/handlers/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,28 @@ class tarHandler implements FormatHandler {
category: ["archive"],
lossless: true
},
CommonFormats.ZIP.builder("zip").allowFrom().allowTo().markLossless()
CommonFormats.ZIP.builder("zip").allowFrom().allowTo().markLossless(),

{
name: "Comic Book Archive (ZIP)",
format: "cbz",
extension: "cbz",
mime: "application/vnd.comicbook+zip",
from: true,
to: true,
internal: "cbz",
category: ["archive"],
},
{
name: "Comic Book Archive (TAR)",
format: "cbt",
extension: "cbt",
mime: "application/vnd.comicbook+tar",
from: true,
to: true,
internal: "cbt",
category: ["archive"],
},
];

public supportAnyInput: boolean = true;
Expand All @@ -46,7 +67,7 @@ class tarHandler implements FormatHandler {
): Promise<FileData[]> {
const outputFiles: FileData[] = [];

if (inputFormat.internal == "zip" && outputFormat.internal == "tar") {
if ((inputFormat.internal === "zip" && outputFormat.internal === "tar") || (inputFormat.internal === "cbz" && outputFormat.internal === "cbt")) {
for (const inputFile of inputFiles) {
const zip = new JSZip();
await zip.loadAsync(inputFile.bytes);
Expand All @@ -70,15 +91,15 @@ class tarHandler implements FormatHandler {
});
}

const name = inputFile.name.replace(/\.zip$/i, ".tar");
const name = inputFile.name.replace(/\.zip$/i, ".tar").replace(/\.cbz$/i, ".cbt");
const bytes = createTar(
archiveFiles,
{},
);

outputFiles.push({ bytes, name });
}
} else if (inputFormat.internal == "tar" && outputFormat.internal == "zip") {
} else if ((inputFormat.internal === "tar" && outputFormat.internal === "zip") || (inputFormat.internal === "cbt" && outputFormat.internal === "cbz")) {
for (const inputFile of inputFiles) {
const files = parseTar(inputFile.bytes);

Expand All @@ -97,10 +118,10 @@ class tarHandler implements FormatHandler {

const bytes = await zip.generateAsync({ type: "uint8array" });

const name = inputFile.name.replace(/\.tar$/i, ".zip");
const name = inputFile.name.replace(/\.tar$/i, ".zip").replace(/\.cbt$/i, ".cbz");
outputFiles.push({ bytes, name });
}
} else if (outputFormat.internal == "tar") {
} else if (outputFormat.internal === "tar") {
const bytes = createTar(
inputFiles.map(file => ({ name: file.name, data: file.bytes })),
{},
Expand Down
Loading