export class Attachment { //Required private name: string; private src: string; private key: string; //Optional private size: number = 0; constructor(name: string, src: string) { this.src = src; this.name = name + this.setExtension(); this.key = this.setKey(); } setSize(size: number): Attachment { this.size = size; return this; } get Src(): string { return this.src; } get Key(): string { return this.key; } get Name(): string { return this.name; } get Size(): number { return this.size; } private setExtension() { const len = this.src.length; let ext: string = ''; for (var i = len - 1; i >= 0; i--) { ext += this.src[i] if (this.src[i] == '.') break; } return ext.split('').reverse().join('') } private setKey(): string { const chars = '0123456789' let _key: string = '' for (var i = 1; i <= 10; i++) { _key += chars.charAt(Math.floor(Math.random() * 10)) } _key += ('_' + this.name); return _key; } toJSON(): any { return { attachment_name: this.name, attachment_key: this.key, size: this.size } } }