Files
docx-js/src/file/media/media.ts

59 lines
1.4 KiB
TypeScript
Raw Normal View History

import * as fs from "fs";
2018-01-22 20:42:57 +00:00
import * as sizeOf from "image-size";
import * as path from "path";
2018-01-22 20:42:57 +00:00
2018-01-23 01:33:12 +00:00
import { IMediaData } from "./data";
export class Media {
private map: Map<string, IMediaData>;
constructor() {
this.map = new Map<string, IMediaData>();
}
public getMedia(key: string): IMediaData {
2017-03-29 22:57:52 +01:00
const data = this.map.get(key);
if (data === undefined) {
throw new Error(`Cannot find image with the key ${key}`);
}
return data;
}
2018-01-09 21:57:10 +00:00
public addMedia(filePath: string): IMediaData {
const key = path.basename(filePath);
2018-01-22 20:42:57 +00:00
const dimensions = sizeOf(filePath);
2018-01-09 21:57:10 +00:00
const imageData = {
2018-01-22 22:05:20 +00:00
referenceId: this.map.size + 3,
stream: fs.createReadStream(filePath),
path: filePath,
2018-01-09 21:57:10 +00:00
fileName: key,
2018-01-22 20:42:57 +00:00
dimensions: {
pixels: {
x: dimensions.width,
y: dimensions.height,
},
emus: {
x: dimensions.width * 9525,
y: dimensions.height * 9525,
},
},
2018-01-09 21:57:10 +00:00
};
this.map.set(key, imageData);
return imageData;
}
public get array(): IMediaData[] {
const array = new Array<IMediaData>();
this.map.forEach((data) => {
array.push(data);
});
return array;
}
}