2021-03-12 03:58:05 +00:00
|
|
|
import { uniqueId } from "convenience-functions";
|
|
|
|
|
2018-08-14 11:06:17 -03:00
|
|
|
import { IDrawingOptions } from "../drawing";
|
2018-08-14 11:28:01 -03:00
|
|
|
import { File } from "../file";
|
2019-06-23 22:36:01 +01:00
|
|
|
import { PictureRun } from "../paragraph";
|
2018-01-23 01:33:12 +00:00
|
|
|
import { IMediaData } from "./data";
|
2019-06-23 22:36:01 +01:00
|
|
|
// import { Image } from "./image";
|
2017-03-25 21:50:33 +00:00
|
|
|
|
|
|
|
export class Media {
|
2018-08-22 10:39:13 -03:00
|
|
|
public static addImage(
|
2021-02-28 16:04:21 +00:00
|
|
|
file: File,
|
2018-08-22 10:39:13 -03:00
|
|
|
buffer: Buffer | string | Uint8Array | ArrayBuffer,
|
|
|
|
width?: number,
|
|
|
|
height?: number,
|
|
|
|
drawingOptions?: IDrawingOptions,
|
2019-06-23 22:36:01 +01:00
|
|
|
): PictureRun {
|
2018-08-02 02:09:00 +01:00
|
|
|
// Workaround to expose id without exposing to API
|
2018-12-24 16:50:53 +00:00
|
|
|
const mediaData = file.Media.addMedia(buffer, width, height);
|
2019-06-23 22:36:01 +01:00
|
|
|
return new PictureRun(mediaData, drawingOptions);
|
2018-08-02 02:09:00 +01:00
|
|
|
}
|
|
|
|
|
2018-01-29 01:55:25 +00:00
|
|
|
private readonly map: Map<string, IMediaData>;
|
2017-03-25 21:50:33 +00:00
|
|
|
|
|
|
|
constructor() {
|
2017-12-30 21:18:55 +00:00
|
|
|
this.map = new Map<string, IMediaData>();
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 15:59:06 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-12-24 16:50:53 +00:00
|
|
|
public addMedia(buffer: Buffer | string | Uint8Array | ArrayBuffer, width: number = 100, height: number = 100): IMediaData {
|
2021-03-12 03:58:05 +00:00
|
|
|
const key = `${uniqueId()}.png`;
|
2018-04-20 15:59:06 +02:00
|
|
|
|
2018-08-15 22:20:43 +01:00
|
|
|
return this.createMedia(
|
|
|
|
key,
|
|
|
|
{
|
2018-04-20 15:59:06 +02:00
|
|
|
width: width,
|
2018-05-06 03:19:36 +01:00
|
|
|
height: height,
|
2018-08-15 22:20:43 +01:00
|
|
|
},
|
|
|
|
buffer,
|
|
|
|
);
|
2018-04-20 15:59:06 +02:00
|
|
|
}
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2018-05-06 03:19:36 +01:00
|
|
|
private createMedia(
|
|
|
|
key: string,
|
2018-11-02 02:51:57 +00:00
|
|
|
dimensions: { readonly width: number; readonly height: number },
|
2018-08-15 22:20:43 +01:00
|
|
|
data: Buffer | string | Uint8Array | ArrayBuffer,
|
2018-05-06 03:19:36 +01:00
|
|
|
filePath?: string,
|
|
|
|
): IMediaData {
|
2018-11-02 02:51:57 +00:00
|
|
|
const newData = typeof data === "string" ? this.convertDataURIToBinary(data) : data;
|
2018-08-15 22:20:43 +01:00
|
|
|
|
2018-11-02 02:51:57 +00:00
|
|
|
const imageData: IMediaData = {
|
|
|
|
stream: newData,
|
2018-05-06 03:19:36 +01:00
|
|
|
path: filePath,
|
|
|
|
fileName: key,
|
|
|
|
dimensions: {
|
|
|
|
pixels: {
|
2018-08-09 23:22:03 +01:00
|
|
|
x: Math.round(dimensions.width),
|
|
|
|
y: Math.round(dimensions.height),
|
2018-05-06 03:19:36 +01:00
|
|
|
},
|
|
|
|
emus: {
|
2018-08-09 23:22:03 +01:00
|
|
|
x: Math.round(dimensions.width * 9525),
|
|
|
|
y: Math.round(dimensions.height * 9525),
|
2018-05-06 03:19:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-08-02 02:09:00 +01:00
|
|
|
|
2018-05-06 03:19:36 +01:00
|
|
|
this.map.set(key, imageData);
|
|
|
|
|
|
|
|
return imageData;
|
|
|
|
}
|
|
|
|
|
2021-02-27 19:23:29 +00:00
|
|
|
public get Array(): readonly IMediaData[] {
|
2017-12-30 21:18:55 +00:00
|
|
|
const array = new Array<IMediaData>();
|
2017-03-25 21:50:33 +00:00
|
|
|
|
|
|
|
this.map.forEach((data) => {
|
|
|
|
array.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
2018-08-15 22:20:43 +01:00
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|