Merge branch 'master' of github.com:h4buli/docx
This commit is contained in:
@ -13,6 +13,7 @@ import { Styles } from "./styles";
|
|||||||
import { DefaultStylesFactory } from "./styles/factory";
|
import { DefaultStylesFactory } from "./styles/factory";
|
||||||
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
import { ExternalStylesFactory } from "./styles/external-styles-factory";
|
||||||
import { Table } from "./table";
|
import { Table } from "./table";
|
||||||
|
import { IMediaData } from "index";
|
||||||
|
|
||||||
export class File {
|
export class File {
|
||||||
private readonly document: Document;
|
private readonly document: Document;
|
||||||
@ -118,6 +119,16 @@ export class File {
|
|||||||
this.document.createDrawing(mediaData);
|
this.document.createDrawing(mediaData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public createImageData(imageName: string, data: Buffer, width?: number, height?: number): IMediaData {
|
||||||
|
const mediaData = this.media.addMediaWithData(imageName, data, this.docRelationships.RelationshipCount, width, height);
|
||||||
|
this.docRelationships.createRelationship(
|
||||||
|
mediaData.referenceId,
|
||||||
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||||
|
`media/${mediaData.fileName}`,
|
||||||
|
);
|
||||||
|
return mediaData;
|
||||||
|
}
|
||||||
|
|
||||||
public get Document(): Document {
|
public get Document(): Document {
|
||||||
return this.document;
|
return this.document;
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ export interface IMediaDataDimensions {
|
|||||||
|
|
||||||
export interface IMediaData {
|
export interface IMediaData {
|
||||||
referenceId: number;
|
referenceId: number;
|
||||||
stream: fs.ReadStream;
|
stream: fs.ReadStream | Buffer;
|
||||||
path: string;
|
path?: string;
|
||||||
fileName: string;
|
fileName: string;
|
||||||
dimensions: IMediaDataDimensions;
|
dimensions: IMediaDataDimensions;
|
||||||
}
|
}
|
||||||
|
@ -11,23 +11,10 @@ export class Media {
|
|||||||
this.map = new Map<string, IMediaData>();
|
this.map = new Map<string, IMediaData>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public getMedia(key: string): IMediaData {
|
private createMedia(key: string, relationshipsCount, dimensions, data: fs.ReadStream | Buffer, filePath?: string, ) {
|
||||||
const data = this.map.get(key);
|
|
||||||
|
|
||||||
if (data === undefined) {
|
|
||||||
throw new Error(`Cannot find image with the key ${key}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public addMedia(filePath: string, relationshipsCount: number): IMediaData {
|
|
||||||
const key = path.basename(filePath);
|
|
||||||
const dimensions = sizeOf(filePath);
|
|
||||||
|
|
||||||
const imageData = {
|
const imageData = {
|
||||||
referenceId: this.map.size + relationshipsCount + 1,
|
referenceId: this.map.size + relationshipsCount + 1,
|
||||||
stream: fs.createReadStream(filePath),
|
stream: data,
|
||||||
path: filePath,
|
path: filePath,
|
||||||
fileName: key,
|
fileName: key,
|
||||||
dimensions: {
|
dimensions: {
|
||||||
@ -45,6 +32,36 @@ export class Media {
|
|||||||
|
|
||||||
return imageData;
|
return imageData;
|
||||||
}
|
}
|
||||||
|
public getMedia(key: string): IMediaData {
|
||||||
|
const data = this.map.get(key);
|
||||||
|
|
||||||
|
if (data === undefined) {
|
||||||
|
throw new Error(`Cannot find image with the key ${key}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public addMedia(filePath: string, relationshipsCount: number): IMediaData {
|
||||||
|
const key = path.basename(filePath);
|
||||||
|
const dimensions = sizeOf(filePath);
|
||||||
|
return this.createMedia(key, relationshipsCount, dimensions, fs.createReadStream(filePath), filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
public addMediaWithData(fileName: string, data: Buffer, relationshipsCount: number, width?, height?): IMediaData {
|
||||||
|
const key = fileName;
|
||||||
|
let dimensions;
|
||||||
|
if (width && height) {
|
||||||
|
dimensions = {
|
||||||
|
width: width,
|
||||||
|
height: height
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dimensions = sizeOf(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.createMedia(key, relationshipsCount, dimensions, data);
|
||||||
|
}
|
||||||
|
|
||||||
public get array(): IMediaData[] {
|
public get array(): IMediaData[] {
|
||||||
const array = new Array<IMediaData>();
|
const array = new Array<IMediaData>();
|
||||||
|
Reference in New Issue
Block a user