diff --git a/src/handlers/index.ts b/src/handlers/index.ts index 9bde6011..5104b0ea 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -72,6 +72,8 @@ import xcursorHandler from "./xcursor.ts"; import shToElfHandler from "./shToElf.ts"; import cssHandler from "./css.ts"; import TypstHandler from "./typst.ts"; +import kraHandler from "./kra.ts"; +import krzHandler from "./krz.ts"; import BRARCHIVEHandler from "./brarchive.ts"; import wasiRunnerHandler from "./wasiRunner.ts"; import clangWasiHandler from "./clang-wasi.ts"; @@ -157,6 +159,8 @@ try { handlers.push(new xcursorHandler()) } catch (_) { }; try { handlers.push(new shToElfHandler()) } catch (_) { }; try { handlers.push(new cssHandler()) } catch (_) { }; try { handlers.push(new TypstHandler()) } catch (_) { }; +try { handlers.push(new kraHandler()) } catch (_) { }; +try { handlers.push(new krzHandler()) } catch (_) { }; try { handlers.push(new BRARCHIVEHandler()) } catch (_) { }; try { handlers.push(new wasiRunnerHandler()) } catch (_) { }; try { handlers.push(new clangWasiHandler()) } catch (_) { }; diff --git a/src/handlers/kra.ts b/src/handlers/kra.ts new file mode 100644 index 00000000..36f6e117 --- /dev/null +++ b/src/handlers/kra.ts @@ -0,0 +1,66 @@ +import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; +import CommonFormats from "src/CommonFormats.ts"; +import JSZip from "jszip"; + +class kraHandler implements FormatHandler { + public name: string = "kra"; + public supportedFormats?: FileFormat[]; + public ready: boolean = false; + + async init() { + this.supportedFormats = [ + CommonFormats.PNG.builder("png").allowFrom(false).allowTo(true), + + { + name: "Krita Raster Archive (KRA)", + format: "kra", + extension: "kra", + mime: "application/x-krita", + from: true, + to: false, + internal: "kra", + category: ["archive"], + lossless: false, + }, + ]; + this.ready = true; + } + + async doConvert( + inputFiles: FileData[], + inputFormat: FileFormat, + outputFormat: FileFormat, + ): Promise { + const outputFiles: FileData[] = []; + if (inputFormat.format == "kra" && outputFormat.format == "png") { + for (const inputFile of inputFiles) { + const zip = new JSZip(); + const zipContent = await zip.loadAsync(inputFile.bytes); + let imageFile; + + try { + imageFile = zipContent.file("mergedimage.png"); + + if (!imageFile) { + throw new Error(); + } + } catch { + imageFile = zipContent.file("preview.png"); + + if (!imageFile) { + throw new Error("Could not find image in KRA file"); + } + } + const imageData = await imageFile.async("uint8array"); + outputFiles.push({ + name: inputFile.name.replace(/\.kra$/, ".png"), + bytes: imageData, + }); + } + } + + return outputFiles; + } +} + +export default kraHandler; diff --git a/src/handlers/krz.ts b/src/handlers/krz.ts new file mode 100644 index 00000000..adb0fb8c --- /dev/null +++ b/src/handlers/krz.ts @@ -0,0 +1,57 @@ +import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; +import CommonFormats from "src/CommonFormats.ts"; +import JSZip from "jszip"; + +class krzHandler implements FormatHandler { + public name: string = "krz"; + public supportedFormats?: FileFormat[]; + public ready: boolean = false; + + async init() { + this.supportedFormats = [ + CommonFormats.PNG.builder("png").allowFrom(false).allowTo(true), + + { + name: "Krita Raster Archive (krz)", + format: "krz", + extension: "krz", + mime: "application/x-krita", + from: true, + to: false, + internal: "krz", + category: ["archive"], + lossless: false, + }, + ]; + this.ready = true; + } + + async doConvert( + inputFiles: FileData[], + inputFormat: FileFormat, + outputFormat: FileFormat, + ): Promise { + const outputFiles: FileData[] = []; + if (inputFormat.format == "krz" && outputFormat.format == "png") { + for (const inputFile of inputFiles) { + const zip = new JSZip(); + const zipContent = await zip.loadAsync(inputFile.bytes); + const imageFile = zipContent.file("preview.png"); + + if (!imageFile) { + throw new Error("Could not find image in krz file"); + } + + const imageData = await imageFile.async("uint8array"); + outputFiles.push({ + name: inputFile.name.replace(/\.krz$/, ".png"), + bytes: imageData, + }); + } + } + + return outputFiles; + } +} + +export default krzHandler;