f7264abf9a15fcda5e6740bd454e63abdb7361c5
[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 _0xc18f=['index','rawAttributes','fieldName','key','model','query','keys','attributes','intersection','fields','hasOwnProperty','order','sort','where','filters','pick','filter','map','type','VIRTUAL','Attachment','includeAll','include','findAll','rows','catch','show','params','length','options','clone','find','omit','updatedAt','create','file','originalname','basename','filename','mimetype','findOne','root','server/files/attachments/','join','offline-chat/','download','eml-format','zip-dir','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','reject','update','then','destroy','end','error','stack','name','render','merge','body','contact','message','agent','interaction'];(function(_0x115ef0,_0x5a83b2){var _0x420f5a=function(_0x102b2d){while(--_0x102b2d){_0x115ef0['push'](_0x115ef0['shift']());}};_0x420f5a(++_0x5a83b2);}(_0xc18f,0x148));var _0xfc18=function(_0x2ccd33,_0x450bc6){_0x2ccd33=_0x2ccd33-0x0;var _0x3bf83c=_0xc18f[_0x2ccd33];return _0x3bf83c;};'use strict';var emlformat=require(_0xfc18('0x0'));var rimraf=require('rimraf');var zipdir=require(_0xfc18('0x1'));var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require('moment');var BPromise=require(_0xfc18('0x2'));var Mustache=require(_0xfc18('0x3'));var util=require(_0xfc18('0x4'));var path=require(_0xfc18('0x5'));var sox=require(_0xfc18('0x6'));var csv=require(_0xfc18('0x7'));var ejs=require(_0xfc18('0x8'));var fs=require('fs');var fs_extra=require(_0xfc18('0x9'));var _=require(_0xfc18('0xa'));var squel=require('squel');var crypto=require(_0xfc18('0xb'));var jsforce=require(_0xfc18('0xc'));var deskjs=require(_0xfc18('0xd'));var toCsv=require(_0xfc18('0x7'));var querystring=require('querystring');var Papa=require(_0xfc18('0xe'));var Redis=require(_0xfc18('0xf'));var authService=require(_0xfc18('0x10'));var qs=require(_0xfc18('0x11'));var as=require(_0xfc18('0x12'));var hardwareService=require(_0xfc18('0x13'));var logger=require(_0xfc18('0x14'))(_0xfc18('0x15'));var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0xfc18('0x16'));var db=require(_0xfc18('0x17'))['db'];function respondWithStatusCode(_0x5d8869,_0x2c68be){_0x2c68be=_0x2c68be||0xcc;return function(_0x262162){if(_0x262162){return _0x5d8869[_0xfc18('0x18')](_0x2c68be);}return _0x5d8869[_0xfc18('0x19')](_0x2c68be)['end']();};}function respondWithResult(_0x1bf002,_0x3db61a){_0x3db61a=_0x3db61a||0xc8;return function(_0x4ebb6e){if(_0x4ebb6e){return _0x1bf002[_0xfc18('0x19')](_0x3db61a)[_0xfc18('0x1a')](_0x4ebb6e);}};}function respondWithFilteredResult(_0x1050b1,_0x230488){return function(_0x274774){if(_0x274774){var _0xc017ac=typeof _0x230488[_0xfc18('0x1b')]===_0xfc18('0x1c')&&typeof _0x230488[_0xfc18('0x1d')]===_0xfc18('0x1c');var _0x2d471a=_0x274774['count'];var _0x19903d=_0xc017ac?0x0:_0x230488[_0xfc18('0x1b')];var _0x462c2f=_0xc017ac?_0x274774[_0xfc18('0x1e')]:_0x230488[_0xfc18('0x1b')]+_0x230488[_0xfc18('0x1d')];var _0x14b7f3;if(_0x462c2f>=_0x2d471a){_0x462c2f=_0x2d471a;_0x14b7f3=0xc8;}else{_0x14b7f3=0xce;}_0x1050b1[_0xfc18('0x19')](_0x14b7f3);return _0x1050b1[_0xfc18('0x1f')](_0xfc18('0x20'),_0x19903d+'-'+_0x462c2f+'/'+_0x2d471a)[_0xfc18('0x1a')](_0x274774);}return null;};}function patchUpdates(_0x13291a){return function(_0x220abd){try{jsonpatch['apply'](_0x220abd,_0x13291a,!![]);}catch(_0x37150f){return BPromise[_0xfc18('0x21')](_0x37150f);}return _0x220abd['save']();};}function saveUpdates(_0x691c85,_0x192884){return function(_0x1caedb){if(_0x1caedb){return _0x1caedb[_0xfc18('0x22')](_0x691c85)[_0xfc18('0x23')](function(_0x2946b0){return _0x2946b0;});}return null;};}function removeEntity(_0x299a0c,_0x41183d){return function(_0x4145be){if(_0x4145be){return _0x4145be[_0xfc18('0x24')]()['then'](function(){_0x299a0c['status'](0xcc)[_0xfc18('0x25')]();});}};}function handleEntityNotFound(_0xa29ae0,_0x5bd5f9){return function(_0x2634ce){if(!_0x2634ce){_0xa29ae0[_0xfc18('0x18')](0x194);}return _0x2634ce;};}function handleError(_0x2aa0c7,_0x43fb6f){_0x43fb6f=_0x43fb6f||0x1f4;return function(_0x1130f2){logger[_0xfc18('0x26')](_0x1130f2[_0xfc18('0x27')]);if(_0x1130f2[_0xfc18('0x28')]){delete _0x1130f2[_0xfc18('0x28')];}_0x2aa0c7['status'](_0x43fb6f)['send'](_0x1130f2);};}function getMustacheRender(_0x46471b,_0x36514d){return Mustache[_0xfc18('0x29')](_0x46471b,_[_0xfc18('0x2a')](_0x36514d[_0xfc18('0x2b')],{'contact':_0x36514d[_0xfc18('0x2c')],'message':_0x36514d[_0xfc18('0x2d')],'agent':_0x36514d[_0xfc18('0x2e')],'interaction':_0x36514d[_0xfc18('0x2f')],'account':_0x36514d['account']}))||'';}exports[_0xfc18('0x30')]=function(_0x319565,_0x4c5116){var _0x2f7fc4={},_0x30dabb={},_0x47576c={'count':0x0,'rows':[]};var _0x4f7b49=_['map'](db['Attachment'][_0xfc18('0x31')],function(_0x1b650f){return{'name':_0x1b650f[_0xfc18('0x32')],'type':_0x1b650f['type'][_0xfc18('0x33')]};});_0x30dabb[_0xfc18('0x34')]=_['map'](_0x4f7b49,_0xfc18('0x28'));_0x30dabb[_0xfc18('0x35')]=_[_0xfc18('0x36')](_0x319565[_0xfc18('0x35')]);_0x30dabb['filters']=_['intersection'](_0x30dabb[_0xfc18('0x34')],_0x30dabb['query']);_0x2f7fc4[_0xfc18('0x37')]=_[_0xfc18('0x38')](_0x30dabb[_0xfc18('0x34')],qs[_0xfc18('0x39')](_0x319565[_0xfc18('0x35')][_0xfc18('0x39')]));_0x2f7fc4[_0xfc18('0x37')]=_0x2f7fc4[_0xfc18('0x37')]['length']?_0x2f7fc4[_0xfc18('0x37')]:_0x30dabb['model'];if(!_0x319565[_0xfc18('0x35')][_0xfc18('0x3a')]('nolimit')){_0x2f7fc4['limit']=qs['limit'](_0x319565[_0xfc18('0x35')][_0xfc18('0x1d')]);_0x2f7fc4[_0xfc18('0x1b')]=qs['offset'](_0x319565['query'][_0xfc18('0x1b')]);}_0x2f7fc4[_0xfc18('0x3b')]=qs[_0xfc18('0x3c')](_0x319565[_0xfc18('0x35')][_0xfc18('0x3c')]);_0x2f7fc4[_0xfc18('0x3d')]=qs[_0xfc18('0x3e')](_[_0xfc18('0x3f')](_0x319565['query'],_0x30dabb[_0xfc18('0x3e')]),_0x4f7b49);if(_0x319565['query'][_0xfc18('0x40')]){_0x2f7fc4['where']=_[_0xfc18('0x2a')](_0x2f7fc4['where'],{'$or':_[_0xfc18('0x41')](_0x4f7b49,function(_0x23eea3){if(_0x23eea3[_0xfc18('0x42')]!==_0xfc18('0x43')){var _0x2adb01={};_0x2adb01[_0x23eea3[_0xfc18('0x28')]]={'$like':'%'+_0x319565[_0xfc18('0x35')][_0xfc18('0x40')]+'%'};return _0x2adb01;}})});}_0x2f7fc4=_[_0xfc18('0x2a')]({},_0x2f7fc4,_0x319565['options']);var _0x2b9690={'where':_0x2f7fc4[_0xfc18('0x3d')]};return db[_0xfc18('0x44')][_0xfc18('0x1e')](_0x2b9690)['then'](function(_0x12935b){_0x47576c[_0xfc18('0x1e')]=_0x12935b;if(_0x319565['query'][_0xfc18('0x45')]){_0x2f7fc4[_0xfc18('0x46')]=[{'all':!![]}];}return db['Attachment'][_0xfc18('0x47')](_0x2f7fc4);})[_0xfc18('0x23')](function(_0x35f14d){_0x47576c[_0xfc18('0x48')]=_0x35f14d;return _0x47576c;})[_0xfc18('0x23')](respondWithFilteredResult(_0x4c5116,_0x2f7fc4))[_0xfc18('0x49')](handleError(_0x4c5116,null));};exports[_0xfc18('0x4a')]=function(_0x4d265d,_0x3a153e){var _0x19f37f={'raw':!![],'where':{'id':_0x4d265d[_0xfc18('0x4b')]['id']}},_0x3db38a={};_0x3db38a['model']=_[_0xfc18('0x36')](db[_0xfc18('0x44')][_0xfc18('0x31')]);_0x3db38a['query']=_[_0xfc18('0x36')](_0x4d265d[_0xfc18('0x35')]);_0x3db38a[_0xfc18('0x3e')]=_[_0xfc18('0x38')](_0x3db38a['model'],_0x3db38a[_0xfc18('0x35')]);_0x19f37f[_0xfc18('0x37')]=_[_0xfc18('0x38')](_0x3db38a[_0xfc18('0x34')],qs['fields'](_0x4d265d['query']['fields']));_0x19f37f[_0xfc18('0x37')]=_0x19f37f[_0xfc18('0x37')][_0xfc18('0x4c')]?_0x19f37f[_0xfc18('0x37')]:_0x3db38a['model'];if(_0x4d265d[_0xfc18('0x35')]['includeAll']){_0x19f37f['include']=[{'all':!![]}];}_0x19f37f=_[_0xfc18('0x2a')]({},_0x19f37f,_0x4d265d[_0xfc18('0x4d')]);return db[_0xfc18('0x44')]['find'](_0x19f37f)[_0xfc18('0x23')](handleEntityNotFound(_0x3a153e,null))['then'](respondWithResult(_0x3a153e,null))[_0xfc18('0x49')](handleError(_0x3a153e,null));};exports[_0xfc18('0x4e')]=function(_0x33a4d,_0x5420b7){var _0x190d02={'where':{'id':_0x33a4d[_0xfc18('0x4b')]['id']}},_0x44f427={};_0x44f427[_0xfc18('0x34')]=_[_0xfc18('0x36')](db['Attachment'][_0xfc18('0x31')]);_0x190d02['attributes']=_[_0xfc18('0x38')](_0x44f427[_0xfc18('0x34')],qs[_0xfc18('0x39')](_0x33a4d[_0xfc18('0x35')][_0xfc18('0x39')]));_0x190d02[_0xfc18('0x37')]=_0x190d02[_0xfc18('0x37')][_0xfc18('0x4c')]?_0x190d02[_0xfc18('0x37')]:_0x44f427[_0xfc18('0x34')];if(_0x33a4d[_0xfc18('0x35')][_0xfc18('0x45')]){_0x190d02[_0xfc18('0x46')]=[{'all':!![]}];}_0x190d02=_[_0xfc18('0x2a')]({},_0x190d02,_0x33a4d[_0xfc18('0x4d')]);return db[_0xfc18('0x44')][_0xfc18('0x4f')](_0x190d02)[_0xfc18('0x23')](handleEntityNotFound(_0x5420b7,null))[_0xfc18('0x23')](function(_0x1b9a6a){if(_0x1b9a6a){var _0x19887c=_0x1b9a6a['get']({'plain':!![]});_0x19887c=qs[_0xfc18('0x50')](_0x19887c,['id','createdAt',_0xfc18('0x51')]);_0x33a4d[_0xfc18('0x2b')]=_[_0xfc18('0x50')](_0x33a4d['body'],['id','createdAt',_0xfc18('0x51')]);return db['Attachment'][_0xfc18('0x52')](_[_0xfc18('0x2a')](_0x19887c,_0x33a4d[_0xfc18('0x2b')]),{'include':_0x33a4d['query'][_0xfc18('0x45')]?[{'all':!![]}]:undefined});}})[_0xfc18('0x23')](respondWithResult(_0x5420b7,0xc9))[_0xfc18('0x49')](handleError(_0x5420b7,null));};exports[_0xfc18('0x22')]=function(_0x48c653,_0x150a20){if(_0x48c653[_0xfc18('0x2b')]['id']){delete _0x48c653[_0xfc18('0x2b')]['id'];}return db[_0xfc18('0x44')]['find']({'where':{'id':_0x48c653['params']['id']}})[_0xfc18('0x23')](handleEntityNotFound(_0x150a20,null))[_0xfc18('0x23')](saveUpdates(_0x48c653[_0xfc18('0x2b')],null))['then'](respondWithResult(_0x150a20,null))['catch'](handleError(_0x150a20,null));};exports[_0xfc18('0x24')]=function(_0xdecd5,_0x7b0137){return db[_0xfc18('0x44')]['find']({'where':{'id':_0xdecd5[_0xfc18('0x4b')]['id']}})[_0xfc18('0x23')](handleEntityNotFound(_0x7b0137,null))[_0xfc18('0x23')](removeEntity(_0x7b0137,null))[_0xfc18('0x49')](handleError(_0x7b0137,null));};exports[_0xfc18('0x52')]=function(_0x3effd9,_0x37ca13,_0x377593){_0x3effd9['body'][_0xfc18('0x28')]=_0x3effd9[_0xfc18('0x53')][_0xfc18('0x54')];_0x3effd9['body'][_0xfc18('0x55')]=_0x3effd9[_0xfc18('0x53')][_0xfc18('0x56')];_0x3effd9[_0xfc18('0x2b')][_0xfc18('0x42')]=_0x3effd9[_0xfc18('0x53')][_0xfc18('0x57')];return db[_0xfc18('0x44')]['create'](_0x3effd9[_0xfc18('0x2b')])[_0xfc18('0x23')](respondWithResult(_0x37ca13,0xc9))['catch'](handleError(_0x37ca13,null));};exports['download']=function(_0x308251,_0x4c5f27,_0x2f417b){return db['Attachment'][_0xfc18('0x58')]({'where':{'id':_0x308251[_0xfc18('0x4b')]['id']},'raw':!![]})[_0xfc18('0x23')](handleEntityNotFound(_0x4c5f27,null))[_0xfc18('0x23')](function(_0x1e12ed){if(_0x1e12ed){var _0x394dac=_0x1e12ed['ChatOfflineMessageId']!==null;var _0x454cf4=path['join'](config[_0xfc18('0x59')],_0xfc18('0x5a'));if(_0x394dac){_0x454cf4=path[_0xfc18('0x5b')](_0x454cf4,_0xfc18('0x5c'));}var _0x19f510=path[_0xfc18('0x5b')](_0x454cf4,_0x1e12ed['basename']);return _0x4c5f27[_0xfc18('0x5d')](_0x19f510);}})[_0xfc18('0x49')](handleError(_0x4c5f27,null));};