2022-06-26 23:26:42 +01:00
|
|
|
import { uniqueId } from "@util/convenience-functions";
|
2021-03-12 03:58:05 +00:00
|
|
|
|
2018-01-23 01:33:12 +00:00
|
|
|
import { IMediaData } from "./data";
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2021-03-18 02:48:37 +00:00
|
|
|
export interface IMediaTransformation {
|
2021-03-15 02:41:37 +00:00
|
|
|
readonly width: number;
|
|
|
|
readonly height: number;
|
|
|
|
readonly flip?: {
|
|
|
|
readonly vertical?: boolean;
|
|
|
|
readonly horizontal?: boolean;
|
|
|
|
};
|
|
|
|
readonly rotation?: number;
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:50:33 +00:00
|
|
|
export class Media {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line functional/prefer-readonly-type
|
2018-01-29 01:55:25 +00:00
|
|
|
private readonly map: Map<string, IMediaData>;
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2022-08-31 07:52:27 +01:00
|
|
|
public constructor() {
|
2017-12-30 21:18:55 +00:00
|
|
|
this.map = new Map<string, IMediaData>();
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 02:48:37 +00:00
|
|
|
public addMedia(data: Buffer | string | Uint8Array | ArrayBuffer, transformation: IMediaTransformation): IMediaData {
|
|
|
|
const key = `${uniqueId()}.png`;
|
2017-03-25 21:50:33 +00:00
|
|
|
|
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
|
|
|
fileName: key,
|
2021-03-15 02:41:37 +00:00
|
|
|
transformation: {
|
2018-05-06 03:19:36 +01:00
|
|
|
pixels: {
|
2021-03-15 02:41:37 +00:00
|
|
|
x: Math.round(transformation.width),
|
|
|
|
y: Math.round(transformation.height),
|
2018-05-06 03:19:36 +01:00
|
|
|
},
|
|
|
|
emus: {
|
2021-03-15 02:41:37 +00:00
|
|
|
x: Math.round(transformation.width * 9525),
|
|
|
|
y: Math.round(transformation.height * 9525),
|
2018-05-06 03:19:36 +01:00
|
|
|
},
|
2021-03-15 02:41:37 +00:00
|
|
|
flip: transformation.flip,
|
|
|
|
rotation: transformation.rotation ? transformation.rotation * 60000 : undefined,
|
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-03-18 02:48:37 +00:00
|
|
|
public addImage(key: string, mediaData: IMediaData): void {
|
|
|
|
this.map.set(key, mediaData);
|
|
|
|
}
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2021-03-18 02:48:37 +00:00
|
|
|
public get Array(): readonly IMediaData[] {
|
|
|
|
return Array.from(this.map.values());
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|
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 {
|
2022-09-15 20:00:50 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
|
2018-08-15 22:20:43 +01:00
|
|
|
const b = require("buf" + "fer");
|
|
|
|
return new b.Buffer(dataURI, "base64");
|
|
|
|
}
|
|
|
|
}
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|