Use single media instead of multiple

This commit is contained in:
Dolan
2018-10-23 00:31:51 +01:00
parent a5bedf9a5b
commit 9c66db97ff
8 changed files with 49 additions and 44 deletions

View File

@ -8,11 +8,11 @@ const doc = new Document();
const paragraph = new Paragraph("Hello World"); const paragraph = new Paragraph("Hello World");
doc.addParagraph(paragraph); doc.addParagraph(paragraph);
const image = Media.addImage(doc, "./demo/images/image1.jpeg"); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
const image2 = Media.addImage(doc, "./demo/images/dog.png"); const image2 = Media.addImage(doc, fs.readFileSync("./demo/images/dog.png"));
const image3 = Media.addImage(doc, "./demo/images/cat.jpg"); const image3 = Media.addImage(doc, fs.readFileSync("./demo/images/cat.jpg"));
const image4 = Media.addImage(doc, "./demo/images/parrots.bmp"); const image4 = Media.addImage(doc, fs.readFileSync("./demo/images/parrots.bmp"));
const image5 = Media.addImage(doc, "./demo/images/pizza.gif"); const image5 = Media.addImage(doc, fs.readFileSync("./demo/images/pizza.gif"));
const imageBase64Data = `iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAACzVBMVEUAAAAAAAAAAAAAAAA/AD8zMzMqKiokJCQfHx8cHBwZGRkuFxcqFSonJyckJCQiIiIfHx8eHh4cHBwoGhomGSYkJCQhISEfHx8eHh4nHR0lHBwkGyQjIyMiIiIgICAfHx8mHh4lHh4kHR0jHCMiGyIhISEgICAfHx8lHx8kHh4jHR0hHCEhISEgICAlHx8kHx8jHh4jHh4iHSIhHCEhISElICAkHx8jHx8jHh4iHh4iHSIhHSElICAkICAjHx8jHx8iHh4iHh4hHiEhHSEkICAjHx8iHx8iHx8hHh4hHiEkHSEjHSAjHx8iHx8iHx8hHh4kHiEkHiEjHSAiHx8hHx8hHh4kHiEjHiAjHSAiHx8iHx8hHx8kHh4jHiEjHiAjHiAiICAiHx8kHx8jHh4jHiEjHiAiHiAiHSAiHx8jHx8jHx8jHiAiHiAiHiAiHSAiHx8jHx8jHx8iHiAiHi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6jaWi1ALNYCocZROj4JdEikroVkjFk9DcStXxpdfCD2MoXodu4RUU9ptxxmXssOfxnvDVcxRTod9FxyhqLoAqis5aPhwTDp9spRgEH2Q6KLbYoKqlaKTm6Isp0C/sJMnjFvhiERXPQvUNRe9p29lhR04CdBpC8Sl8YiuncIxEuzUUg4Dkgj+paVozygY9plPMh28SaymO9kabAopREGF3vt9MzeFFl8G7lRSZ8FFGK8XX4VA8QjEd7XrM3M0OXz8YCy+qKBLgq3wqnofiTorF0Ax56Rg1J1elW+BBAsVe+My6iYq7IK6keBdOIseV2qn5Pb8f3MqkWAXf9ThM8c8lAOIotuFsF875lRrH5klRcG0+xcPwQ1oLxfeRAP4heQTnGL78X2rqlw2DK59SXAV/zKaiGMAuko5InCt68mcOan5+ohf+z1pP8lQY/GHZQMV4YD3FpXDp4qerqbF/lBWBswyi+AL+ia+maLgcRRQj4IYlY/UpauqKBsPJAxQF8NM1TRQ/RudSPAD34rK3scOuR8/HGcspxsJfOVS8NZbiGXiUtPgINU3v3WFDmx8pEuG3EiqKKVbCC1vm2iZqap5LAtCtleQf8F9sFYWDohzeJczYyQ4V2bEZFGsQgJRGqqqhS2phHTWn9lDkIhBTqWqxQZ+IsRvtdHY9AvI2VX2hW68nfqGmuQsCEl3JdjfCF8OW1bPdtwhQ0gm2mQzfRE3a7KCYj0BNZJs8+Kxf/r6WtTEI2FIqlsMfFgRB5A6KUnSe/vUkX0AnuvUIt8SjM1m6wWQymUwmk8lkMgXRf5vi8rLQxtUhAAAAAElFTkSuQmCC`;
const image6 = Media.addImage(doc, Buffer.from(imageBase64Data, "base64"), 100, 100); const image6 = Media.addImage(doc, Buffer.from(imageBase64Data, "base64"), 100, 100);

