2022-06-26 23:26:42 +01:00
|
|
|
import { File } from "@file/file";
|
2022-08-22 13:25:59 -07:00
|
|
|
import { Stream } from "stream";
|
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
|
|
|
|
*/
|
2022-07-05 05:06:32 +01:00
|
|
|
export enum PrettifyType {
|
2022-03-07 20:02:14 +08:00
|
|
|
NONE = "",
|
|
|
|
WITH_2_BLANKS = " ",
|
|
|
|
WITH_4_BLANKS = " ",
|
2022-06-19 00:31:36 +01:00
|
|
|
WITH_TAB = "\t",
|
2022-03-07 20:02:14 +08:00
|
|
|
}
|
|
|
|
|
2018-08-14 01:46:48 +01:00
|
|
|
export class Packer {
|
2022-08-17 18:52:19 +02:00
|
|
|
public static async toString(file: File, prettify?: boolean | PrettifyType): Promise<string> {
|
|
|
|
const zip = this.compiler.compile(file, prettify);
|
|
|
|
const zipData = await zip.generateAsync({
|
|
|
|
type: "string",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
|
|
|
|
return zipData;
|
|
|
|
}
|
|
|
|
|
2022-07-05 05:06:32 +01:00
|
|
|
public static async toBuffer(file: File, prettify?: boolean | PrettifyType): Promise<Buffer> {
|
2019-08-07 22:12:14 +01:00
|
|
|
const zip = this.compiler.compile(file, prettify);
|
2019-08-15 00:48:36 +01:00
|
|
|
const zipData = await zip.generateAsync({
|
2018-11-12 12:32:07 +00:00
|
|
|
type: "nodebuffer",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
2019-08-09 09:41:58 +02:00
|
|
|
compression: "DEFLATE",
|
2019-08-15 00:48:36 +01:00
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
|
|
|
return zipData;
|
|
|
|
}
|
2018-01-31 20:08:36 +00:00
|
|
|
|
2022-07-05 05:06:32 +01:00
|
|
|
public static async toBase64String(file: File, prettify?: boolean | PrettifyType): Promise<string> {
|
2019-08-07 22:12:14 +01:00
|
|
|
const zip = this.compiler.compile(file, prettify);
|
2019-08-15 00:48:36 +01:00
|
|
|
const zipData = await zip.generateAsync({
|
2018-11-12 12:32:07 +00:00
|
|
|
type: "base64",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
2019-08-09 09:41:58 +02:00
|
|
|
compression: "DEFLATE",
|
2019-08-15 00:48:36 +01:00
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
|
|
|
return zipData;
|
|
|
|
}
|
|
|
|
|
2022-07-05 05:06:32 +01:00
|
|
|
public static async toBlob(file: File, prettify?: boolean | PrettifyType): Promise<Blob> {
|
2019-08-07 22:12:14 +01:00
|
|
|
const zip = this.compiler.compile(file, prettify);
|
2019-08-15 00:48:36 +01:00
|
|
|
const zipData = await zip.generateAsync({
|
2018-11-12 12:32:07 +00:00
|
|
|
type: "blob",
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
2019-08-09 09:41:58 +02:00
|
|
|
compression: "DEFLATE",
|
2019-08-15 00:48:36 +01:00
|
|
|
});
|
2018-08-14 01:46:48 +01:00
|
|
|
|
|
|
|
return zipData;
|
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
2022-08-22 13:25:59 -07:00
|
|
|
public static async toStream(file: File, prettify?: boolean | PrettifyType): Promise<Stream> {
|
|
|
|
const zip = this.compiler.compile(file, prettify);
|
|
|
|
const zipData = zip.generateNodeStream({
|
|
|
|
type: "nodebuffer",
|
|
|
|
streamFiles: true,
|
|
|
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
});
|
|
|
|
|
|
|
|
return zipData;
|
|
|
|
}
|
2019-08-07 22:12:14 +01:00
|
|
|
|
|
|
|
private static readonly compiler = new Compiler();
|
2018-08-14 01:46:48 +01:00
|
|
|
}
|