2017-03-25 21:50:33 +00:00
|
|
|
import * as fs from "fs";
|
2018-01-22 20:42:57 +00:00
|
|
|
import * as sizeOf from "image-size";
|
2017-03-25 21:50:33 +00:00
|
|
|
import * as path from "path";
|
2018-01-22 20:42:57 +00:00
|
|
|
|
2018-08-02 02:09:00 +01:00
|
|
|
import { File } from "../file";
|
2018-01-23 01:33:12 +00:00
|
|
|
import { IMediaData } from "./data";
|
2017-03-25 21:50:33 +00:00
|
|
|
|
2018-08-02 02:09:00 +01:00
|
|
|
interface IHackedFile {
|
|
|
|
currentRelationshipId: number;
|
|
|
|
}
|
|
|
|
|
2017-03-25 21:50:33 +00:00
|
|
|
export class Media {
|
2018-08-02 02:09:00 +01:00
|
|
|
public static addImage(file: File, filePath: string): IMediaData {
|
|
|
|
// 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 mediaData;
|
|
|
|
}
|
|
|
|
|
2018-08-02 02:14:57 +01:00
|
|
|
public static addImageFromBuffer(file: File, buffer: Buffer, width?: number, height?: number): IMediaData {
|
2018-08-02 02:09:00 +01:00
|
|
|
// Workaround to expose id without exposing to API
|
|
|
|
const exposedFile = (file as {}) as IHackedFile;
|
2018-08-02 02:14:57 +01:00
|
|
|
const mediaData = file.Media.addMediaFromBuffer(`${Media.generateId()}.png`, buffer, exposedFile.currentRelationshipId++, width, height);
|
2018-08-02 02:09:00 +01:00
|
|
|
file.DocumentRelationships.createRelationship(
|
|
|
|
mediaData.referenceId,
|
|
|
|
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image",
|
|
|
|
`media/${mediaData.fileName}`,
|
|
|
|
);
|
|
|
|
return mediaData;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static generateId(): string {
|
|
|
|
// https://gist.github.com/6174/6062387
|
|
|
|
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
|
|
}
|
|
|
|
|
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-06-21 12:03:34 +02:00
|
|
|
public addMedia(filePath: string, referenceId: number): IMediaData {
|
2018-04-20 15:59:06 +02:00
|
|
|
const key = path.basename(filePath);
|
|
|
|
const dimensions = sizeOf(filePath);
|
2018-06-21 12:03:34 +02:00
|
|
|
return this.createMedia(key, referenceId, dimensions, fs.createReadStream(filePath), filePath);
|
2018-04-20 15:59:06 +02:00
|
|
|
}
|
|
|
|
|
2018-08-02 02:14:57 +01:00
|
|
|
public addMediaFromBuffer(fileName: string, buffer: Buffer, referenceId: number, width?: number, height?: number): IMediaData {
|
2018-04-20 15:59:06 +02:00
|
|
|
const key = fileName;
|
|
|
|
let dimensions;
|
|
|
|
if (width && height) {
|
|
|
|
dimensions = {
|
|
|
|
width: width,
|
2018-05-06 03:19:36 +01:00
|
|
|
height: height,
|
|
|
|
};
|
2018-04-20 15:59:06 +02:00
|
|
|
} else {
|
2018-08-02 02:14:57 +01:00
|
|
|
dimensions = sizeOf(buffer);
|
2018-04-20 15:59:06 +02:00
|
|
|
}
|
2018-05-06 03:19:36 +01:00
|
|
|
|
2018-08-02 02:14:57 +01:00
|
|
|
return this.createMedia(key, referenceId, dimensions, 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,
|
|
|
|
relationshipsCount: number,
|
|
|
|
dimensions: { width: number; height: number },
|
|
|
|
data: fs.ReadStream | Buffer,
|
|
|
|
filePath?: string,
|
|
|
|
): IMediaData {
|
|
|
|
const imageData = {
|
|
|
|
referenceId: this.map.size + relationshipsCount + 1,
|
|
|
|
stream: data,
|
|
|
|
path: filePath,
|
|
|
|
fileName: key,
|
|
|
|
dimensions: {
|
|
|
|
pixels: {
|
|
|
|
x: dimensions.width,
|
|
|
|
y: dimensions.height,
|
|
|
|
},
|
|
|
|
emus: {
|
|
|
|
x: dimensions.width * 9525,
|
|
|
|
y: dimensions.height * 9525,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-08-02 02:09:00 +01:00
|
|
|
|
2018-05-06 03:19:36 +01:00
|
|
|
this.map.set(key, imageData);
|
|
|
|
|
|
|
|
return imageData;
|
|
|
|
}
|
|
|
|
|
2017-12-30 21:18:55 +00:00
|
|
|
public get array(): IMediaData[] {
|
|
|
|
const array = new Array<IMediaData>();
|
2017-03-25 21:50:33 +00:00
|
|
|
|
|
|
|
this.map.forEach((data) => {
|
|
|
|
array.push(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
}
|