View File

@ -8,7 +8,7 @@ const doc = new Document();
const table = doc.createTable(4, 4); const table = doc.createTable(4, 4);
table.getCell(2, 2).addContent(new Paragraph("Hello")); table.getCell(2, 2).addContent(new Paragraph("Hello"));
const image = Media.addImage(doc, "./demo/images/image1.jpeg"); const image = Media.addImage(doc, fs.readFileSync("./demo/images/image1.jpeg"));
table.getCell(1, 1).addContent(image.Paragraph); table.getCell(1, 1).addContent(image.Paragraph);
const packer = new Packer(); const packer = new Packer();

View File

@ -3,12 +3,15 @@ import { expect } from "chai";
import { Formatter } from "../../../../export/formatter"; import { Formatter } from "../../../../export/formatter";
import { FooterWrapper } from "../../../footer-wrapper"; import { FooterWrapper } from "../../../footer-wrapper";
import { HeaderWrapper } from "../../../header-wrapper"; import { HeaderWrapper } from "../../../header-wrapper";
import { Media } from "../../../media";
import { PageBorderOffsetFrom, PageNumberFormat } from "./"; import { PageBorderOffsetFrom, PageNumberFormat } from "./";
import { SectionProperties } from "./section-properties"; import { SectionProperties } from "./section-properties";
describe("SectionProperties", () => { describe("SectionProperties", () => {
describe("#constructor()", () => { describe("#constructor()", () => {
it("should create section properties with options", () => { it("should create section properties with options", () => {
const media = new Media();
const properties = new SectionProperties({ const properties = new SectionProperties({
width: 11906, width: 11906,
height: 16838, height: 16838,
@ -23,10 +26,10 @@ describe("SectionProperties", () => {
space: 708, space: 708,
linePitch: 360, linePitch: 360,
headers: { headers: {
default: new HeaderWrapper(100), default: new HeaderWrapper(media, 100),
}, },
footers: { footers: {
even: new FooterWrapper(200), even: new FooterWrapper(media, 200),
}, },
pageNumberStart: 10, pageNumberStart: 10,
pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT, pageNumberFormatType: PageNumberFormat.CARDINAL_TEXT,

View File

@ -173,13 +173,13 @@ export class File {
} }
public createHeader(): HeaderWrapper { public createHeader(): HeaderWrapper {
const header = new HeaderWrapper(this.currentRelationshipId++); const header = new HeaderWrapper(this.media, this.currentRelationshipId++);
this.addHeaderToDocument(header); this.addHeaderToDocument(header);
return header; return header;
} }
public createFooter(): FooterWrapper { public createFooter(): FooterWrapper {
const footer = new FooterWrapper(this.currentRelationshipId++); const footer = new FooterWrapper(this.media, this.currentRelationshipId++);
this.addFooterToDocument(footer); this.addFooterToDocument(footer);
return footer; return footer;
} }

View File

@ -1,8 +1,8 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { FooterReferenceType } from "./document"; import { FooterReferenceType } from "./document";
import { Footer } from "./footer/footer"; import { Footer } from "./footer/footer";
import { Image, Media } from "./media"; import { Image, IMediaData, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -15,10 +15,8 @@ export interface IDocumentFooter {
export class FooterWrapper { export class FooterWrapper {
private readonly footer: Footer; private readonly footer: Footer;
private readonly relationships: Relationships; private readonly relationships: Relationships;
private readonly media: Media;
constructor(referenceId: number, initContent?: XmlComponent) { constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) {
this.media = new Media();
this.footer = new Footer(referenceId, initContent); this.footer = new Footer(referenceId, initContent);
this.relationships = new Relationships(); this.relationships = new Relationships();
} }
@ -48,7 +46,7 @@ export class FooterWrapper {
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData { public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height); const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship( this.relationships.createRelationship(
refId, mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`, `media/${mediaData.fileName}`,
); );
@ -64,8 +62,10 @@ export class FooterWrapper {
); );
} }
public createImage(image: Buffer, width?: number, height?: number): void { public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height); // TODO
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData))); this.addImage(new Image(new ImageParagraph(mediaData)));
} }

View File

@ -1,8 +1,8 @@
import { IMediaData } from "file/media";
import { XmlComponent } from "file/xml-components"; import { XmlComponent } from "file/xml-components";
import { HeaderReferenceType } from "./document"; import { HeaderReferenceType } from "./document";
import { Header } from "./header/header"; import { Header } from "./header/header";
import { Image, Media } from "./media"; import { Image, IMediaData, Media } from "./media";
import { ImageParagraph, Paragraph } from "./paragraph"; import { ImageParagraph, Paragraph } from "./paragraph";
import { Relationships } from "./relationships"; import { Relationships } from "./relationships";
import { Table } from "./table"; import { Table } from "./table";
@ -15,10 +15,8 @@ export interface IDocumentHeader {
export class HeaderWrapper { export class HeaderWrapper {
private readonly header: Header; private readonly header: Header;
private readonly relationships: Relationships; private readonly relationships: Relationships;
private readonly media: Media;
constructor(referenceId: number, initContent?: XmlComponent) { constructor(private readonly media: Media, referenceId: number, initContent?: XmlComponent) {
this.media = new Media();
this.header = new Header(referenceId, initContent); this.header = new Header(referenceId, initContent);
this.relationships = new Relationships(); this.relationships = new Relationships();
} }
@ -48,7 +46,7 @@ export class HeaderWrapper {
public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData { public addImageRelationship(image: Buffer, refId: number, width?: number, height?: number): IMediaData {
const mediaData = this.media.addMedia(image, refId, width, height); const mediaData = this.media.addMedia(image, refId, width, height);
this.relationships.createRelationship( this.relationships.createRelationship(
refId, mediaData.referenceId,
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
`media/${mediaData.fileName}`, `media/${mediaData.fileName}`,
); );
@ -64,8 +62,10 @@ export class HeaderWrapper {
); );
} }
public createImage(image: Buffer, width?: number, height?: number): void { public createImage(image: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): void {
const mediaData = this.addImageRelationship(image, this.relationships.RelationshipCount, width, height); // TODO
// tslint:disable-next-line:no-any
const mediaData = this.addImageRelationship(image as any, this.relationships.RelationshipCount, width, height);
this.addImage(new Image(new ImageParagraph(mediaData))); this.addImage(new Image(new ImageParagraph(mediaData)));
} }

View File

@ -87,7 +87,7 @@ export class Media {
} }
const imageData = { const imageData = {
referenceId: this.map.size + relationshipsCount + 1, referenceId: relationshipsCount,
stream: data, stream: data,
path: filePath, path: filePath,
fileName: key, fileName: key,

View File

@ -5,10 +5,10 @@ import { FooterReferenceType } from "file/document/body/section-properties/foote
import { HeaderReferenceType } from "file/document/body/section-properties/header-reference"; import { HeaderReferenceType } from "file/document/body/section-properties/header-reference";
import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper"; import { FooterWrapper, IDocumentFooter } from "file/footer-wrapper";
import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper"; import { HeaderWrapper, IDocumentHeader } from "file/header-wrapper";
import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components"; import { Media } from "file/Media";
import { Styles } from "file/styles"; import { Styles } from "file/styles";
import { ExternalStylesFactory } from "file/styles/external-styles-factory"; import { ExternalStylesFactory } from "file/styles/external-styles-factory";
import { convertToXmlComponent, ImportedXmlComponent, parseOptions } from "file/xml-components";
const importParseOptions = { const importParseOptions = {
...parseOptions, ...parseOptions,
@ -65,37 +65,39 @@ export class ImportDotx {
const relationshipContent = zipContent.files["word/_rels/document.xml.rels"]; const relationshipContent = zipContent.files["word/_rels/document.xml.rels"];
const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text")); const documentRelationships: IRelationshipFileInfo[] = this.findReferenceFiles(await relationshipContent.async("text"));
const media = new Media();
const headers: IDocumentHeader[] = []; const headers: IDocumentHeader[] = [];
for (const headerRef of documentRefs.headers) { for (const headerRef of documentRefs.headers) {
const headerKey = "w:hdr"; const headerKey = "w:hdr";
const relationFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id); const relationshipFileInfo = documentRelationships.find((rel) => rel.id === headerRef.id);
if (relationFileInfo === null || !relationFileInfo) { if (!relationshipFileInfo) {
throw new Error(`Can not find target file for id ${headerRef.id}`); throw new Error(`Can not find target file for id ${headerRef.id}`);
} }
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text"); const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text");
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent; const importedComp = convertToXmlComponent(headerKey, xmlObj[headerKey]) as ImportedXmlComponent;
const header = new HeaderWrapper(this.currentRelationshipId++, importedComp); const header = new HeaderWrapper(media, this.currentRelationshipId++, importedComp);
await this.addRelationToWrapper(relationFileInfo, zipContent, header); await this.addRelationToWrapper(relationshipFileInfo, zipContent, header);
headers.push({ type: headerRef.type, header }); headers.push({ type: headerRef.type, header });
} }
const footers: IDocumentFooter[] = []; const footers: IDocumentFooter[] = [];
for (const footerRef of documentRefs.footers) { for (const footerRef of documentRefs.footers) {
const footerKey = "w:ftr"; const footerKey = "w:ftr";
const relationFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id); const relationshipFileInfo = documentRelationships.find((rel) => rel.id === footerRef.id);
if (relationFileInfo === null || !relationFileInfo) { if (!relationshipFileInfo) {
throw new Error(`Can not find target file for id ${footerRef.id}`); throw new Error(`Can not find target file for id ${footerRef.id}`);
} }
const xmlData = await zipContent.files[`word/${relationFileInfo.target}`].async("text"); const xmlData = await zipContent.files[`word/${relationshipFileInfo.target}`].async("text");
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent; const importedComp = convertToXmlComponent(footerKey, xmlObj[footerKey]) as ImportedXmlComponent;
const footer = new FooterWrapper(this.currentRelationshipId++, importedComp); const footer = new FooterWrapper(media, this.currentRelationshipId++, importedComp);
await this.addRelationToWrapper(relationFileInfo, zipContent, footer); await this.addRelationToWrapper(relationshipFileInfo, zipContent, footer);
footers.push({ type: footerRef.type, footer }); footers.push({ type: footerRef.type, footer });
} }
@ -110,13 +112,13 @@ export class ImportDotx {
} }
public async addRelationToWrapper( public async addRelationToWrapper(
relationFile: IRelationshipFileInfo, relationhipFile: IRelationshipFileInfo,
zipContent: JSZip, zipContent: JSZip,
wrapper: HeaderWrapper | FooterWrapper, wrapper: HeaderWrapper | FooterWrapper,
): Promise<void> { ): Promise<void> {
let wrapperImagesReferences: IRelationshipFileInfo[] = []; let wrapperImagesReferences: IRelationshipFileInfo[] = [];
let hyperLinkReferences: IRelationshipFileInfo[] = []; let hyperLinkReferences: IRelationshipFileInfo[] = [];
const refFile = zipContent.files[`word/_rels/${relationFile.target}.rels`]; const refFile = zipContent.files[`word/_rels/${relationhipFile.target}.rels`];
if (refFile) { if (refFile) {
const xmlRef = await refFile.async("text"); const xmlRef = await refFile.async("text");
wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image"); wrapperImagesReferences = this.findReferenceFiles(xmlRef).filter((r) => r.type === "image");
@ -133,10 +135,10 @@ export class ImportDotx {
public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] { public findReferenceFiles(xmlData: string): IRelationshipFileInfo[] {
const xmlObj = fastXmlParser.parse(xmlData, importParseOptions); const xmlObj = fastXmlParser.parse(xmlData, importParseOptions);
const relationXmlArray = Array.isArray(xmlObj.Relationships.Relationship) const relationshipXmlArray = Array.isArray(xmlObj.Relationships.Relationship)
? xmlObj.Relationships.Relationship ? xmlObj.Relationships.Relationship
: [xmlObj.Relationships.Relationship]; : [xmlObj.Relationships.Relationship];
const relations: IRelationshipFileInfo[] = relationXmlArray const relationships: IRelationshipFileInfo[] = relationshipXmlArray
.map((item) => { .map((item) => {
return { return {
id: this.parseRefId(item._attr.Id), id: this.parseRefId(item._attr.Id),
@ -145,7 +147,7 @@ export class ImportDotx {
}; };
}) })
.filter((item) => item.type !== null); .filter((item) => item.type !== null);
return relations; return relationships;
} }
public extractDocumentRefs(xmlData: string): IDocumentRefs { public extractDocumentRefs(xmlData: string): IDocumentRefs {