d580e28f26028d9a20a36e75ed77d5c09f1d3f25
[motion2.git] / server / api / sound / sound.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 _0x2f96=['filter','where','merge','options','includeAll','include','findAll','rows','show','params','catch','download','find','save_name','converted_format','existsSync','join','root','Sound\x20not\x20found','server/files/sounds/converted','format','%s.%s','basename','file','extname','originalname','substring','wav','server/files/sounds/original','filename','info','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','error','unlinkSync','src','original_duration','duration','original_sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_bitRate','create','body','userProfileId','role','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','description','delete','original_format','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','ejs','fs-extra','crypto','jsforce','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/license/util','../../mysqldb','json','offset','undefined','limit','count','status','set','apply','reject','save','update','then','destroy','get','Sounds','UserProfileResource','end','sendStatus','stack','name','send','map','Sound','rawAttributes','type','model','query','keys','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','filters','pick'];(function(_0x53c8c7,_0x348f83){var _0x4f9626=function(_0x25c457){while(--_0x25c457){_0x53c8c7['push'](_0x53c8c7['shift']());}};_0x4f9626(++_0x348f83);}(_0x2f96,0x1b5));var _0x62f9=function(_0x4e9378,_0x837853){_0x4e9378=_0x4e9378-0x0;var _0x5a8906=_0x2f96[_0x4e9378];return _0x5a8906;};'use strict';var emlformat=require(_0x62f9('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x62f9('0x1'));var jsonpatch=require(_0x62f9('0x2'));var rp=require(_0x62f9('0x3'));var moment=require(_0x62f9('0x4'));var BPromise=require(_0x62f9('0x5'));var Mustache=require(_0x62f9('0x6'));var util=require(_0x62f9('0x7'));var path=require(_0x62f9('0x8'));var sox=require(_0x62f9('0x9'));var csv=require('to-csv');var ejs=require(_0x62f9('0xa'));var fs=require('fs');var fs_extra=require(_0x62f9('0xb'));var _=require('lodash');var squel=require('squel');var crypto=require(_0x62f9('0xc'));var jsforce=require(_0x62f9('0xd'));var deskjs=require('desk.js');var toCsv=require(_0x62f9('0xe'));var querystring=require(_0x62f9('0xf'));var Papa=require(_0x62f9('0x10'));var Redis=require(_0x62f9('0x11'));var authService=require(_0x62f9('0x12'));var qs=require(_0x62f9('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x62f9('0x14'));var logger=require(_0x62f9('0x15'))(_0x62f9('0x16'));var utils=require(_0x62f9('0x17'));var config=require('../../config/environment');var licenseUtil=require(_0x62f9('0x18'));var db=require(_0x62f9('0x19'))['db'];function respondWithStatusCode(_0x1febcd,_0x3fd3ef){_0x3fd3ef=_0x3fd3ef||0xcc;return function(_0x37e82d){if(_0x37e82d){return _0x1febcd['sendStatus'](_0x3fd3ef);}return _0x1febcd['status'](_0x3fd3ef)['end']();};}function respondWithResult(_0x73ad52,_0x5bba6e){_0x5bba6e=_0x5bba6e||0xc8;return function(_0x3af45d){if(_0x3af45d){return _0x73ad52['status'](_0x5bba6e)[_0x62f9('0x1a')](_0x3af45d);}};}function respondWithFilteredResult(_0x286927,_0x4d3a23){return function(_0x27c26f){if(_0x27c26f){var _0x5dd965=typeof _0x4d3a23[_0x62f9('0x1b')]===_0x62f9('0x1c')&&typeof _0x4d3a23[_0x62f9('0x1d')]===_0x62f9('0x1c');var _0x2c4344=_0x27c26f[_0x62f9('0x1e')];var _0x3b36af=_0x5dd965?0x0:_0x4d3a23[_0x62f9('0x1b')];var _0xcbf2be=_0x5dd965?_0x27c26f[_0x62f9('0x1e')]:_0x4d3a23[_0x62f9('0x1b')]+_0x4d3a23[_0x62f9('0x1d')];var _0x9dd598;if(_0xcbf2be>=_0x2c4344){_0xcbf2be=_0x2c4344;_0x9dd598=0xc8;}else{_0x9dd598=0xce;}_0x286927[_0x62f9('0x1f')](_0x9dd598);return _0x286927[_0x62f9('0x20')]('Content-Range',_0x3b36af+'-'+_0xcbf2be+'/'+_0x2c4344)[_0x62f9('0x1a')](_0x27c26f);}return null;};}function patchUpdates(_0x13d55a){return function(_0x2111e2){try{jsonpatch[_0x62f9('0x21')](_0x2111e2,_0x13d55a,!![]);}catch(_0x1f61ad){return BPromise[_0x62f9('0x22')](_0x1f61ad);}return _0x2111e2[_0x62f9('0x23')]();};}function saveUpdates(_0x5eda2c,_0xacac78){return function(_0x2b54eb){if(_0x2b54eb){return _0x2b54eb[_0x62f9('0x24')](_0x5eda2c)[_0x62f9('0x25')](function(_0xce3a88){return _0xce3a88;});}return null;};}function removeEntity(_0x274b92,_0x1257b7){return function(_0x2ad47b){if(_0x2ad47b){return _0x2ad47b[_0x62f9('0x26')]()[_0x62f9('0x25')](function(){var _0x2b916c=_0x2ad47b[_0x62f9('0x27')]({'plain':!![]});var _0x90be89=_0x62f9('0x28');return db[_0x62f9('0x29')][_0x62f9('0x26')]({'where':{'type':_0x90be89,'resourceId':_0x2b916c['id']}})['then'](function(){return _0x2ad47b;});})[_0x62f9('0x25')](function(){_0x274b92[_0x62f9('0x1f')](0xcc)[_0x62f9('0x2a')]();});}};}function handleEntityNotFound(_0x4d6f6e,_0xdcc21a){return function(_0x554190){if(!_0x554190){_0x4d6f6e[_0x62f9('0x2b')](0x194);}return _0x554190;};}function handleError(_0x5b57ee,_0x308cbb){_0x308cbb=_0x308cbb||0x1f4;return function(_0xdead31){logger['error'](_0xdead31[_0x62f9('0x2c')]);if(_0xdead31[_0x62f9('0x2d')]){delete _0xdead31[_0x62f9('0x2d')];}_0x5b57ee[_0x62f9('0x1f')](_0x308cbb)[_0x62f9('0x2e')](_0xdead31);};}exports['index']=function(_0x31d6c1,_0x15383b){var _0x206ccf={},_0x42644a={},_0x18165d={'count':0x0,'rows':[]};var _0x571a8c=_[_0x62f9('0x2f')](db[_0x62f9('0x30')][_0x62f9('0x31')],function(_0x25cec6){return{'name':_0x25cec6['fieldName'],'type':_0x25cec6[_0x62f9('0x32')]['key']};});_0x42644a[_0x62f9('0x33')]=_[_0x62f9('0x2f')](_0x571a8c,_0x62f9('0x2d'));_0x42644a[_0x62f9('0x34')]=_[_0x62f9('0x35')](_0x31d6c1[_0x62f9('0x34')]);_0x42644a['filters']=_[_0x62f9('0x36')](_0x42644a[_0x62f9('0x33')],_0x42644a[_0x62f9('0x34')]);_0x206ccf[_0x62f9('0x37')]=_[_0x62f9('0x36')](_0x42644a['model'],qs[_0x62f9('0x38')](_0x31d6c1[_0x62f9('0x34')][_0x62f9('0x38')]));_0x206ccf['attributes']=_0x206ccf[_0x62f9('0x37')][_0x62f9('0x39')]?_0x206ccf['attributes']:_0x42644a[_0x62f9('0x33')];if(!_0x31d6c1['query'][_0x62f9('0x3a')](_0x62f9('0x3b'))){_0x206ccf[_0x62f9('0x1d')]=qs[_0x62f9('0x1d')](_0x31d6c1['query'][_0x62f9('0x1d')]);_0x206ccf[_0x62f9('0x1b')]=qs['offset'](_0x31d6c1[_0x62f9('0x34')][_0x62f9('0x1b')]);}_0x206ccf['order']=qs[_0x62f9('0x3c')](_0x31d6c1['query'][_0x62f9('0x3c')]);_0x206ccf['where']=qs[_0x62f9('0x3d')](_[_0x62f9('0x3e')](_0x31d6c1[_0x62f9('0x34')],_0x42644a['filters']),_0x571a8c);if(_0x31d6c1[_0x62f9('0x34')][_0x62f9('0x3f')]){_0x206ccf[_0x62f9('0x40')]=_[_0x62f9('0x41')](_0x206ccf[_0x62f9('0x40')],{'$or':_[_0x62f9('0x2f')](_0x571a8c,function(_0x3127e7){if(_0x3127e7[_0x62f9('0x32')]!=='VIRTUAL'){var _0x5413bd={};_0x5413bd[_0x3127e7[_0x62f9('0x2d')]]={'$like':'%'+_0x31d6c1[_0x62f9('0x34')]['filter']+'%'};return _0x5413bd;}})});}_0x206ccf=_[_0x62f9('0x41')]({},_0x206ccf,_0x31d6c1[_0x62f9('0x42')]);var _0x35f7ee={'where':_0x206ccf[_0x62f9('0x40')]};return db['Sound']['count'](_0x35f7ee)['then'](function(_0x94f3d5){_0x18165d[_0x62f9('0x1e')]=_0x94f3d5;if(_0x31d6c1[_0x62f9('0x34')][_0x62f9('0x43')]){_0x206ccf[_0x62f9('0x44')]=[{'all':!![]}];}return db[_0x62f9('0x30')][_0x62f9('0x45')](_0x206ccf);})[_0x62f9('0x25')](function(_0x2eb554){_0x18165d[_0x62f9('0x46')]=_0x2eb554;return _0x18165d;})[_0x62f9('0x25')](respondWithFilteredResult(_0x15383b,_0x206ccf))['catch'](handleError(_0x15383b,null));};exports[_0x62f9('0x47')]=function(_0x3ce242,_0x22de5){var _0x429dd3={'raw':!![],'where':{'id':_0x3ce242[_0x62f9('0x48')]['id']}},_0x309e17={};_0x309e17['model']=_[_0x62f9('0x35')](db[_0x62f9('0x30')][_0x62f9('0x31')]);_0x309e17[_0x62f9('0x34')]=_[_0x62f9('0x35')](_0x3ce242[_0x62f9('0x34')]);_0x309e17[_0x62f9('0x3d')]=_[_0x62f9('0x36')](_0x309e17['model'],_0x309e17[_0x62f9('0x34')]);_0x429dd3['attributes']=_['intersection'](_0x309e17[_0x62f9('0x33')],qs[_0x62f9('0x38')](_0x3ce242[_0x62f9('0x34')][_0x62f9('0x38')]));_0x429dd3[_0x62f9('0x37')]=_0x429dd3['attributes'][_0x62f9('0x39')]?_0x429dd3[_0x62f9('0x37')]:_0x309e17[_0x62f9('0x33')];if(_0x3ce242['query'][_0x62f9('0x43')]){_0x429dd3[_0x62f9('0x44')]=[{'all':!![]}];}_0x429dd3=_[_0x62f9('0x41')]({},_0x429dd3,_0x3ce242[_0x62f9('0x42')]);return db[_0x62f9('0x30')]['find'](_0x429dd3)['then'](handleEntityNotFound(_0x22de5,null))['then'](respondWithResult(_0x22de5,null))[_0x62f9('0x49')](handleError(_0x22de5,null));};exports[_0x62f9('0x4a')]=function(_0xec9f5c,_0x2aaf40,_0x210a4d){return db[_0x62f9('0x30')][_0x62f9('0x4b')]({'where':{'id':_0xec9f5c[_0x62f9('0x48')]['id']},'attributes':[_0x62f9('0x2d'),_0x62f9('0x4c'),_0x62f9('0x4d')],'raw':!![]})[_0x62f9('0x25')](handleEntityNotFound(_0x2aaf40,null))['then'](function(_0x29e5db){if(_0x29e5db){if(!fs[_0x62f9('0x4e')](path[_0x62f9('0x4f')](config[_0x62f9('0x50')],'server/files/sounds/converted',_0x29e5db[_0x62f9('0x4c')]+'.'+_0x29e5db['converted_format']))){throw new db['Sequelize']['ValidationError'](_0x62f9('0x51'));}return _0x2aaf40['download'](path[_0x62f9('0x4f')](config[_0x62f9('0x50')],_0x62f9('0x52'),util[_0x62f9('0x53')](_0x62f9('0x54'),_0x29e5db[_0x62f9('0x4c')],_0x29e5db[_0x62f9('0x4d')])),util[_0x62f9('0x53')](_0x62f9('0x54'),_0x29e5db[_0x62f9('0x2d')],_0x29e5db['converted_format']));}return null;})[_0x62f9('0x49')](handleError(_0x2aaf40,null));};exports['create']=function(_0x95318,_0x12b00a,_0x5d03ab){var _0x2fc2b4={'name':path[_0x62f9('0x55')](_0x95318[_0x62f9('0x56')]['originalname'],path[_0x62f9('0x57')](_0x95318[_0x62f9('0x56')][_0x62f9('0x58')])),'save_name':path[_0x62f9('0x55')](_0x95318[_0x62f9('0x56')]['filename'],path[_0x62f9('0x57')](_0x95318[_0x62f9('0x56')]['filename'])),'original_format':path[_0x62f9('0x57')](_0x95318[_0x62f9('0x56')][_0x62f9('0x58')])[_0x62f9('0x59')](0x1),'converted_format':_0x62f9('0x5a')};var _0x36a3e7=path[_0x62f9('0x4f')](config['root'],_0x62f9('0x5b'),_0x95318[_0x62f9('0x56')][_0x62f9('0x5c')]);var _0x1f8f70=path['join'](config[_0x62f9('0x50')],'server/files/sounds/converted',util['format'](_0x62f9('0x54'),path[_0x62f9('0x55')](_0x95318[_0x62f9('0x56')]['filename'],path[_0x62f9('0x57')](_0x95318[_0x62f9('0x56')][_0x62f9('0x5c')])),_0x2fc2b4[_0x62f9('0x4d')]));logger[_0x62f9('0x5d')]('Sound\x20upload\x20filepath:\x20%s',_0x36a3e7);logger[_0x62f9('0x5d')](_0x62f9('0x5e'),_0x1f8f70);try{fs[_0x62f9('0x5f')](_0x36a3e7,parseInt(_0x62f9('0x60'),0x8));var _0x18b7b8=sox[_0x62f9('0x61')](_0x36a3e7,_0x1f8f70,{'sampleRate':0x1f40,'format':_0x62f9('0x5a'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x18b7b8['on'](_0x62f9('0x62'),function(_0x2caffd){fs[_0x62f9('0x63')](_0x36a3e7);_0x12b00a['status'](0x1f4)['send'](_0x2caffd);});_0x18b7b8['on'](_0x62f9('0x64'),function(_0x41f3c5){_0x2fc2b4[_0x62f9('0x65')]=isNaN(_0x41f3c5[_0x62f9('0x66')])?null:_0x41f3c5['duration']*0x3e8;_0x2fc2b4[_0x62f9('0x67')]=isNaN(_0x41f3c5['sampleCount'])?null:_0x41f3c5['sampleCount'];_0x2fc2b4[_0x62f9('0x68')]=isNaN(_0x41f3c5[_0x62f9('0x69')])?null:_0x41f3c5['channelCount'];_0x2fc2b4[_0x62f9('0x6a')]=isNaN(_0x41f3c5[_0x62f9('0x6b')])?null:_0x41f3c5[_0x62f9('0x6b')];_0x2fc2b4['original_sampleRate']=isNaN(_0x41f3c5[_0x62f9('0x6c')])?null:_0x41f3c5[_0x62f9('0x6c')];});_0x18b7b8['on'](_0x62f9('0x6d'),function(_0x4b518b){_0x2fc2b4[_0x62f9('0x6e')]=isNaN(_0x4b518b['duration'])?null:_0x4b518b[_0x62f9('0x66')]*0x3e8;_0x2fc2b4[_0x62f9('0x6f')]=isNaN(_0x4b518b['sampleCount'])?null:_0x4b518b['sampleCount'];_0x2fc2b4['converted_channelCount']=isNaN(_0x4b518b[_0x62f9('0x69')])?null:_0x4b518b[_0x62f9('0x69')];_0x2fc2b4[_0x62f9('0x70')]=isNaN(_0x4b518b[_0x62f9('0x6b')])?null:_0x4b518b[_0x62f9('0x6b')];_0x2fc2b4['converted_sampleRate']=isNaN(_0x4b518b[_0x62f9('0x6c')])?null:_0x4b518b[_0x62f9('0x6c')];});_0x18b7b8['on'](_0x62f9('0x2a'),function(){fs[_0x62f9('0x5f')](_0x1f8f70,parseInt(_0x62f9('0x60'),0x8));return db[_0x62f9('0x30')][_0x62f9('0x71')](_0x2fc2b4)['then'](function(_0x478b3a){var _0x1f6aca={'role':_0x95318[_0x62f9('0x72')]['role'],'userProfileId':_0x95318[_0x62f9('0x72')][_0x62f9('0x73')]};if(!_0x1f6aca)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x1f6aca[_0x62f9('0x74')]===_0x62f9('0x75')){var _0x29e631=_0x478b3a[_0x62f9('0x27')]({'plain':!![]});return db[_0x62f9('0x76')][_0x62f9('0x4b')]({'where':{'name':_0x62f9('0x28'),'userProfileId':_0x1f6aca['userProfileId']},'raw':!![]})['then'](function(_0x2c533d){if(_0x2c533d&&_0x2c533d[_0x62f9('0x77')]===0x0){return db[_0x62f9('0x29')]['create']({'name':_0x29e631[_0x62f9('0x2d')],'resourceId':_0x29e631['id'],'type':_0x2c533d[_0x62f9('0x2d')],'sectionId':_0x2c533d['id']},{})[_0x62f9('0x25')](function(){return _0x478b3a;});}else{return _0x478b3a;}})[_0x62f9('0x49')](function(_0x4c6391){logger[_0x62f9('0x62')](_0x62f9('0x78'),_0x4c6391);throw _0x4c6391;});}return _0x478b3a;})[_0x62f9('0x25')](respondWithResult(_0x12b00a,null))['catch'](handleError(_0x12b00a,null));});_0x18b7b8[_0x62f9('0x79')]();}catch(_0x109e64){_0x12b00a['status'](0x1f4)[_0x62f9('0x2e')](_0x109e64);}};exports[_0x62f9('0x24')]=function(_0x3717e2,_0x2be415,_0x48b7bb){return db[_0x62f9('0x30')][_0x62f9('0x4b')]({'where':{'id':_0x3717e2[_0x62f9('0x48')]['id']}})[_0x62f9('0x25')](handleEntityNotFound(_0x2be415,null))[_0x62f9('0x25')](saveUpdates(_[_0x62f9('0x3e')](_0x3717e2[_0x62f9('0x72')],[_0x62f9('0x2d'),_0x62f9('0x7a')]),null))[_0x62f9('0x25')](respondWithResult(_0x2be415,null))[_0x62f9('0x49')](handleError(_0x2be415,null));};exports[_0x62f9('0x7b')]=function(_0x1ec92e,_0x5e76af,_0x33a8f5){return db[_0x62f9('0x30')][_0x62f9('0x4b')]({'where':{'id':_0x1ec92e[_0x62f9('0x48')]['id']}})[_0x62f9('0x25')](function(_0x598f29){if(_0x598f29){return _0x598f29[_0x62f9('0x26')]();}})[_0x62f9('0x25')](function(_0x3b4269){var _0x5aaeb9=path['join'](config['root'],_0x62f9('0x5b'),util[_0x62f9('0x53')]('%s.%s',_0x3b4269[_0x62f9('0x4c')],_0x3b4269[_0x62f9('0x7c')]));var _0x1b5b46=path[_0x62f9('0x4f')](config[_0x62f9('0x50')],_0x62f9('0x52'),util[_0x62f9('0x53')](_0x62f9('0x54'),_0x3b4269[_0x62f9('0x4c')],_0x3b4269['converted_format']));fs[_0x62f9('0x63')](_0x5aaeb9);fs['unlinkSync'](_0x1b5b46);return _0x3b4269;})[_0x62f9('0x25')](function(_0x2811e5){if(_0x2811e5){return _0x5e76af['status'](0xcc)[_0x62f9('0x2a')]();}})['then'](handleEntityNotFound(_0x5e76af,null))['catch'](handleError(_0x5e76af,null));};