2016-03-31 18:07:22 +01:00
|
|
|
import * as archiver from "archiver";
|
2017-05-09 18:03:31 +01:00
|
|
|
import * as express from "express";
|
2017-04-15 23:45:27 +01:00
|
|
|
import * as fs from "fs";
|
2017-03-13 20:35:42 +01:00
|
|
|
import * as path from "path";
|
2016-04-05 01:49:12 +01:00
|
|
|
import * as xml from "xml";
|
2017-09-30 18:15:33 +01:00
|
|
|
|
2017-03-08 21:54:52 +00:00
|
|
|
import { Document } from "../../docx";
|
2017-03-25 21:50:33 +00:00
|
|
|
import { Media } from "../../media";
|
2017-03-08 21:54:52 +00:00
|
|
|
import { Numbering } from "../../numbering";
|
|
|
|
import { Properties } from "../../properties";
|
|
|
|
import { Styles } from "../../styles";
|
|
|
|
import { DefaultStylesFactory } from "../../styles/factory";
|
|
|
|
import { Formatter } from "../formatter";
|
2017-09-30 18:15:33 +01:00
|
|
|
import { IPackOptions } from "./pack-options";
|
2016-05-21 03:08:09 +01:00
|
|
|
|
2017-03-27 01:28:31 +01:00
|
|
|
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
|
2017-03-13 20:35:42 +01:00
|
|
|
|
2016-03-31 19:28:12 +01:00
|
|
|
export abstract class Packer {
|
2017-05-09 18:03:31 +01:00
|
|
|
protected archive: archiver.Archiver;
|
2017-03-08 21:54:52 +00:00
|
|
|
private formatter: Formatter;
|
2016-04-09 04:27:49 +01:00
|
|
|
private style: Styles;
|
2016-05-26 15:08:34 +01:00
|
|
|
|
2017-03-13 23:30:26 +00:00
|
|
|
constructor(
|
|
|
|
protected document: Document,
|
|
|
|
style?: Styles,
|
|
|
|
private properties: Properties = new Properties({
|
|
|
|
creator: "Un-named",
|
|
|
|
revision: "1",
|
|
|
|
lastModifiedBy: "Un-named",
|
|
|
|
}),
|
|
|
|
private numbering: Numbering = new Numbering(),
|
2017-03-25 21:50:33 +00:00
|
|
|
private media: Media = new Media(),
|
2017-03-13 23:30:26 +00:00
|
|
|
) {
|
2016-03-31 19:28:12 +01:00
|
|
|
this.formatter = new Formatter();
|
2016-03-31 18:07:22 +01:00
|
|
|
this.archive = archiver.create("zip", {});
|
2016-03-31 19:04:54 +01:00
|
|
|
|
2017-03-10 14:30:07 +01:00
|
|
|
if (style) {
|
|
|
|
this.style = style;
|
|
|
|
} else {
|
2017-03-08 21:54:52 +00:00
|
|
|
const stylesFactory = new DefaultStylesFactory();
|
2016-07-04 19:01:30 +01:00
|
|
|
this.style = stylesFactory.newInstance();
|
2016-07-04 18:47:13 +01:00
|
|
|
}
|
|
|
|
|
2016-05-26 15:08:34 +01:00
|
|
|
this.archive.on("error", (err) => {
|
2016-03-31 19:04:54 +01:00
|
|
|
throw err;
|
|
|
|
});
|
2016-03-31 18:03:16 +01:00
|
|
|
}
|
|
|
|
|
2017-09-30 18:15:33 +01:00
|
|
|
public abstract pack(path: string, options?: IPackOptions): void;
|
|
|
|
|
2017-05-09 18:03:31 +01:00
|
|
|
protected compile(output: fs.WriteStream | express.Response): void {
|
2016-03-31 18:03:16 +01:00
|
|
|
this.archive.pipe(output);
|
2017-03-07 13:49:21 +01:00
|
|
|
this.archive.glob("**", {
|
2017-03-27 01:28:31 +01:00
|
|
|
cwd: TEMPLATE_PATH,
|
2017-03-07 13:49:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this.archive.glob("**/.rels", {
|
2017-03-27 01:28:31 +01:00
|
|
|
cwd: TEMPLATE_PATH,
|
2017-03-07 13:49:21 +01:00
|
|
|
});
|
2016-03-31 19:04:54 +01:00
|
|
|
|
2017-03-08 21:54:52 +00:00
|
|
|
const xmlDocument = xml(this.formatter.format(this.document));
|
|
|
|
const xmlStyles = xml(this.formatter.format(this.style));
|
2017-03-25 21:50:33 +00:00
|
|
|
const xmlProperties = xml(this.formatter.format(this.properties), {
|
|
|
|
declaration: {
|
|
|
|
standalone: "yes",
|
|
|
|
encoding: "UTF-8",
|
|
|
|
},
|
|
|
|
});
|
2017-03-08 21:54:52 +00:00
|
|
|
const xmlNumbering = xml(this.formatter.format(this.numbering));
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2016-04-05 01:49:12 +01:00
|
|
|
this.archive.append(xmlDocument, {
|
2017-03-08 21:54:52 +00:00
|
|
|
name: "word/document.xml",
|
2016-04-03 05:28:37 +01:00
|
|
|
});
|
|
|
|
|
2016-05-21 03:08:09 +01:00
|
|
|
this.archive.append(xmlStyles, {
|
2017-03-08 21:54:52 +00:00
|
|
|
name: "word/styles.xml",
|
2016-05-09 03:44:16 +01:00
|
|
|
});
|
2016-04-03 05:28:37 +01:00
|
|
|
|
2016-04-05 01:49:12 +01:00
|
|
|
this.archive.append(xmlProperties, {
|
2017-03-08 21:54:52 +00:00
|
|
|
name: "docProps/core.xml",
|
2016-04-03 05:28:37 +01:00
|
|
|
});
|
2016-05-26 15:08:34 +01:00
|
|
|
|
2016-05-21 03:08:09 +01:00
|
|
|
this.archive.append(xmlNumbering, {
|
2017-03-08 21:54:52 +00:00
|
|
|
name: "word/numbering.xml",
|
2016-05-26 15:08:34 +01:00
|
|
|
});
|
2016-04-03 05:28:37 +01:00
|
|
|
|
2017-03-25 21:50:33 +00:00
|
|
|
for (const data of this.media.array) {
|
|
|
|
this.archive.append(data.stream, {
|
|
|
|
name: `media/${data.fileName}`,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:04:54 +01:00
|
|
|
this.archive.finalize();
|
2016-03-31 18:03:16 +01:00
|
|
|
}
|
2017-09-30 18:15:33 +01:00
|
|
|
|
|
|
|
protected convertToPdf(): void {
|
|
|
|
// TODO
|
|
|
|
}
|
2017-03-08 21:54:52 +00:00
|
|
|
}
|