From 2119ae769b738ca18e10015404d6357ae66f4f0a Mon Sep 17 00:00:00 2001 From: h4buli <34742290+h4buli@users.noreply.github.com> Date: Fri, 20 Apr 2018 15:59:06 +0200 Subject: [PATCH] Images: Extend API for working with images (#5) * extend creating image using buffer and dimensions from outside * remove empty space --- src/file/file.ts | 11 ++++++++++ src/file/media/data.ts | 4 ++-- src/file/media/media.ts | 47 ++++++++++++++++++++++++++++------------- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/file/file.ts b/src/file/file.ts index e7e14c207d..dce3ef24b1 100644 --- a/src/file/file.ts +++ b/src/file/file.ts @@ -13,6 +13,7 @@ import { Styles } from "./styles"; import { DefaultStylesFactory } from "./styles/factory"; import { ExternalStylesFactory } from "./styles/external-styles-factory"; import { Table } from "./table"; +import { IMediaData } from "index"; export class File { private readonly document: Document; @@ -118,6 +119,16 @@ export class File { 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 { return this.document; } diff --git a/src/file/media/data.ts b/src/file/media/data.ts index 00836ed962..cbc7d8c5bf 100644 --- a/src/file/media/data.ts +++ b/src/file/media/data.ts @@ -13,8 +13,8 @@ export interface IMediaDataDimensions { export interface IMediaData { referenceId: number; - stream: fs.ReadStream; - path: string; + stream: fs.ReadStream | Buffer; + path?: string; fileName: string; dimensions: IMediaDataDimensions; } diff --git a/src/file/media/media.ts b/src/file/media/media.ts index 5ac8905bc7..7f5c960a55 100644 --- a/src/file/media/media.ts +++ b/src/file/media/media.ts @@ -11,23 +11,10 @@ export class Media { this.map = new Map(); } - 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); - + private createMedia(key: string, relationshipsCount, dimensions, data: fs.ReadStream | Buffer, filePath?: string, ) { const imageData = { referenceId: this.map.size + relationshipsCount + 1, - stream: fs.createReadStream(filePath), + stream: data, path: filePath, fileName: key, dimensions: { @@ -45,6 +32,36 @@ export class Media { 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[] { const array = new Array();