4c13601476dd9e3a1e10dad0acfba15e582bcc81
[motion2.git] / server / api / attachment / attachment.controller.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf633=['omit','createdAt','body','updatedAt','create','file','originalname','basename','type','download','join','root','server/files/attachments/','offline-chat/','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','path','to-csv','ejs','fs-extra','lodash','squel','crypto','querystring','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','sendStatus','end','status','json','offset','undefined','limit','set','Content-Range','apply','reject','save','update','then','destroy','stack','name','send','index','Attachment','rawAttributes','model','map','filters','query','intersection','fields','attributes','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','count','include','findAll','rows','catch','keys','includeAll','clone','params','find'];(function(_0x4a4b52,_0x3e5344){var _0x1ffa82=function(_0x2daadb){while(--_0x2daadb){_0x4a4b52['push'](_0x4a4b52['shift']());}};_0x1ffa82(++_0x3e5344);}(_0xf633,0x107));var _0x3f63=function(_0x13d462,_0x31ab91){_0x13d462=_0x13d462-0x0;var _0x2cc2a8=_0xf633[_0x13d462];return _0x2cc2a8;};'use strict';var emlformat=require(_0x3f63('0x0'));var rimraf=require(_0x3f63('0x1'));var zipdir=require(_0x3f63('0x2'));var jsonpatch=require(_0x3f63('0x3'));var rp=require(_0x3f63('0x4'));var moment=require(_0x3f63('0x5'));var BPromise=require(_0x3f63('0x6'));var Mustache=require('mustache');var util=require('util');var path=require(_0x3f63('0x7'));var sox=require('sox');var csv=require(_0x3f63('0x8'));var ejs=require(_0x3f63('0x9'));var fs=require('fs');var fs_extra=require(_0x3f63('0xa'));var _=require(_0x3f63('0xb'));var squel=require(_0x3f63('0xc'));var crypto=require(_0x3f63('0xd'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x3f63('0xe'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x3f63('0xf'));var logger=require(_0x3f63('0x10'))(_0x3f63('0x11'));var utils=require('../../config/utils');var config=require(_0x3f63('0x12'));var licenseUtil=require(_0x3f63('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x1e661b,_0x2df05f){_0x2df05f=_0x2df05f||0xcc;return function(_0x24d1e2){if(_0x24d1e2){return _0x1e661b[_0x3f63('0x14')](_0x2df05f);}return _0x1e661b['status'](_0x2df05f)[_0x3f63('0x15')]();};}function respondWithResult(_0x4d0c52,_0x1f6181){_0x1f6181=_0x1f6181||0xc8;return function(_0xe13746){if(_0xe13746){return _0x4d0c52[_0x3f63('0x16')](_0x1f6181)[_0x3f63('0x17')](_0xe13746);}};}function respondWithFilteredResult(_0x47db77,_0x426ec4){return function(_0x6d00ca){if(_0x6d00ca){var _0x2e1f09=typeof _0x426ec4[_0x3f63('0x18')]===_0x3f63('0x19')&&typeof _0x426ec4[_0x3f63('0x1a')]===_0x3f63('0x19');var _0x4cc258=_0x6d00ca['count'];var _0x149609=_0x2e1f09?0x0:_0x426ec4[_0x3f63('0x18')];var _0xdee62c=_0x2e1f09?_0x6d00ca['count']:_0x426ec4[_0x3f63('0x18')]+_0x426ec4[_0x3f63('0x1a')];var _0x4edb17;if(_0xdee62c>=_0x4cc258){_0xdee62c=_0x4cc258;_0x4edb17=0xc8;}else{_0x4edb17=0xce;}_0x47db77[_0x3f63('0x16')](_0x4edb17);return _0x47db77[_0x3f63('0x1b')](_0x3f63('0x1c'),_0x149609+'-'+_0xdee62c+'/'+_0x4cc258)[_0x3f63('0x17')](_0x6d00ca);}return null;};}function patchUpdates(_0x2eee68){return function(_0x40a887){try{jsonpatch[_0x3f63('0x1d')](_0x40a887,_0x2eee68,!![]);}catch(_0x48df5f){return BPromise[_0x3f63('0x1e')](_0x48df5f);}return _0x40a887[_0x3f63('0x1f')]();};}function saveUpdates(_0x1e5026,_0x2832a2){return function(_0x288143){if(_0x288143){return _0x288143[_0x3f63('0x20')](_0x1e5026)[_0x3f63('0x21')](function(_0x352fe6){return _0x352fe6;});}return null;};}function removeEntity(_0xed04ea,_0x7d8154){return function(_0x5664ce){if(_0x5664ce){return _0x5664ce[_0x3f63('0x22')]()[_0x3f63('0x21')](function(){_0xed04ea[_0x3f63('0x16')](0xcc)[_0x3f63('0x15')]();});}};}function handleEntityNotFound(_0x1e2e3a,_0x430f7b){return function(_0x3860de){if(!_0x3860de){_0x1e2e3a[_0x3f63('0x14')](0x194);}return _0x3860de;};}function handleError(_0x430828,_0x476b17){_0x476b17=_0x476b17||0x1f4;return function(_0x2ee2d7){logger['error'](_0x2ee2d7[_0x3f63('0x23')]);if(_0x2ee2d7['name']){delete _0x2ee2d7[_0x3f63('0x24')];}_0x430828[_0x3f63('0x16')](_0x476b17)[_0x3f63('0x25')](_0x2ee2d7);};}exports[_0x3f63('0x26')]=function(_0x307681,_0xb6d352){var _0x5f35e5={},_0x4384a8={},_0x520608={'count':0x0,'rows':[]};var _0x5cf785=_['map'](db[_0x3f63('0x27')][_0x3f63('0x28')],function(_0x4fd57b){return{'name':_0x4fd57b['fieldName'],'type':_0x4fd57b['type']['key']};});_0x4384a8[_0x3f63('0x29')]=_[_0x3f63('0x2a')](_0x5cf785,_0x3f63('0x24'));_0x4384a8['query']=_['keys'](_0x307681['query']);_0x4384a8[_0x3f63('0x2b')]=_['intersection'](_0x4384a8[_0x3f63('0x29')],_0x4384a8[_0x3f63('0x2c')]);_0x5f35e5['attributes']=_[_0x3f63('0x2d')](_0x4384a8[_0x3f63('0x29')],qs[_0x3f63('0x2e')](_0x307681[_0x3f63('0x2c')]['fields']));_0x5f35e5[_0x3f63('0x2f')]=_0x5f35e5['attributes'][_0x3f63('0x30')]?_0x5f35e5[_0x3f63('0x2f')]:_0x4384a8[_0x3f63('0x29')];if(!_0x307681['query'][_0x3f63('0x31')](_0x3f63('0x32'))){_0x5f35e5['limit']=qs['limit'](_0x307681['query'][_0x3f63('0x1a')]);_0x5f35e5['offset']=qs[_0x3f63('0x18')](_0x307681[_0x3f63('0x2c')][_0x3f63('0x18')]);}_0x5f35e5[_0x3f63('0x33')]=qs[_0x3f63('0x34')](_0x307681[_0x3f63('0x2c')][_0x3f63('0x34')]);_0x5f35e5[_0x3f63('0x35')]=qs['filters'](_[_0x3f63('0x36')](_0x307681['query'],_0x4384a8[_0x3f63('0x2b')]),_0x5cf785);if(_0x307681[_0x3f63('0x2c')][_0x3f63('0x37')]){_0x5f35e5[_0x3f63('0x35')]=_[_0x3f63('0x38')](_0x5f35e5[_0x3f63('0x35')],{'$or':_[_0x3f63('0x2a')](_0x5cf785,function(_0x579075){if(_0x579075['type']!==_0x3f63('0x39')){var _0x457c44={};_0x457c44[_0x579075[_0x3f63('0x24')]]={'$like':'%'+_0x307681[_0x3f63('0x2c')][_0x3f63('0x37')]+'%'};return _0x457c44;}})});}_0x5f35e5=_[_0x3f63('0x38')]({},_0x5f35e5,_0x307681[_0x3f63('0x3a')]);var _0x2cdbac={'where':_0x5f35e5[_0x3f63('0x35')]};return db['Attachment'][_0x3f63('0x3b')](_0x2cdbac)[_0x3f63('0x21')](function(_0x214845){_0x520608[_0x3f63('0x3b')]=_0x214845;if(_0x307681[_0x3f63('0x2c')]['includeAll']){_0x5f35e5[_0x3f63('0x3c')]=[{'all':!![]}];}return db['Attachment'][_0x3f63('0x3d')](_0x5f35e5);})['then'](function(_0x1f1fa3){_0x520608[_0x3f63('0x3e')]=_0x1f1fa3;return _0x520608;})['then'](respondWithFilteredResult(_0xb6d352,_0x5f35e5))[_0x3f63('0x3f')](handleError(_0xb6d352,null));};exports['show']=function(_0x8caf3b,_0x19dce9){var _0x59d137={'raw':!![],'where':{'id':_0x8caf3b['params']['id']}},_0x255d6e={};_0x255d6e[_0x3f63('0x29')]=_[_0x3f63('0x40')](db['Attachment']['rawAttributes']);_0x255d6e['query']=_[_0x3f63('0x40')](_0x8caf3b[_0x3f63('0x2c')]);_0x255d6e['filters']=_[_0x3f63('0x2d')](_0x255d6e[_0x3f63('0x29')],_0x255d6e['query']);_0x59d137['attributes']=_['intersection'](_0x255d6e['model'],qs[_0x3f63('0x2e')](_0x8caf3b[_0x3f63('0x2c')][_0x3f63('0x2e')]));_0x59d137[_0x3f63('0x2f')]=_0x59d137[_0x3f63('0x2f')]['length']?_0x59d137[_0x3f63('0x2f')]:_0x255d6e[_0x3f63('0x29')];if(_0x8caf3b[_0x3f63('0x2c')][_0x3f63('0x41')]){_0x59d137[_0x3f63('0x3c')]=[{'all':!![]}];}_0x59d137=_[_0x3f63('0x38')]({},_0x59d137,_0x8caf3b[_0x3f63('0x3a')]);return db[_0x3f63('0x27')]['find'](_0x59d137)[_0x3f63('0x21')](handleEntityNotFound(_0x19dce9,null))['then'](respondWithResult(_0x19dce9,null))['catch'](handleError(_0x19dce9,null));};exports[_0x3f63('0x42')]=function(_0x1b1371,_0xe685eb){var _0x4cc211={'where':{'id':_0x1b1371[_0x3f63('0x43')]['id']}},_0x20980e={};_0x20980e[_0x3f63('0x29')]=_[_0x3f63('0x40')](db[_0x3f63('0x27')]['rawAttributes']);_0x4cc211[_0x3f63('0x2f')]=_[_0x3f63('0x2d')](_0x20980e[_0x3f63('0x29')],qs[_0x3f63('0x2e')](_0x1b1371[_0x3f63('0x2c')]['fields']));_0x4cc211['attributes']=_0x4cc211[_0x3f63('0x2f')]['length']?_0x4cc211[_0x3f63('0x2f')]:_0x20980e[_0x3f63('0x29')];if(_0x1b1371[_0x3f63('0x2c')]['includeAll']){_0x4cc211[_0x3f63('0x3c')]=[{'all':!![]}];}_0x4cc211=_['merge']({},_0x4cc211,_0x1b1371[_0x3f63('0x3a')]);return db[_0x3f63('0x27')][_0x3f63('0x44')](_0x4cc211)[_0x3f63('0x21')](handleEntityNotFound(_0xe685eb,null))['then'](function(_0x1b6d23){if(_0x1b6d23){var _0x543bae=_0x1b6d23['get']({'plain':!![]});_0x543bae=qs[_0x3f63('0x45')](_0x543bae,['id',_0x3f63('0x46'),'updatedAt']);_0x1b1371[_0x3f63('0x47')]=_[_0x3f63('0x45')](_0x1b1371[_0x3f63('0x47')],['id',_0x3f63('0x46'),_0x3f63('0x48')]);return db[_0x3f63('0x27')][_0x3f63('0x49')](_[_0x3f63('0x38')](_0x543bae,_0x1b1371[_0x3f63('0x47')]),{'include':_0x1b1371[_0x3f63('0x2c')][_0x3f63('0x41')]?[{'all':!![]}]:undefined});}})['then'](respondWithResult(_0xe685eb,0xc9))[_0x3f63('0x3f')](handleError(_0xe685eb,null));};exports[_0x3f63('0x20')]=function(_0x2a7fe2,_0x48409e){if(_0x2a7fe2['body']['id']){delete _0x2a7fe2[_0x3f63('0x47')]['id'];}return db[_0x3f63('0x27')][_0x3f63('0x44')]({'where':{'id':_0x2a7fe2[_0x3f63('0x43')]['id']}})[_0x3f63('0x21')](handleEntityNotFound(_0x48409e,null))[_0x3f63('0x21')](saveUpdates(_0x2a7fe2[_0x3f63('0x47')],null))[_0x3f63('0x21')](respondWithResult(_0x48409e,null))[_0x3f63('0x3f')](handleError(_0x48409e,null));};exports[_0x3f63('0x22')]=function(_0x3337e3,_0x43e624){return db[_0x3f63('0x27')]['find']({'where':{'id':_0x3337e3[_0x3f63('0x43')]['id']}})[_0x3f63('0x21')](handleEntityNotFound(_0x43e624,null))[_0x3f63('0x21')](removeEntity(_0x43e624,null))[_0x3f63('0x3f')](handleError(_0x43e624,null));};exports['create']=function(_0x10853d,_0x4ebdae,_0x2cd3fc){_0x10853d['body']['name']=_0x10853d[_0x3f63('0x4a')][_0x3f63('0x4b')];_0x10853d[_0x3f63('0x47')][_0x3f63('0x4c')]=_0x10853d[_0x3f63('0x4a')]['filename'];_0x10853d[_0x3f63('0x47')][_0x3f63('0x4d')]=_0x10853d[_0x3f63('0x4a')]['mimetype'];return db[_0x3f63('0x27')][_0x3f63('0x49')](_0x10853d[_0x3f63('0x47')])[_0x3f63('0x21')](respondWithResult(_0x4ebdae,0xc9))['catch'](handleError(_0x4ebdae,null));};exports[_0x3f63('0x4e')]=function(_0x342bb2,_0x358780,_0x231904){return db[_0x3f63('0x27')]['findOne']({'where':{'id':_0x342bb2[_0x3f63('0x43')]['id']},'raw':!![]})[_0x3f63('0x21')](handleEntityNotFound(_0x358780,null))[_0x3f63('0x21')](function(_0x3a76dd){if(_0x3a76dd){var _0x1c0540=_0x3a76dd['ChatOfflineMessageId']!==null;var _0x294b80=path[_0x3f63('0x4f')](config[_0x3f63('0x50')],_0x3f63('0x51'));if(_0x1c0540){_0x294b80=path[_0x3f63('0x4f')](_0x294b80,_0x3f63('0x52'));}var _0x8e9432=path['join'](_0x294b80,_0x3a76dd[_0x3f63('0x4c')]);return _0x358780[_0x3f63('0x4e')](_0x8e9432);}})[_0x3f63('0x3f')](handleError(_0x358780,null));};