This commit is contained in:
Dolan
2017-12-06 01:03:14 +00:00
parent ae8a0c7fd0
commit ebec10e312
8 changed files with 123 additions and 121 deletions

View File

@ -0,0 +1,99 @@
import * as archiver from "archiver";
import * as express from "express";
import * as fs from "fs";
import * as path from "path";
import * as xml from "xml";
import { Document } from "../../docx";
import { Media } from "../../media";
import { Numbering } from "../../numbering";
import { Properties } from "../../properties";
import { Styles } from "../../styles";
import { DefaultStylesFactory } from "../../styles/factory";
import { Formatter } from "../formatter";
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
export class Compiler {
protected archive: archiver.Archiver;
private formatter: Formatter;
private style: Styles;
constructor(
protected document: Document,
style?: Styles,
private properties: Properties = new Properties({
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
}),
private numbering: Numbering = new Numbering(),
private media: Media = new Media(),
) {
this.formatter = new Formatter();
this.archive = archiver.create("zip", {});
if (style) {
this.style = style;
} else {
const stylesFactory = new DefaultStylesFactory();
this.style = stylesFactory.newInstance();
}
this.archive.on("error", (err) => {
throw err;
});
}
public async compile(output: fs.WriteStream | express.Response): Promise<void> {
this.archive.pipe(output);
this.archive.glob("**", {
cwd: TEMPLATE_PATH,
});
this.archive.glob("**/.rels", {
cwd: TEMPLATE_PATH,
});
const xmlDocument = xml(this.formatter.format(this.document));
const xmlStyles = xml(this.formatter.format(this.style));
const xmlProperties = xml(this.formatter.format(this.properties), {
declaration: {
standalone: "yes",
encoding: "UTF-8",
},
});
const xmlNumbering = xml(this.formatter.format(this.numbering));
this.archive.append(xmlDocument, {
name: "word/document.xml",
});
this.archive.append(xmlStyles, {
name: "word/styles.xml",
});
this.archive.append(xmlProperties, {
name: "docProps/core.xml",
});
this.archive.append(xmlNumbering, {
name: "word/numbering.xml",
});
for (const data of this.media.array) {
this.archive.append(data.stream, {
name: `media/${data.fileName}`,
});
}
this.archive.finalize();
return new Promise<void>((resolve) => {
output.on("close", () => {
resolve();
});
});
}
}

View File

@ -5,14 +5,16 @@ import { Media } from "../../media";
import { Numbering } from "../../numbering"; import { Numbering } from "../../numbering";
import { Properties } from "../../properties"; import { Properties } from "../../properties";
import { Styles } from "../../styles"; import { Styles } from "../../styles";
import { IPackOptions } from "./pack-options"; import { Compiler } from "./compiler";
import { Packer } from "./packer"; import { IPacker } from "./packer";
export class ExpressPacker extends Packer { export class ExpressPacker implements IPacker {
private res: express.Response; private res: express.Response;
private packer: Compiler;
constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) { constructor(document: Document, res: express.Response, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
super(document, styles, properties, numbering, media); this.packer = new Compiler(document, styles, properties, numbering, media);
this.res = res; this.res = res;
this.res.on("close", () => { this.res.on("close", () => {
@ -20,10 +22,10 @@ export class ExpressPacker extends Packer {
}); });
} }
public pack(name: string, options: IPackOptions): void { public pack(name: string): void {
name = name.replace(/.docx$/, ""); name = name.replace(/.docx$/, "");
this.res.attachment(`${name}.docx`); this.res.attachment(`${name}.docx`);
super.compile(this.res); this.packer.compile(this.res);
} }
} }

View File

