a306de974b832dfa40d25d3eda8837e81309132f
[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 _0x6bf6=['sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','status','json','offset','undefined','count','set','Content-Range','save','update','then','error','name','send','index','rawAttributes','type','key','model','map','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','order','sort','pick','filter','where','merge','VIRTUAL','options','Attachment','includeAll','include','findAll','rows','catch','show','find','clone','get','omit','createdAt','updatedAt','body','params','destroy','create','file','originalname','basename','filename','mimetype','download','findOne','ChatOfflineMessageId','server/files/attachments/','join','offline-chat/','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path'];(function(_0x1b2ff7,_0x12a034){var _0x31656a=function(_0x1165a5){while(--_0x1165a5){_0x1b2ff7['push'](_0x1b2ff7['shift']());}};_0x31656a(++_0x12a034);}(_0x6bf6,0x1d2));var _0x66bf=function(_0x11c4b6,_0x572fdb){_0x11c4b6=_0x11c4b6-0x0;var _0x2cb405=_0x6bf6[_0x11c4b6];return _0x2cb405;};'use strict';var emlformat=require(_0x66bf('0x0'));var rimraf=require(_0x66bf('0x1'));var zipdir=require(_0x66bf('0x2'));var jsonpatch=require(_0x66bf('0x3'));var rp=require('request-promise');var moment=require(_0x66bf('0x4'));var BPromise=require(_0x66bf('0x5'));var Mustache=require(_0x66bf('0x6'));var util=require(_0x66bf('0x7'));var path=require(_0x66bf('0x8'));var sox=require(_0x66bf('0x9'));var csv=require('to-csv');var ejs=require(_0x66bf('0xa'));var fs=require('fs');var fs_extra=require(_0x66bf('0xb'));var _=require(_0x66bf('0xc'));var squel=require(_0x66bf('0xd'));var crypto=require(_0x66bf('0xe'));var jsforce=require(_0x66bf('0xf'));var deskjs=require(_0x66bf('0x10'));var toCsv=require(_0x66bf('0x11'));var querystring=require(_0x66bf('0x12'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require(_0x66bf('0x13'));var qs=require(_0x66bf('0x14'));var as=require(_0x66bf('0x15'));var hardwareService=require(_0x66bf('0x16'));var logger=require(_0x66bf('0x17'))(_0x66bf('0x18'));var utils=require(_0x66bf('0x19'));var config=require(_0x66bf('0x1a'));var licenseUtil=require('../../config/license/util');var db=require(_0x66bf('0x1b'))['db'];function respondWithStatusCode(_0x17f69d,_0x57b75a){_0x57b75a=_0x57b75a||0xcc;return function(_0x258c3d){if(_0x258c3d){return _0x17f69d[_0x66bf('0x1c')](_0x57b75a);}return _0x17f69d['status'](_0x57b75a)[_0x66bf('0x1d')]();};}function respondWithResult(_0x52f6ed,_0x5a46f4){_0x5a46f4=_0x5a46f4||0xc8;return function(_0x361403){if(_0x361403){return _0x52f6ed[_0x66bf('0x1e')](_0x5a46f4)[_0x66bf('0x1f')](_0x361403);}};}function respondWithFilteredResult(_0x2303ff,_0x2d5388){return function(_0x2077f0){if(_0x2077f0){var _0x4f5135=typeof _0x2d5388[_0x66bf('0x20')]===_0x66bf('0x21')&&typeof _0x2d5388['limit']===_0x66bf('0x21');var _0xaca9c9=_0x2077f0[_0x66bf('0x22')];var _0x277f79=_0x4f5135?0x0:_0x2d5388[_0x66bf('0x20')];var _0x1a0125=_0x4f5135?_0x2077f0[_0x66bf('0x22')]:_0x2d5388[_0x66bf('0x20')]+_0x2d5388['limit'];var _0x31d19c;if(_0x1a0125>=_0xaca9c9){_0x1a0125=_0xaca9c9;_0x31d19c=0xc8;}else{_0x31d19c=0xce;}_0x2303ff['status'](_0x31d19c);return _0x2303ff[_0x66bf('0x23')](_0x66bf('0x24'),_0x277f79+'-'+_0x1a0125+'/'+_0xaca9c9)['json'](_0x2077f0);}return null;};}function patchUpdates(_0x627e6f){return function(_0x5f0334){try{jsonpatch['apply'](_0x5f0334,_0x627e6f,!![]);}catch(_0x2c9268){return BPromise['reject'](_0x2c9268);}return _0x5f0334[_0x66bf('0x25')]();};}function saveUpdates(_0x15b3d9,_0x471539){return function(_0x2fbba6){if(_0x2fbba6){return _0x2fbba6[_0x66bf('0x26')](_0x15b3d9)[_0x66bf('0x27')](function(_0x1171c8){return _0x1171c8;});}return null;};}function removeEntity(_0x531d65,_0x31efe1){return function(_0x260951){if(_0x260951){return _0x260951['destroy']()['then'](function(){_0x531d65[_0x66bf('0x1e')](0xcc)[_0x66bf('0x1d')]();});}};}function handleEntityNotFound(_0x4aacaa,_0x54e5e9){return function(_0x2fed90){if(!_0x2fed90){_0x4aacaa[_0x66bf('0x1c')](0x194);}return _0x2fed90;};}function handleError(_0x39868a,_0x4e611e){_0x4e611e=_0x4e611e||0x1f4;return function(_0x56dd34){logger[_0x66bf('0x28')](_0x56dd34['stack']);if(_0x56dd34[_0x66bf('0x29')]){delete _0x56dd34[_0x66bf('0x29')];}_0x39868a[_0x66bf('0x1e')](_0x4e611e)[_0x66bf('0x2a')](_0x56dd34);};}exports[_0x66bf('0x2b')]=function(_0x16d559,_0x11bf58){var _0x38ae18={},_0x510877={},_0x64c417={'count':0x0,'rows':[]};var _0x23915f=_['map'](db['Attachment'][_0x66bf('0x2c')],function(_0x458a39){return{'name':_0x458a39['fieldName'],'type':_0x458a39[_0x66bf('0x2d')][_0x66bf('0x2e')]};});_0x510877[_0x66bf('0x2f')]=_[_0x66bf('0x30')](_0x23915f,_0x66bf('0x29'));_0x510877[_0x66bf('0x31')]=_[_0x66bf('0x32')](_0x16d559[_0x66bf('0x31')]);_0x510877[_0x66bf('0x33')]=_[_0x66bf('0x34')](_0x510877[_0x66bf('0x2f')],_0x510877[_0x66bf('0x31')]);_0x38ae18['attributes']=_[_0x66bf('0x34')](_0x510877[_0x66bf('0x2f')],qs[_0x66bf('0x35')](_0x16d559[_0x66bf('0x31')][_0x66bf('0x35')]));_0x38ae18[_0x66bf('0x36')]=_0x38ae18['attributes'][_0x66bf('0x37')]?_0x38ae18[_0x66bf('0x36')]:_0x510877[_0x66bf('0x2f')];if(!_0x16d559[_0x66bf('0x31')][_0x66bf('0x38')](_0x66bf('0x39'))){_0x38ae18[_0x66bf('0x3a')]=qs[_0x66bf('0x3a')](_0x16d559[_0x66bf('0x31')][_0x66bf('0x3a')]);_0x38ae18[_0x66bf('0x20')]=qs[_0x66bf('0x20')](_0x16d559['query'][_0x66bf('0x20')]);}_0x38ae18[_0x66bf('0x3b')]=qs[_0x66bf('0x3c')](_0x16d559[_0x66bf('0x31')][_0x66bf('0x3c')]);_0x38ae18['where']=qs[_0x66bf('0x33')](_[_0x66bf('0x3d')](_0x16d559[_0x66bf('0x31')],_0x510877[_0x66bf('0x33')]),_0x23915f);if(_0x16d559[_0x66bf('0x31')][_0x66bf('0x3e')]){_0x38ae18[_0x66bf('0x3f')]=_[_0x66bf('0x40')](_0x38ae18[_0x66bf('0x3f')],{'$or':_[_0x66bf('0x30')](_0x23915f,function(_0x434f23){if(_0x434f23[_0x66bf('0x2d')]!==_0x66bf('0x41')){var _0x135bb3={};_0x135bb3[_0x434f23['name']]={'$like':'%'+_0x16d559[_0x66bf('0x31')][_0x66bf('0x3e')]+'%'};return _0x135bb3;}})});}_0x38ae18=_[_0x66bf('0x40')]({},_0x38ae18,_0x16d559[_0x66bf('0x42')]);var _0x238e43={'where':_0x38ae18[_0x66bf('0x3f')]};return db[_0x66bf('0x43')][_0x66bf('0x22')](_0x238e43)['then'](function(_0x4732a9){_0x64c417[_0x66bf('0x22')]=_0x4732a9;if(_0x16d559['query'][_0x66bf('0x44')]){_0x38ae18[_0x66bf('0x45')]=[{'all':!![]}];}return db[_0x66bf('0x43')][_0x66bf('0x46')](_0x38ae18);})[_0x66bf('0x27')](function(_0xb2c768){_0x64c417[_0x66bf('0x47')]=_0xb2c768;return _0x64c417;})[_0x66bf('0x27')](respondWithFilteredResult(_0x11bf58,_0x38ae18))[_0x66bf('0x48')](handleError(_0x11bf58,null));};exports[_0x66bf('0x49')]=function(_0x1f6e1e,_0x168beb){var _0x416976={'raw':!![],'where':{'id':_0x1f6e1e['params']['id']}},_0x2d252c={};_0x2d252c['model']=_[_0x66bf('0x32')](db[_0x66bf('0x43')][_0x66bf('0x2c')]);_0x2d252c[_0x66bf('0x31')]=_[_0x66bf('0x32')](_0x1f6e1e[_0x66bf('0x31')]);_0x2d252c['filters']=_[_0x66bf('0x34')](_0x2d252c['model'],_0x2d252c['query']);_0x416976[_0x66bf('0x36')]=_[_0x66bf('0x34')](_0x2d252c[_0x66bf('0x2f')],qs[_0x66bf('0x35')](_0x1f6e1e[_0x66bf('0x31')][_0x66bf('0x35')]));_0x416976[_0x66bf('0x36')]=_0x416976[_0x66bf('0x36')][_0x66bf('0x37')]?_0x416976[_0x66bf('0x36')]:_0x2d252c[_0x66bf('0x2f')];if(_0x1f6e1e[_0x66bf('0x31')]['includeAll']){_0x416976[_0x66bf('0x45')]=[{'all':!![]}];}_0x416976=_[_0x66bf('0x40')]({},_0x416976,_0x1f6e1e['options']);return db[_0x66bf('0x43')][_0x66bf('0x4a')](_0x416976)[_0x66bf('0x27')](handleEntityNotFound(_0x168beb,null))['then'](respondWithResult(_0x168beb,null))[_0x66bf('0x48')](handleError(_0x168beb,null));};exports[_0x66bf('0x4b')]=function(_0x160f0a,_0x27ecb0){var _0x541f76={'where':{'id':_0x160f0a['params']['id']}},_0x4eebf9={};_0x4eebf9[_0x66bf('0x2f')]=_['keys'](db[_0x66bf('0x43')][_0x66bf('0x2c')]);_0x541f76[_0x66bf('0x36')]=_['intersection'](_0x4eebf9[_0x66bf('0x2f')],qs[_0x66bf('0x35')](_0x160f0a[_0x66bf('0x31')]['fields']));_0x541f76['attributes']=_0x541f76['attributes'][_0x66bf('0x37')]?_0x541f76[_0x66bf('0x36')]:_0x4eebf9[_0x66bf('0x2f')];if(_0x160f0a[_0x66bf('0x31')][_0x66bf('0x44')]){_0x541f76[_0x66bf('0x45')]=[{'all':!![]}];}_0x541f76=_['merge']({},_0x541f76,_0x160f0a[_0x66bf('0x42')]);return db[_0x66bf('0x43')][_0x66bf('0x4a')](_0x541f76)[_0x66bf('0x27')](handleEntityNotFound(_0x27ecb0,null))[_0x66bf('0x27')](function(_0x43ef94){if(_0x43ef94){var _0x45ad36=_0x43ef94[_0x66bf('0x4c')]({'plain':!![]});_0x45ad36=qs[_0x66bf('0x4d')](_0x45ad36,['id',_0x66bf('0x4e'),_0x66bf('0x4f')]);_0x160f0a[_0x66bf('0x50')]=_['omit'](_0x160f0a[_0x66bf('0x50')],['id','createdAt','updatedAt']);return db['Attachment']['create'](_['merge'](_0x45ad36,_0x160f0a[_0x66bf('0x50')]),{'include':_0x160f0a[_0x66bf('0x31')][_0x66bf('0x44')]?[{'all':!![]}]:undefined});}})[_0x66bf('0x27')](respondWithResult(_0x27ecb0,0xc9))[_0x66bf('0x48')](handleError(_0x27ecb0,null));};exports[_0x66bf('0x26')]=function(_0x7f5e38,_0x39f6c3){if(_0x7f5e38['body']['id']){delete _0x7f5e38['body']['id'];}return db[_0x66bf('0x43')][_0x66bf('0x4a')]({'where':{'id':_0x7f5e38[_0x66bf('0x51')]['id']}})[_0x66bf('0x27')](handleEntityNotFound(_0x39f6c3,null))[_0x66bf('0x27')](saveUpdates(_0x7f5e38['body'],null))[_0x66bf('0x27')](respondWithResult(_0x39f6c3,null))[_0x66bf('0x48')](handleError(_0x39f6c3,null));};exports[_0x66bf('0x52')]=function(_0x3acdb5,_0x20f181){return db[_0x66bf('0x43')][_0x66bf('0x4a')]({'where':{'id':_0x3acdb5[_0x66bf('0x51')]['id']}})[_0x66bf('0x27')](handleEntityNotFound(_0x20f181,null))[_0x66bf('0x27')](removeEntity(_0x20f181,null))[_0x66bf('0x48')](handleError(_0x20f181,null));};exports[_0x66bf('0x53')]=function(_0x468bb5,_0x3604ee,_0x369075){_0x468bb5[_0x66bf('0x50')]['name']=_0x468bb5[_0x66bf('0x54')][_0x66bf('0x55')];_0x468bb5[_0x66bf('0x50')][_0x66bf('0x56')]=_0x468bb5['file'][_0x66bf('0x57')];_0x468bb5['body'][_0x66bf('0x2d')]=_0x468bb5['file'][_0x66bf('0x58')];return db[_0x66bf('0x43')]['create'](_0x468bb5['body'])['then'](respondWithResult(_0x3604ee,0xc9))[_0x66bf('0x48')](handleError(_0x3604ee,null));};exports[_0x66bf('0x59')]=function(_0x27c8f1,_0x5e3097,_0x50354c){return db[_0x66bf('0x43')][_0x66bf('0x5a')]({'where':{'id':_0x27c8f1[_0x66bf('0x51')]['id']},'raw':!![]})[_0x66bf('0x27')](handleEntityNotFound(_0x5e3097,null))[_0x66bf('0x27')](function(_0x90aa80){if(_0x90aa80){var _0x50185e=_0x90aa80[_0x66bf('0x5b')]!==null;var _0x5e5a21=path['join'](config['root'],_0x66bf('0x5c'));if(_0x50185e){_0x5e5a21=path[_0x66bf('0x5d')](_0x5e5a21,_0x66bf('0x5e'));}var _0x38e769=path[_0x66bf('0x5d')](_0x5e5a21,_0x90aa80[_0x66bf('0x56')]);return _0x5e3097['download'](_0x38e769);}})[_0x66bf('0x48')](handleError(_0x5e3097,null));};