Built motion from commit 1fd437e9.|2.6.19
[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 _0x807f=['intersection','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','options','includeAll','include','catch','params','download','save_name','existsSync','join','root','server/files/sounds/converted','converted_format','ValidationError','Sound\x20not\x20found','%s.%s','format','create','basename','file','originalname','filename','extname','wav','server/files/sounds/original','Sound\x20upload\x20filepath:\x20%s','chmodSync','transcode','src','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','0777','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','UserProfileSection','autoAssociation','UserProfileResource','start','find','description','delete','unlinkSync','eml-format','rimraf','fast-json-patch','request-promise','moment','bluebird','util','ejs','crypto','jsforce','desk.js','to-csv','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/environment','../../config/license/util','sendStatus','status','end','offset','undefined','count','limit','set','Content-Range','json','apply','reject','save','update','then','destroy','get','Sounds','error','name','send','index','map','Sound','rawAttributes','type','key','query','keys','filters','model','attributes'];(function(_0x5e4c44,_0x49b6fa){var _0x4482ea=function(_0x2f866f){while(--_0x2f866f){_0x5e4c44['push'](_0x5e4c44['shift']());}};_0x4482ea(++_0x49b6fa);}(_0x807f,0x1ad));var _0xf807=function(_0x59befa,_0x4af99b){_0x59befa=_0x59befa-0x0;var _0x1dd63d=_0x807f[_0x59befa];return _0x1dd63d;};'use strict';var emlformat=require(_0xf807('0x0'));var rimraf=require(_0xf807('0x1'));var zipdir=require('zip-dir');var jsonpatch=require(_0xf807('0x2'));var rp=require(_0xf807('0x3'));var moment=require(_0xf807('0x4'));var BPromise=require(_0xf807('0x5'));var Mustache=require('mustache');var util=require(_0xf807('0x6'));var path=require('path');var sox=require('sox');var csv=require('to-csv');var ejs=require(_0xf807('0x7'));var fs=require('fs');var fs_extra=require('fs-extra');var _=require('lodash');var squel=require('squel');var crypto=require(_0xf807('0x8'));var jsforce=require(_0xf807('0x9'));var deskjs=require(_0xf807('0xa'));var toCsv=require(_0xf807('0xb'));var querystring=require(_0xf807('0xc'));var Papa=require(_0xf807('0xd'));var Redis=require(_0xf807('0xe'));var authService=require(_0xf807('0xf'));var qs=require(_0xf807('0x10'));var as=require(_0xf807('0x11'));var hardwareService=require(_0xf807('0x12'));var logger=require(_0xf807('0x13'))(_0xf807('0x14'));var utils=require('../../config/utils');var config=require(_0xf807('0x15'));var licenseUtil=require(_0xf807('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x14751f,_0x170eaf){_0x170eaf=_0x170eaf||0xcc;return function(_0x83a546){if(_0x83a546){return _0x14751f[_0xf807('0x17')](_0x170eaf);}return _0x14751f[_0xf807('0x18')](_0x170eaf)[_0xf807('0x19')]();};}function respondWithResult(_0x202b1f,_0x450a79){_0x450a79=_0x450a79||0xc8;return function(_0x2dacfd){if(_0x2dacfd){return _0x202b1f[_0xf807('0x18')](_0x450a79)['json'](_0x2dacfd);}};}function respondWithFilteredResult(_0x4d49cf,_0x11c900){return function(_0x2746b0){if(_0x2746b0){var _0x124957=typeof _0x11c900[_0xf807('0x1a')]===_0xf807('0x1b')&&typeof _0x11c900['limit']===_0xf807('0x1b');var _0x4d6729=_0x2746b0[_0xf807('0x1c')];var _0x20ee13=_0x124957?0x0:_0x11c900[_0xf807('0x1a')];var _0x228a18=_0x124957?_0x2746b0[_0xf807('0x1c')]:_0x11c900[_0xf807('0x1a')]+_0x11c900[_0xf807('0x1d')];var _0x9db93d;if(_0x228a18>=_0x4d6729){_0x228a18=_0x4d6729;_0x9db93d=0xc8;}else{_0x9db93d=0xce;}_0x4d49cf[_0xf807('0x18')](_0x9db93d);return _0x4d49cf[_0xf807('0x1e')](_0xf807('0x1f'),_0x20ee13+'-'+_0x228a18+'/'+_0x4d6729)[_0xf807('0x20')](_0x2746b0);}return null;};}function patchUpdates(_0x564a82){return function(_0x5e0c01){try{jsonpatch[_0xf807('0x21')](_0x5e0c01,_0x564a82,!![]);}catch(_0x3c5185){return BPromise[_0xf807('0x22')](_0x3c5185);}return _0x5e0c01[_0xf807('0x23')]();};}function saveUpdates(_0x1b9611,_0x4f99a8){return function(_0x29c565){if(_0x29c565){return _0x29c565[_0xf807('0x24')](_0x1b9611)[_0xf807('0x25')](function(_0xfee807){return _0xfee807;});}return null;};}function removeEntity(_0x593912,_0x158200){return function(_0x51f865){if(_0x51f865){return _0x51f865[_0xf807('0x26')]()[_0xf807('0x25')](function(){var _0x1bf5f6=_0x51f865[_0xf807('0x27')]({'plain':!![]});var _0x4e7e59=_0xf807('0x28');return db['UserProfileResource']['destroy']({'where':{'type':_0x4e7e59,'resourceId':_0x1bf5f6['id']}})[_0xf807('0x25')](function(){return _0x51f865;});})[_0xf807('0x25')](function(){_0x593912[_0xf807('0x18')](0xcc)['end']();});}};}function handleEntityNotFound(_0x26da23,_0x1ee6b0){return function(_0x1b5f8f){if(!_0x1b5f8f){_0x26da23[_0xf807('0x17')](0x194);}return _0x1b5f8f;};}function handleError(_0x811395,_0x3cce86){_0x3cce86=_0x3cce86||0x1f4;return function(_0x35e882){logger[_0xf807('0x29')](_0x35e882['stack']);if(_0x35e882[_0xf807('0x2a')]){delete _0x35e882[_0xf807('0x2a')];}_0x811395[_0xf807('0x18')](_0x3cce86)[_0xf807('0x2b')](_0x35e882);};}exports[_0xf807('0x2c')]=function(_0x37ab8f,_0x539376){var _0x2afaf1={},_0x5b304b={},_0x4706ec={'count':0x0,'rows':[]};var _0x32cdd1=_[_0xf807('0x2d')](db[_0xf807('0x2e')][_0xf807('0x2f')],function(_0x198132){return{'name':_0x198132['fieldName'],'type':_0x198132[_0xf807('0x30')][_0xf807('0x31')]};});_0x5b304b['model']=_[_0xf807('0x2d')](_0x32cdd1,_0xf807('0x2a'));_0x5b304b[_0xf807('0x32')]=_[_0xf807('0x33')](_0x37ab8f[_0xf807('0x32')]);_0x5b304b[_0xf807('0x34')]=_['intersection'](_0x5b304b[_0xf807('0x35')],_0x5b304b[_0xf807('0x32')]);_0x2afaf1[_0xf807('0x36')]=_[_0xf807('0x37')](_0x5b304b[_0xf807('0x35')],qs[_0xf807('0x38')](_0x37ab8f[_0xf807('0x32')][_0xf807('0x38')]));_0x2afaf1[_0xf807('0x36')]=_0x2afaf1[_0xf807('0x36')][_0xf807('0x39')]?_0x2afaf1[_0xf807('0x36')]:_0x5b304b[_0xf807('0x35')];if(!_0x37ab8f[_0xf807('0x32')][_0xf807('0x3a')](_0xf807('0x3b'))){_0x2afaf1[_0xf807('0x1d')]=qs[_0xf807('0x1d')](_0x37ab8f[_0xf807('0x32')][_0xf807('0x1d')]);_0x2afaf1['offset']=qs[_0xf807('0x1a')](_0x37ab8f[_0xf807('0x32')][_0xf807('0x1a')]);}_0x2afaf1[_0xf807('0x3c')]=qs[_0xf807('0x3d')](_0x37ab8f[_0xf807('0x32')][_0xf807('0x3d')]);_0x2afaf1[_0xf807('0x3e')]=qs[_0xf807('0x34')](_[_0xf807('0x3f')](_0x37ab8f[_0xf807('0x32')],_0x5b304b[_0xf807('0x34')]),_0x32cdd1);if(_0x37ab8f[_0xf807('0x32')][_0xf807('0x40')]){_0x2afaf1['where']=_[_0xf807('0x41')](_0x2afaf1[_0xf807('0x3e')],{'$or':_[_0xf807('0x2d')](_0x32cdd1,function(_0x20f898){if(_0x20f898['type']!=='VIRTUAL'){var _0x529d74={};_0x529d74[_0x20f898[_0xf807('0x2a')]]={'$like':'%'+_0x37ab8f[_0xf807('0x32')][_0xf807('0x40')]+'%'};return _0x529d74;}})});}_0x2afaf1=_[_0xf807('0x41')]({},_0x2afaf1,_0x37ab8f[_0xf807('0x42')]);var _0x484702={'where':_0x2afaf1['where']};return db[_0xf807('0x2e')][_0xf807('0x1c')](_0x484702)[_0xf807('0x25')](function(_0x51656a){_0x4706ec['count']=_0x51656a;if(_0x37ab8f[_0xf807('0x32')][_0xf807('0x43')]){_0x2afaf1[_0xf807('0x44')]=[{'all':!![]}];}return db[_0xf807('0x2e')]['findAll'](_0x2afaf1);})[_0xf807('0x25')](function(_0x38e226){_0x4706ec['rows']=_0x38e226;return _0x4706ec;})[_0xf807('0x25')](respondWithFilteredResult(_0x539376,_0x2afaf1))[_0xf807('0x45')](handleError(_0x539376,null));};exports['show']=function(_0x3fbdb7,_0x4e2102){var _0x59a6a0={'raw':!![],'where':{'id':_0x3fbdb7[_0xf807('0x46')]['id']}},_0x4e9718={};_0x4e9718[_0xf807('0x35')]=_[_0xf807('0x33')](db[_0xf807('0x2e')]['rawAttributes']);_0x4e9718[_0xf807('0x32')]=_[_0xf807('0x33')](_0x3fbdb7[_0xf807('0x32')]);_0x4e9718[_0xf807('0x34')]=_[_0xf807('0x37')](_0x4e9718['model'],_0x4e9718[_0xf807('0x32')]);_0x59a6a0[_0xf807('0x36')]=_['intersection'](_0x4e9718[_0xf807('0x35')],qs['fields'](_0x3fbdb7[_0xf807('0x32')]['fields']));_0x59a6a0[_0xf807('0x36')]=_0x59a6a0[_0xf807('0x36')]['length']?_0x59a6a0[_0xf807('0x36')]:_0x4e9718[_0xf807('0x35')];if(_0x3fbdb7[_0xf807('0x32')][_0xf807('0x43')]){_0x59a6a0[_0xf807('0x44')]=[{'all':!![]}];}_0x59a6a0=_[_0xf807('0x41')]({},_0x59a6a0,_0x3fbdb7[_0xf807('0x42')]);return db[_0xf807('0x2e')]['find'](_0x59a6a0)['then'](handleEntityNotFound(_0x4e2102,null))['then'](respondWithResult(_0x4e2102,null))[_0xf807('0x45')](handleError(_0x4e2102,null));};exports[_0xf807('0x47')]=function(_0x30ff4a,_0x91118a,_0x593645){return db[_0xf807('0x2e')]['find']({'where':{'id':_0x30ff4a[_0xf807('0x46')]['id']},'attributes':[_0xf807('0x2a'),_0xf807('0x48'),'converted_format'],'raw':!![]})[_0xf807('0x25')](handleEntityNotFound(_0x91118a,null))[_0xf807('0x25')](function(_0x111adc){if(_0x111adc){if(!fs[_0xf807('0x49')](path[_0xf807('0x4a')](config[_0xf807('0x4b')],_0xf807('0x4c'),_0x111adc[_0xf807('0x48')]+'.'+_0x111adc[_0xf807('0x4d')]))){throw new db['Sequelize'][(_0xf807('0x4e'))](_0xf807('0x4f'));}return _0x91118a[_0xf807('0x47')](path[_0xf807('0x4a')](config[_0xf807('0x4b')],_0xf807('0x4c'),util['format'](_0xf807('0x50'),_0x111adc[_0xf807('0x48')],_0x111adc[_0xf807('0x4d')])),util[_0xf807('0x51')](_0xf807('0x50'),_0x111adc[_0xf807('0x2a')],_0x111adc[_0xf807('0x4d')]));}return null;})[_0xf807('0x45')](handleError(_0x91118a,null));};exports[_0xf807('0x52')]=function(_0x23d349,_0x33eb6b,_0x264971){var _0x1b2d4e={'name':path[_0xf807('0x53')](_0x23d349[_0xf807('0x54')][_0xf807('0x55')],path['extname'](_0x23d349[_0xf807('0x54')][_0xf807('0x55')])),'save_name':path['basename'](_0x23d349[_0xf807('0x54')][_0xf807('0x56')],path[_0xf807('0x57')](_0x23d349[_0xf807('0x54')]['filename'])),'original_format':path[_0xf807('0x57')](_0x23d349['file'][_0xf807('0x55')])['substring'](0x1),'converted_format':_0xf807('0x58')};var _0x1582c5=path['join'](config[_0xf807('0x4b')],_0xf807('0x59'),_0x23d349['file'][_0xf807('0x56')]);var _0x36100e=path[_0xf807('0x4a')](config[_0xf807('0x4b')],_0xf807('0x4c'),util['format'](_0xf807('0x50'),path[_0xf807('0x53')](_0x23d349['file'][_0xf807('0x56')],path['extname'](_0x23d349[_0xf807('0x54')]['filename'])),_0x1b2d4e[_0xf807('0x4d')]));logger['info'](_0xf807('0x5a'),_0x1582c5);logger['info']('Sound\x20upload\x20destination:\x20%s',_0x36100e);try{fs[_0xf807('0x5b')](_0x1582c5,parseInt('0777',0x8));var _0x307618=sox[_0xf807('0x5c')](_0x1582c5,_0x36100e,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x307618['on']('error',function(_0x4022c2){fs['unlinkSync'](_0x1582c5);_0x33eb6b[_0xf807('0x18')](0x1f4)[_0xf807('0x2b')](_0x4022c2);});_0x307618['on'](_0xf807('0x5d'),function(_0xf182f2){_0x1b2d4e[_0xf807('0x5e')]=isNaN(_0xf182f2[_0xf807('0x5f')])?null:_0xf182f2[_0xf807('0x5f')]*0x3e8;_0x1b2d4e[_0xf807('0x60')]=isNaN(_0xf182f2[_0xf807('0x61')])?null:_0xf182f2[_0xf807('0x61')];_0x1b2d4e[_0xf807('0x62')]=isNaN(_0xf182f2[_0xf807('0x63')])?null:_0xf182f2[_0xf807('0x63')];_0x1b2d4e['original_bitRate']=isNaN(_0xf182f2['bitRate'])?null:_0xf182f2[_0xf807('0x64')];_0x1b2d4e[_0xf807('0x65')]=isNaN(_0xf182f2[_0xf807('0x66')])?null:_0xf182f2[_0xf807('0x66')];});_0x307618['on'](_0xf807('0x67'),function(_0xccb204){_0x1b2d4e[_0xf807('0x68')]=isNaN(_0xccb204['duration'])?null:_0xccb204[_0xf807('0x5f')]*0x3e8;_0x1b2d4e[_0xf807('0x69')]=isNaN(_0xccb204[_0xf807('0x61')])?null:_0xccb204[_0xf807('0x61')];_0x1b2d4e[_0xf807('0x6a')]=isNaN(_0xccb204['channelCount'])?null:_0xccb204[_0xf807('0x63')];_0x1b2d4e[_0xf807('0x6b')]=isNaN(_0xccb204[_0xf807('0x64')])?null:_0xccb204[_0xf807('0x64')];_0x1b2d4e[_0xf807('0x6c')]=isNaN(_0xccb204[_0xf807('0x66')])?null:_0xccb204[_0xf807('0x66')];});_0x307618['on'](_0xf807('0x19'),function(){fs['chmodSync'](_0x36100e,parseInt(_0xf807('0x6d'),0x8));return db[_0xf807('0x2e')][_0xf807('0x52')](_0x1b2d4e)[_0xf807('0x25')](function(_0x239c7b){var _0x58dc58={'role':_0x23d349['body'][_0xf807('0x6e')],'userProfileId':_0x23d349['body'][_0xf807('0x6f')]};if(!_0x58dc58)throw new Error(_0xf807('0x70'));if(_0x58dc58[_0xf807('0x6e')]==='user'){var _0x451804=_0x239c7b[_0xf807('0x27')]({'plain':!![]});return db[_0xf807('0x71')]['find']({'where':{'name':_0xf807('0x28'),'userProfileId':_0x58dc58[_0xf807('0x6f')]},'raw':!![]})[_0xf807('0x25')](function(_0x3fb9f1){if(_0x3fb9f1&&_0x3fb9f1[_0xf807('0x72')]===0x0){return db[_0xf807('0x73')][_0xf807('0x52')]({'name':_0x451804[_0xf807('0x2a')],'resourceId':_0x451804['id'],'type':_0x3fb9f1[_0xf807('0x2a')],'sectionId':_0x3fb9f1['id']},{})[_0xf807('0x25')](function(){return _0x239c7b;});}else{return _0x239c7b;}})[_0xf807('0x45')](function(_0xfed184){logger[_0xf807('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0xfed184);throw _0xfed184;});}return _0x239c7b;})[_0xf807('0x25')](respondWithResult(_0x33eb6b,null))[_0xf807('0x45')](handleError(_0x33eb6b,null));});_0x307618[_0xf807('0x74')]();}catch(_0x6741db){_0x33eb6b['status'](0x1f4)[_0xf807('0x2b')](_0x6741db);}};exports['update']=function(_0x56d147,_0x8a220b,_0x2a4427){return db['Sound'][_0xf807('0x75')]({'where':{'id':_0x56d147['params']['id']}})[_0xf807('0x25')](handleEntityNotFound(_0x8a220b,null))['then'](saveUpdates(_[_0xf807('0x3f')](_0x56d147['body'],[_0xf807('0x2a'),_0xf807('0x76')]),null))['then'](respondWithResult(_0x8a220b,null))[_0xf807('0x45')](handleError(_0x8a220b,null));};exports[_0xf807('0x77')]=function(_0x2b1865,_0x41dba5,_0x338908){return db['Sound'][_0xf807('0x75')]({'where':{'id':_0x2b1865[_0xf807('0x46')]['id']}})[_0xf807('0x25')](function(_0x1dbf75){if(_0x1dbf75){return _0x1dbf75[_0xf807('0x26')]();}})[_0xf807('0x25')](function(_0x2435a6){var _0x14e236=path['join'](config[_0xf807('0x4b')],_0xf807('0x59'),util[_0xf807('0x51')](_0xf807('0x50'),_0x2435a6[_0xf807('0x48')],_0x2435a6['original_format']));var _0x1a8698=path[_0xf807('0x4a')](config[_0xf807('0x4b')],_0xf807('0x4c'),util[_0xf807('0x51')]('%s.%s',_0x2435a6['save_name'],_0x2435a6[_0xf807('0x4d')]));fs[_0xf807('0x78')](_0x14e236);fs[_0xf807('0x78')](_0x1a8698);return _0x2435a6;})['then'](function(_0x4a4b34){if(_0x4a4b34){return _0x41dba5[_0xf807('0x18')](0xcc)[_0xf807('0x19')]();}})['then'](handleEntityNotFound(_0x41dba5,null))['catch'](handleError(_0x41dba5,null));};