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

45 lines
1.0 KiB
TypeScript
Raw Normal View History

import * as fs from "fs";
import * as path from "path";
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);
const imageData = {
2018-01-10 00:35:26 +00:00
referenceId: this.map.values.length + 3,
stream: fs.createReadStream(filePath),
path: filePath,
2018-01-09 21:57:10 +00:00
fileName: key,
};
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;
}
}