Make add image support base64 strings

This commit is contained in:
Dolan
2018-08-15 22:20:43 +01:00
parent f969c866a7
commit bf5bcea607
3 changed files with 41 additions and 16 deletions

View File

@ -129,7 +129,7 @@ export class File {
return this; return this;
} }
public createImage(buffer: Buffer, width?: number, height?: number): Image { public createImage(buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
const image = Media.addImage(this, buffer, width, height); const image = Media.addImage(this, buffer, width, height);
this.document.addParagraph(image.Paragraph); this.document.addParagraph(image.Paragraph);

View File

@ -11,7 +11,7 @@ export interface IMediaDataDimensions {
export interface IMediaData { export interface IMediaData {
referenceId: number; referenceId: number;
stream: Buffer; stream: Buffer | Uint8Array | ArrayBuffer;
path?: string; path?: string;
fileName: string; fileName: string;
dimensions: IMediaDataDimensions; dimensions: IMediaDataDimensions;

View File

@ -1,5 +1,3 @@
import * as sizeOf from "image-size";
import { File } from "../file"; import { File } from "../file";
import { ImageParagraph } from "../paragraph"; import { ImageParagraph } from "../paragraph";
import { IMediaData } from "./data"; import { IMediaData } from "./data";
@ -10,7 +8,7 @@ interface IHackedFile {
} }
export class Media { export class Media {
public static addImage(file: File, buffer: Buffer, width?: number, height?: number): Image { public static addImage(file: File, buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number): Image {
// Workaround to expose id without exposing to API // Workaround to expose id without exposing to API
const exposedFile = (file as {}) as IHackedFile; const exposedFile = (file as {}) as IHackedFile;
const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height); const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height);
@ -51,28 +49,36 @@ export class Media {
return data; return data;
} }
public addMedia(buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData { public addMedia(
buffer: Buffer | string | Uint8Array | ArrayBuffer,
referenceId: number,
width: number = 100,
height: number = 100,
): IMediaData {
const key = `${Media.generateId()}.png`; const key = `${Media.generateId()}.png`;
let dimensions;
if (width && height) { return this.createMedia(
dimensions = { key,
referenceId,
{
width: width, width: width,
height: height, height: height,
}; },
} else { buffer,
dimensions = sizeOf(buffer); );
}
return this.createMedia(key, referenceId, dimensions, buffer);
} }
private createMedia( private createMedia(
key: string, key: string,
relationshipsCount: number, relationshipsCount: number,
dimensions: { width: number; height: number }, dimensions: { width: number; height: number },
data: Buffer, data: Buffer | string | Uint8Array | ArrayBuffer,
filePath?: string, filePath?: string,
): IMediaData { ): IMediaData {
if (typeof data === "string") {
data = this.convertDataURIToBinary(data);
}
const imageData = { const imageData = {
referenceId: this.map.size + relationshipsCount + 1, referenceId: this.map.size + relationshipsCount + 1,
stream: data, stream: data,
@ -104,4 +110,23 @@ export class Media {
return array; return array;
} }
private convertDataURIToBinary(dataURI: string): Uint8Array {
// https://gist.github.com/borismus/1032746
// https://github.com/mafintosh/base64-to-uint8array
const BASE64_MARKER = ";base64,";
const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
if (typeof atob === "function") {
return new Uint8Array(
atob(dataURI.substring(base64Index))
.split("")
.map((c) => c.charCodeAt(0)),
);
} else {
const b = require("buf" + "fer");
return new b.Buffer(dataURI, "base64");
}
}
} }