SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/common/vo/attachment-vo.js

70 lines
2.7 KiB
JavaScript

"use strict";
/**
* Value object for attachment.
*
* @author Alex Hnatkovskyy
* @constructor
*/
function AttachmentVO() {
// simple fields
this.name = '';
this.modifiedDate = '';
this.size = 0;
this.type = '';
this.contentType = '';
this.thumbnail = '';
this.attachmentReference = '';
}
AttachmentVO.prototype = new BaseVO();
AttachmentVO.prototype.constructor = AttachmentVO;
/**
* @override
* @return {Array} properties
*/
AttachmentVO.prototype.getProps = function () {
return BaseVO.prototype.getProps().concat('name', 'modifiedDate', 'fileContentType', 'fileName', 'fileSize', 'size', 'type', 'contentType', 'thumbnail', 'attachmentReference');
};
AttachmentVO.prototype.getFileGenericIconClass = function (name) {
var fileExt = name ? name.split('.').pop() : "", documentExts = ['xls', 'xlsx', 'doc', 'docx', 'pdf', 'csv', 'rtf', 'odt', 'ods'], imageExts = ['jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff', 'bmp'], iconClassName = 'icon-paperclip_square';
if (documentExts.indexOf(fileExt) >= 0) {
iconClassName = 'icon-' + fileExt + '_square';
}
else if (imageExts.indexOf(fileExt) >= 0) {
iconClassName = 'icon-image_square';
}
return iconClassName;
};
AttachmentVO.prototype.base64Validate = new RegExp('^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$');
AttachmentVO.prototype.normalizeThumbnail = function (thumbString) {
var thumbnailSource = thumbString.split(',').length > 1 ? thumbString.split(',') : thumbString.split(';'), imageBase64 = thumbnailSource.pop(), mimeType = thumbnailSource[0] || '';
if (this.base64Validate.test(imageBase64) && (mimeType.indexOf('base64') !== -1 && mimeType.indexOf('image/') !== -1)) {
return thumbString;
}
else if (this.base64Validate.test(imageBase64) && mimeType.indexOf('base64') === -1) {
var filters = {
jpeg: '/9j/4',
gif: 'R0lGOD',
png: 'iVBORw'
}, imageType;
_.each(filters, function (value, ext) {
if (imageBase64.indexOf(value) === 0) {
imageType = ext;
}
});
return imageType ? 'data:image/' + (imageType || 'png') + ';base64,' + imageBase64 : '';
}
else {
return '';
}
};
AttachmentVO.prototype.postBuild = function () {
this.name = this.name || this.fileName;
this.contentType = this.contentType || this.fileContentType || (this.name ? this.name.split('.').pop() : "");
this.size = this.size || this.fileSize;
this.thumbnail = this.normalizeThumbnail(this.thumbnail);
if (!this.thumbnail) {
this.fileGenericIconClass = this.getFileGenericIconClass(this.name);
this.badThumbSource = true;
}
};