@ -9,7 +9,7 @@ import { LocalPacker } from "../../export/packer/local";
import { Properties } from "../../properties"; import { Properties } from "../../properties";
import { DefaultStylesFactory } from "../../styles/factory"; import { DefaultStylesFactory } from "../../styles/factory";
describe("Packer", () => { describe("LocalPacker", () => {
let packer: LocalPacker; let packer: LocalPacker;
let stylesFactory: DefaultStylesFactory; let stylesFactory: DefaultStylesFactory;
@ -52,7 +52,9 @@ describe("Packer", () => {
} }
}, 2000); }, 2000);
}); });
});
describe("#packPdf", () => {
it("should create a standard PDF file", async function () { it("should create a standard PDF file", async function () {
this.timeout(99999999); this.timeout(99999999);

View File

@ -1,32 +1,31 @@
import * as fs from "fs"; import * as fs from "fs";
import * as os from "os"; import * as os from "os";
import * as path from "path"; import * as path from "path";
import * as util from "util";
import { Document } from "../../docx/document"; import { Document } from "../../docx/document";
import { Media } from "../../media"; import { Media } from "../../media";
import { Numbering } from "../../numbering"; import { Numbering } from "../../numbering";
import { Properties } from "../../properties"; import { Properties } from "../../properties";
import { Styles } from "../../styles"; import { Styles } from "../../styles";
import { IPackOptions } from "./pack-options"; import { Compiler } from "./compiler";
import { Packer } from "./packer"; import { IPacker } from "./packer";
import { PdfConvertWrapper } from "./pdf-convert-wrapper"; import { PdfConvertWrapper } from "./pdf-convert-wrapper";
export class LocalPacker extends Packer { export class LocalPacker implements IPacker {
private stream: fs.WriteStream; private stream: fs.WriteStream;
private pdfConverter: PdfConvertWrapper; private pdfConverter: PdfConvertWrapper;
private packer: Compiler;
constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) { constructor(document: Document, styles?: Styles, properties?: Properties, numbering?: Numbering, media?: Media) {
super(document, styles, properties, numbering, media);
this.pdfConverter = new PdfConvertWrapper(); this.pdfConverter = new PdfConvertWrapper();
this.packer = new Compiler(document, styles, properties, numbering, media);
} }
public pack(filePath: string): void { public pack(filePath: string): void {
filePath = filePath.replace(/.docx$/, ""); filePath = filePath.replace(/.docx$/, "");
this.stream = fs.createWriteStream(`${filePath}.docx`); this.stream = fs.createWriteStream(`${filePath}.docx`);
super.compile(this.stream); this.packer.compile(this.stream);
} }
public async packPdf(filePath: string): Promise<void> { public async packPdf(filePath: string): Promise<void> {
@ -35,7 +34,7 @@ export class LocalPacker extends Packer {
const fileName = path.basename(filePath, path.extname(filePath)); const fileName = path.basename(filePath, path.extname(filePath));
const tempPath = path.join(os.tmpdir(), `${fileName}.docx`); const tempPath = path.join(os.tmpdir(), `${fileName}.docx`);
this.stream = fs.createWriteStream(tempPath); this.stream = fs.createWriteStream(tempPath);
await super.compile(this.stream); await this.packer.compile(this.stream);
const text = await this.pdfConverter.convert(tempPath); const text = await this.pdfConverter.convert(tempPath);
// const writeFile = util.promisify(fs.writeFile); --use this in future, in 3 years time. Only in node 8 // const writeFile = util.promisify(fs.writeFile); --use this in future, in 3 years time. Only in node 8
// return writeFile(`${filePath}.pdf`, text); // return writeFile(`${filePath}.pdf`, text);

View File

@ -1,3 +0,0 @@
/* tslint:disable-next-line:no-empty-interface */
export interface IPackOptions {
}

View File

@ -1,102 +1,3 @@
import * as archiver from "archiver"; export interface IPacker {
import * as express from "express"; pack(path: string): void;
import * as fs from "fs";
import * as path from "path";
import * as xml from "xml";
import { Document } from "../../docx";
import { Media } from "../../media";
import { Numbering } from "../../numbering";
import { Properties } from "../../properties";
import { Styles } from "../../styles";
import { DefaultStylesFactory } from "../../styles/factory";
import { Formatter } from "../formatter";
import { IPackOptions } from "./pack-options";
const TEMPLATE_PATH = path.resolve(__dirname, "../../../template");
export abstract class Packer {
protected archive: archiver.Archiver;
private formatter: Formatter;
private style: Styles;
constructor(
protected document: Document,
style?: Styles,
private properties: Properties = new Properties({
creator: "Un-named",
revision: "1",
lastModifiedBy: "Un-named",
}),
private numbering: Numbering = new Numbering(),
private media: Media = new Media(),
) {
this.formatter = new Formatter();
this.archive = archiver.create("zip", {});
if (style) {
this.style = style;
} else {
const stylesFactory = new DefaultStylesFactory();
this.style = stylesFactory.newInstance();
}
this.archive.on("error", (err) => {
throw err;
});
}
public abstract pack(path: string, options?: IPackOptions): void;
protected async compile(output: fs.WriteStream | express.Response): Promise<void> {
this.archive.pipe(output);
this.archive.glob("**", {
cwd: TEMPLATE_PATH,
});
this.archive.glob("**/.rels", {
cwd: TEMPLATE_PATH,
});
const xmlDocument = xml(this.formatter.format(this.document));
const xmlStyles = xml(this.formatter.format(this.style));
const xmlProperties = xml(this.formatter.format(this.properties), {
declaration: {
standalone: "yes",
encoding: "UTF-8",
},
});
const xmlNumbering = xml(this.formatter.format(this.numbering));
this.archive.append(xmlDocument, {
name: "word/document.xml",
});
this.archive.append(xmlStyles, {
name: "word/styles.xml",
});
this.archive.append(xmlProperties, {
name: "docProps/core.xml",
});
this.archive.append(xmlNumbering, {
name: "word/numbering.xml",
});
for (const data of this.media.array) {
this.archive.append(data.stream, {
name: `media/${data.fileName}`,
});
}
this.archive.finalize();
return new Promise<void>((resolve) => {
output.on("close", () => {
resolve();
});
});
}
} }

View File

@ -8,7 +8,8 @@
"outDir": "../build-tests", "outDir": "../build-tests",
"sourceRoot": "./", "sourceRoot": "./",
"rootDir": "./", "rootDir": "./",
"module": "commonjs" "module": "commonjs",
"noUnusedLocals": true
}, },
"include": [ "include": [
"**/*.spec.ts", "**/*.spec.ts",

View File

@ -9,7 +9,8 @@
"sourceRoot": "./", "sourceRoot": "./",
"rootDir": "./", "rootDir": "./",
"module": "commonjs", "module": "commonjs",
"declaration": true "declaration": true,
"noUnusedLocals": true
}, },
"exclude": [ "exclude": [
"tests", "tests",