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
|
|
|
|
|
|
|
import {IMediaData} from "./data";
|
2017-03-25 21:50:33 +00:00
|
|
|
|
|
|
|
export class Media {
|
2017-12-30 21:18:55 +00:00
|
|
|
private 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
|
|
|
}
|
|
|
|
|
2017-12-30 21:18:55 +00:00
|
|
|
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;
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2017-03-25 21:50:33 +00:00
|
|
|
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;
|
2017-03-25 21:50:33 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|