Merge branch 'master' into master
This commit is contained in:
@ -1,7 +1,3 @@
|
||||
import * as fs from "fs";
|
||||
import * as sizeOf from "image-size";
|
||||
import * as path from "path";
|
||||
|
||||
import { IDrawingOptions } from "../drawing";
|
||||
import { File } from "../file";
|
||||
import { ImageParagraph } from "../paragraph";
|
||||
@ -13,28 +9,11 @@ interface IHackedFile {
|
||||
}
|
||||
|
||||
export class Media {
|
||||
public static addImage(file: File, filePath: string, drawingOptions?: IDrawingOptions): Image {
|
||||
// Workaround to expose id without exposing to API
|
||||
const exposedFile = (file as {}) as IHackedFile;
|
||||
const mediaData = file.Media.addMedia(filePath, exposedFile.currentRelationshipId++);
|
||||
file.DocumentRelationships.createRelationship(
|
||||
mediaData.referenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
`media/${mediaData.fileName}`,
|
||||
);
|
||||
return new Image(new ImageParagraph(mediaData, drawingOptions));
|
||||
}
|
||||
|
||||
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number, drawingOptions?: IDrawingOptions): Image {
|
||||
public static addImage(file: File, buffer: Buffer | string | Uint8Array | ArrayBuffer, width?: number, height?: number, drawingOptions?: IDrawingOptions): Image {
|
||||
// Workaround to expose id without exposing to API
|
||||
const exposedFile = (file as {}) as IHackedFile;
|
||||
const mediaData = file.Media.addMediaFromBuffer(
|
||||
`${Media.generateId()}.png`,
|
||||
buffer,
|
||||
exposedFile.currentRelationshipId++,
|
||||
width,
|
||||
height,
|
||||
);
|
||||
const mediaData = file.Media.addMedia(buffer, exposedFile.currentRelationshipId++, width, height);
|
||||
file.DocumentRelationships.createRelationship(
|
||||
mediaData.referenceId,
|
||||
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
||||
@ -72,34 +51,36 @@ export class Media {
|
||||
return data;
|
||||
}
|
||||
|
||||
public addMedia(filePath: string, referenceId: number): IMediaData {
|
||||
const key = path.basename(filePath);
|
||||
const dimensions = sizeOf(filePath);
|
||||
return this.createMedia(key, referenceId, dimensions, fs.createReadStream(filePath), filePath);
|
||||
}
|
||||
public addMedia(
|
||||
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
referenceId: number,
|
||||
width: number = 100,
|
||||
height: number = 100,
|
||||
): IMediaData {
|
||||
const key = `${Media.generateId()}.png`;
|
||||
|
||||
public addMediaFromBuffer(fileName: string, buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData {
|
||||
const key = fileName;
|
||||
let dimensions;
|
||||
if (width && height) {
|
||||
dimensions = {
|
||||
return this.createMedia(
|
||||
key,
|
||||
referenceId,
|
||||
{
|
||||
width: width,
|
||||
height: height,
|
||||
};
|
||||
} else {
|
||||
dimensions = sizeOf(buffer);
|
||||
}
|
||||
|
||||
return this.createMedia(key, referenceId, dimensions, buffer);
|
||||
},
|
||||
buffer,
|
||||
);
|
||||
}
|
||||
|
||||
private createMedia(
|
||||
key: string,
|
||||
relationshipsCount: number,
|
||||
dimensions: { width: number; height: number },
|
||||
data: fs.ReadStream | Buffer,
|
||||
data: Buffer | string | Uint8Array | ArrayBuffer,
|
||||
filePath?: string,
|
||||
): IMediaData {
|
||||
if (typeof data === "string") {
|
||||
data = this.convertDataURIToBinary(data);
|
||||
}
|
||||
|
||||
const imageData = {
|
||||
referenceId: this.map.size + relationshipsCount + 1,
|
||||
stream: data,
|
||||
@ -131,4 +112,23 @@ export class Media {
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user