Images: Extend API for working with images (#5)
* extend creating image using buffer and dimensions from outside * remove empty space
This commit is contained in:
@ -11,23 +11,10 @@ export class Media {
|
||||
this.map = new Map<string, IMediaData>();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public addMedia(filePath: string, relationshipsCount: number): IMediaData {
|
||||
const key = path.basename(filePath);
|
||||
const dimensions = sizeOf(filePath);
|
||||
|
||||
private createMedia(key: string, relationshipsCount, dimensions, data: fs.ReadStream | Buffer, filePath?: string, ) {
|
||||
const imageData = {
|
||||
referenceId: this.map.size + relationshipsCount + 1,
|
||||
stream: fs.createReadStream(filePath),
|
||||
stream: data,
|
||||
path: filePath,
|
||||
fileName: key,
|
||||
dimensions: {
|
||||
@ -45,6 +32,36 @@ export class Media {
|
||||
|
||||
return imageData;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
public addMedia(filePath: string, relationshipsCount: number): IMediaData {
|
||||
const key = path.basename(filePath);
|
||||
const dimensions = sizeOf(filePath);
|
||||
return this.createMedia(key, relationshipsCount, dimensions, fs.createReadStream(filePath), filePath);
|
||||
}
|
||||
|
||||
public addMediaWithData(fileName: string, data: Buffer, relationshipsCount: number, width?, height?): IMediaData {
|
||||
const key = fileName;
|
||||
let dimensions;
|
||||
if (width && height) {
|
||||
dimensions = {
|
||||
width: width,
|
||||
height: height
|
||||
}
|
||||
} else {
|
||||
dimensions = sizeOf(data);
|
||||
}
|
||||
|
||||
return this.createMedia(key, relationshipsCount, dimensions, data);
|
||||
}
|
||||
|
||||
public get array(): IMediaData[] {
|
||||
const array = new Array<IMediaData>();
|
||||
|
Reference in New Issue
Block a user