2022-08-22 13:25:59 -07:00
|
|
|
import { Stream } from "stream";
|
2022-09-15 20:00:50 +01:00
|
|
|
import { File } from "@file/file";
|
2022-06-26 23:26:42 +01:00
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
import { Compiler } from "./next-compiler";
|
|
|
|
|
2022-03-07 20:02:14 +08:00
|
|
|
/**
|
|
|
|
* Use blanks to prettify
|
|
|
|
*/
|
2023-12-22 10:25:00 +09:00
|
|
|
export const PrettifyType = {
|
|
|
|
NONE: "",
|
|
|
|
WITH_2_BLANKS: " ",
|
|
|
|
WITH_4_BLANKS: " ",
|
|
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
|
|
WITH_TAB: "\t",
|
|
|
|
} as const;
|
2022-03-07 20:02:14 +08:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
const convertPrettifyType = (
|
|
|
|
prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType],
|
|
|
|
): (typeof PrettifyType)[keyof typeof PrettifyType] | undefined =>
|
2023-06-12 18:43:29 +01:00
|
|
|
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify;
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
export class Packer {
|
2023-12-22 10:25:00 +09:00
|
|
|
public static async toString(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> {
|
2023-06-12 18:43:29 +01:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "string",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
|
|
|
|
return zipData;
|
2022-08-17 18:52:19 +02:00
|
|
|
}
|
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
public static async toBuffer(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Buffer> {
|
2023-06-12 18:43:29 +01:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "nodebuffer",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
|
|
|
|
return zipData;
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
2018-01-31 20:08:36 +00:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
public static async toBase64String(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<string> {
|
2023-06-12 18:43:29 +01:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "base64",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2023-06-12 18:43:29 +01:00
|
|
|
return zipData;
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
public static async toBlob(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Promise<Blob> {
|
2023-06-12 18:43:29 +01:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "blob",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
2023-06-12 18:43:29 +01:00
|
|
|
return zipData;
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
2023-12-22 10:25:00 +09:00
|
|
|
public static toStream(file: File, prettify?: boolean | (typeof PrettifyType)[keyof typeof PrettifyType]): Stream {
|
2023-06-01 02:05:35 +01:00
|
|
|
const stream = new Stream();
|
2023-06-12 18:43:29 +01:00
|
|
|
const zip = this.compiler.compile(file, convertPrettifyType(prettify));
|
2023-06-05 00:33:43 +01:00
|
|
|
|
2023-06-12 18:43:29 +01:00
|
|
|
zip.generateAsync({
|
|
|
|
type: "nodebuffer",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
}).then((z) => {
|
2023-06-05 00:33:43 +01:00
|
|
|
stream.emit("data", z);
|
|
|
|
stream.emit("end");
|
|
|
|
});
|
2023-06-01 02:05:35 +01:00
|
|
|
|
|
|
|
return stream;
|
2022-08-22 13:25:59 -07:00
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
|
|
|
private static readonly compiler = new Compiler();
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|