Built motion from commit (unavailable).|2.4.3
[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 _0xf017=['sort','where','pick','filter','merge','map','type','key','VIRTUAL','field','include','findAll','rows','catch','show','options','find','download','params','name','save_name','converted_format','existsSync','join','root','server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','%s.%s','format','create','basename','originalname','file','filename','extname','substring','wav','info','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','unlinkSync','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','autoAssociation','start','description','delete','server/files/sounds/original','original_format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','querystring','../../config/license/hardware','../../config/logger','api','../../mysqldb','status','json','count','offset','limit','Content-Range','update','destroy','then','get','Sounds','UserProfileResource','end','sendStatus','error','stack','send','index','Sound','rawAttributes','model','keys','query','filters','attributes','intersection','fields','length','hasOwnProperty','order'];(function(_0x2ed7d1,_0x363741){var _0x38ae3c=function(_0x4a1b42){while(--_0x4a1b42){_0x2ed7d1['push'](_0x2ed7d1['shift']());}};_0x38ae3c(++_0x363741);}(_0xf017,0xc2));var _0x7f01=function(_0x92c215,_0x42c7c8){_0x92c215=_0x92c215-0x0;var _0x370137=_0xf017[_0x92c215];return _0x370137;};'use strict';var pdf=require('html-pdf');var emlformat=require('eml-format');var rimraf=require(_0x7f01('0x0'));var zipdir=require(_0x7f01('0x1'));var jsonpatch=require(_0x7f01('0x2'));var rp=require(_0x7f01('0x3'));var moment=require(_0x7f01('0x4'));var BPromise=require(_0x7f01('0x5'));var Mustache=require('mustache');var util=require(_0x7f01('0x6'));var path=require(_0x7f01('0x7'));var sox=require(_0x7f01('0x8'));var csv=require(_0x7f01('0x9'));var ejs=require(_0x7f01('0xa'));var fs=require('fs');var _=require(_0x7f01('0xb'));var squel=require(_0x7f01('0xc'));var crypto=require(_0x7f01('0xd'));var jsforce=require(_0x7f01('0xe'));var deskjs=require('desk.js');var toCsv=require(_0x7f01('0x9'));var querystring=require(_0x7f01('0xf'));var Papa=require('papaparse');var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var hardwareService=require(_0x7f01('0x10'));var logger=require(_0x7f01('0x11'))(_0x7f01('0x12'));var utils=require('../../config/utils');var config=require('../../config/environment');var db=require(_0x7f01('0x13'))['db'];function respondWithStatusCode(_0x587d7d,_0x217a3b){_0x217a3b=_0x217a3b||0xcc;return function(_0x301071){if(_0x301071){return _0x587d7d['sendStatus'](_0x217a3b);}return _0x587d7d['status'](_0x217a3b)['end']();};}function respondWithResult(_0x2b2c90,_0x3d3e87){_0x3d3e87=_0x3d3e87||0xc8;return function(_0x4edc9e){if(_0x4edc9e){return _0x2b2c90[_0x7f01('0x14')](_0x3d3e87)[_0x7f01('0x15')](_0x4edc9e);}};}function respondWithFilteredResult(_0x143794,_0x46f4dd){return function(_0x13163b){if(_0x13163b){var _0x524a51=_0x13163b[_0x7f01('0x16')],_0x5daf96=_0x46f4dd[_0x7f01('0x17')],_0x50b92f=_0x46f4dd['offset']+_0x46f4dd[_0x7f01('0x18')],_0x1ed82e;if(_0x50b92f>=_0x524a51){_0x50b92f=_0x524a51;_0x1ed82e=0xc8;}else{_0x1ed82e=0xce;}_0x143794[_0x7f01('0x14')](_0x1ed82e);return _0x143794['set'](_0x7f01('0x19'),_0x5daf96+'-'+_0x50b92f+'/'+_0x524a51)[_0x7f01('0x15')](_0x13163b);}return null;};}function patchUpdates(_0x23eeaf){return function(_0x466f0a){try{jsonpatch['apply'](_0x466f0a,_0x23eeaf,!![]);}catch(_0x19d45f){return BPromise['reject'](_0x19d45f);}return _0x466f0a['save']();};}function saveUpdates(_0x530cd7,_0x252d21){return function(_0x5341c3){if(_0x5341c3){return _0x5341c3[_0x7f01('0x1a')](_0x530cd7)['then'](function(_0x1ad0a7){return _0x1ad0a7;});}return null;};}function removeEntity(_0x477bbc,_0x4ec66e){return function(_0x55168e){if(_0x55168e){return _0x55168e[_0x7f01('0x1b')]()[_0x7f01('0x1c')](function(){var _0x470f98=_0x55168e[_0x7f01('0x1d')]({'plain':!![]});var _0x393f97=_0x7f01('0x1e');return db[_0x7f01('0x1f')][_0x7f01('0x1b')]({'where':{'type':_0x393f97,'resourceId':_0x470f98['id']}})[_0x7f01('0x1c')](function(){return _0x55168e;});})[_0x7f01('0x1c')](function(){_0x477bbc['status'](0xcc)[_0x7f01('0x20')]();});}};}function handleEntityNotFound(_0x295a62,_0x4195bf){return function(_0x51bf4b){if(!_0x51bf4b){_0x295a62[_0x7f01('0x21')](0x194);}return _0x51bf4b;};}function handleError(_0x1bb51f,_0x4d652e){_0x4d652e=_0x4d652e||0x1f4;return function(_0x2aef1d){logger[_0x7f01('0x22')](_0x2aef1d[_0x7f01('0x23')]);if(_0x2aef1d['name']){delete _0x2aef1d['name'];}_0x1bb51f[_0x7f01('0x14')](_0x4d652e)[_0x7f01('0x24')](_0x2aef1d);};}exports[_0x7f01('0x25')]=function(_0x52fd07,_0x5c365a){var _0xd6c8d5={},_0x11ad0b={},_0x3d422c={'count':0x0,'rows':[]};var _0x3171e0=db[_0x7f01('0x26')][_0x7f01('0x27')];_0x11ad0b[_0x7f01('0x28')]=_[_0x7f01('0x29')](_0x3171e0);_0x11ad0b[_0x7f01('0x2a')]=_[_0x7f01('0x29')](_0x52fd07[_0x7f01('0x2a')]);_0x11ad0b[_0x7f01('0x2b')]=_['intersection'](_0x11ad0b[_0x7f01('0x28')],_0x11ad0b[_0x7f01('0x2a')]);_0xd6c8d5[_0x7f01('0x2c')]=_[_0x7f01('0x2d')](_0x11ad0b[_0x7f01('0x28')],qs[_0x7f01('0x2e')](_0x52fd07['query'][_0x7f01('0x2e')]));_0xd6c8d5[_0x7f01('0x2c')]=_0xd6c8d5[_0x7f01('0x2c')][_0x7f01('0x2f')]?_0xd6c8d5['attributes']:_0x11ad0b[_0x7f01('0x28')];if(!_0x52fd07['query'][_0x7f01('0x30')]('nolimit')){_0xd6c8d5[_0x7f01('0x18')]=qs[_0x7f01('0x18')](_0x52fd07[_0x7f01('0x2a')][_0x7f01('0x18')]);_0xd6c8d5[_0x7f01('0x17')]=qs[_0x7f01('0x17')](_0x52fd07['query'][_0x7f01('0x17')]);}_0xd6c8d5[_0x7f01('0x31')]=qs[_0x7f01('0x32')](_0x52fd07[_0x7f01('0x2a')][_0x7f01('0x32')]);_0xd6c8d5[_0x7f01('0x33')]=qs['filters'](_[_0x7f01('0x34')](_0x52fd07[_0x7f01('0x2a')],_0x11ad0b['filters']));if(_0x52fd07[_0x7f01('0x2a')][_0x7f01('0x35')]){_0xd6c8d5['where']=_[_0x7f01('0x36')](_0xd6c8d5[_0x7f01('0x33')],{'$or':_[_0x7f01('0x37')](_0x3171e0,function(_0x325a29){if(_0x325a29[_0x7f01('0x38')][_0x7f01('0x39')]!==_0x7f01('0x3a')){var _0x744f5f={};_0x744f5f[_0x325a29[_0x7f01('0x3b')]]={'$like':'%'+_0x52fd07[_0x7f01('0x2a')]['filter']+'%'};return _0x744f5f;}})});}_0xd6c8d5=_[_0x7f01('0x36')]({},_0xd6c8d5,_0x52fd07['options']);var _0x1b901a={'where':_0xd6c8d5[_0x7f01('0x33')]};return db[_0x7f01('0x26')][_0x7f01('0x16')](_0x1b901a)['then'](function(_0xef1db1){_0x3d422c[_0x7f01('0x16')]=_0xef1db1;if(_0x52fd07[_0x7f01('0x2a')]['includeAll']){_0xd6c8d5[_0x7f01('0x3c')]=[{'all':!![]}];}return db[_0x7f01('0x26')][_0x7f01('0x3d')](_0xd6c8d5);})[_0x7f01('0x1c')](function(_0x478c68){_0x3d422c[_0x7f01('0x3e')]=_0x478c68;return _0x3d422c;})['then'](respondWithFilteredResult(_0x5c365a,_0xd6c8d5))[_0x7f01('0x3f')](handleError(_0x5c365a,null));};exports[_0x7f01('0x40')]=function(_0x33425f,_0x52b903){var _0x389ea4={'raw':!![],'where':{'id':_0x33425f['params']['id']}},_0x217f2b={};_0x217f2b[_0x7f01('0x28')]=_[_0x7f01('0x29')](db[_0x7f01('0x26')][_0x7f01('0x27')]);_0x217f2b['query']=_[_0x7f01('0x29')](_0x33425f['query']);_0x217f2b[_0x7f01('0x2b')]=_[_0x7f01('0x2d')](_0x217f2b[_0x7f01('0x28')],_0x217f2b[_0x7f01('0x2a')]);_0x389ea4[_0x7f01('0x2c')]=_[_0x7f01('0x2d')](_0x217f2b[_0x7f01('0x28')],qs[_0x7f01('0x2e')](_0x33425f['query'][_0x7f01('0x2e')]));_0x389ea4['attributes']=_0x389ea4[_0x7f01('0x2c')][_0x7f01('0x2f')]?_0x389ea4[_0x7f01('0x2c')]:_0x217f2b['model'];if(_0x33425f['query']['includeAll']){_0x389ea4[_0x7f01('0x3c')]=[{'all':!![]}];}_0x389ea4=_[_0x7f01('0x36')]({},_0x389ea4,_0x33425f[_0x7f01('0x41')]);return db[_0x7f01('0x26')][_0x7f01('0x42')](_0x389ea4)[_0x7f01('0x1c')](handleEntityNotFound(_0x52b903,null))['then'](respondWithResult(_0x52b903,null))[_0x7f01('0x3f')](handleError(_0x52b903,null));};exports[_0x7f01('0x43')]=function(_0x2b7f0d,_0xd07e00,_0x57a8d6){return db['Sound']['find']({'where':{'id':_0x2b7f0d[_0x7f01('0x44')]['id']},'attributes':[_0x7f01('0x45'),_0x7f01('0x46'),_0x7f01('0x47')],'raw':!![]})[_0x7f01('0x1c')](handleEntityNotFound(_0xd07e00,null))[_0x7f01('0x1c')](function(_0x6fac39){if(_0x6fac39){if(!fs[_0x7f01('0x48')](path[_0x7f01('0x49')](config[_0x7f01('0x4a')],_0x7f01('0x4b'),_0x6fac39[_0x7f01('0x46')]+'.'+_0x6fac39[_0x7f01('0x47')]))){throw new db[(_0x7f01('0x4c'))][(_0x7f01('0x4d'))](_0x7f01('0x4e'));}return _0xd07e00[_0x7f01('0x43')](path[_0x7f01('0x49')](config[_0x7f01('0x4a')],_0x7f01('0x4b'),util['format'](_0x7f01('0x4f'),_0x6fac39[_0x7f01('0x46')],_0x6fac39['converted_format'])),util[_0x7f01('0x50')](_0x7f01('0x4f'),_0x6fac39[_0x7f01('0x45')],_0x6fac39[_0x7f01('0x47')]));}return null;})[_0x7f01('0x3f')](handleError(_0xd07e00,null));};exports[_0x7f01('0x51')]=function(_0x27d97a,_0x350d10,_0x4bf914){var _0x5091af={'name':path[_0x7f01('0x52')](_0x27d97a['file'][_0x7f01('0x53')],path['extname'](_0x27d97a['file'][_0x7f01('0x53')])),'save_name':path[_0x7f01('0x52')](_0x27d97a[_0x7f01('0x54')][_0x7f01('0x55')],path['extname'](_0x27d97a['file'][_0x7f01('0x55')])),'original_format':path[_0x7f01('0x56')](_0x27d97a[_0x7f01('0x54')][_0x7f01('0x53')])[_0x7f01('0x57')](0x1),'converted_format':_0x7f01('0x58')};var _0x3d8223=path[_0x7f01('0x49')](config[_0x7f01('0x4a')],'server/files/sounds/original',_0x27d97a['file'][_0x7f01('0x55')]);var _0x23c43c=path[_0x7f01('0x49')](config[_0x7f01('0x4a')],_0x7f01('0x4b'),util[_0x7f01('0x50')](_0x7f01('0x4f'),path['basename'](_0x27d97a[_0x7f01('0x54')][_0x7f01('0x55')],path[_0x7f01('0x56')](_0x27d97a['file'][_0x7f01('0x55')])),_0x5091af['converted_format']));logger[_0x7f01('0x59')](_0x7f01('0x5a'),_0x3d8223);logger[_0x7f01('0x59')](_0x7f01('0x5b'),_0x23c43c);try{fs[_0x7f01('0x5c')](_0x3d8223,parseInt(_0x7f01('0x5d'),0x8));var _0x1d4d47=sox[_0x7f01('0x5e')](_0x3d8223,_0x23c43c,{'sampleRate':0x1f40,'format':_0x7f01('0x58'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x1d4d47['on']('error',function(_0x55c64c){fs[_0x7f01('0x5f')](_0x3d8223);_0x350d10[_0x7f01('0x14')](0x1f4)[_0x7f01('0x24')](_0x55c64c);});_0x1d4d47['on'](_0x7f01('0x60'),function(_0x13a5e1){_0x5091af['original_duration']=isNaN(_0x13a5e1[_0x7f01('0x61')])?null:_0x13a5e1[_0x7f01('0x61')]*0x3e8;_0x5091af[_0x7f01('0x62')]=isNaN(_0x13a5e1['sampleCount'])?null:_0x13a5e1[_0x7f01('0x63')];_0x5091af[_0x7f01('0x64')]=isNaN(_0x13a5e1[_0x7f01('0x65')])?null:_0x13a5e1[_0x7f01('0x65')];_0x5091af[_0x7f01('0x66')]=isNaN(_0x13a5e1[_0x7f01('0x67')])?null:_0x13a5e1[_0x7f01('0x67')];_0x5091af[_0x7f01('0x68')]=isNaN(_0x13a5e1[_0x7f01('0x69')])?null:_0x13a5e1[_0x7f01('0x69')];});_0x1d4d47['on'](_0x7f01('0x6a'),function(_0xa244a5){_0x5091af[_0x7f01('0x6b')]=isNaN(_0xa244a5[_0x7f01('0x61')])?null:_0xa244a5[_0x7f01('0x61')]*0x3e8;_0x5091af['converted_sampleCount']=isNaN(_0xa244a5[_0x7f01('0x63')])?null:_0xa244a5[_0x7f01('0x63')];_0x5091af['converted_channelCount']=isNaN(_0xa244a5[_0x7f01('0x65')])?null:_0xa244a5[_0x7f01('0x65')];_0x5091af[_0x7f01('0x6c')]=isNaN(_0xa244a5[_0x7f01('0x67')])?null:_0xa244a5['bitRate'];_0x5091af[_0x7f01('0x6d')]=isNaN(_0xa244a5[_0x7f01('0x69')])?null:_0xa244a5[_0x7f01('0x69')];});_0x1d4d47['on'](_0x7f01('0x20'),function(){fs[_0x7f01('0x5c')](_0x23c43c,parseInt(_0x7f01('0x5d'),0x8));return db[_0x7f01('0x26')]['create'](_0x5091af)[_0x7f01('0x1c')](function(_0x3b8fe5){var _0x1c3bed={'role':_0x27d97a[_0x7f01('0x6e')][_0x7f01('0x6f')],'userProfileId':_0x27d97a['body'][_0x7f01('0x70')]};if(!_0x1c3bed)throw new Error(_0x7f01('0x71'));if(_0x1c3bed[_0x7f01('0x6f')]===_0x7f01('0x72')){var _0x5c6ff8=_0x3b8fe5[_0x7f01('0x1d')]({'plain':!![]});return db[_0x7f01('0x73')][_0x7f01('0x42')]({'where':{'name':'Sounds','userProfileId':_0x1c3bed[_0x7f01('0x70')]},'raw':!![]})['then'](function(_0x1546ad){if(_0x1546ad&&_0x1546ad[_0x7f01('0x74')]===0x0){return db[_0x7f01('0x1f')][_0x7f01('0x51')]({'name':_0x5c6ff8['name'],'resourceId':_0x5c6ff8['id'],'type':_0x1546ad[_0x7f01('0x45')],'sectionId':_0x1546ad['id']},{})[_0x7f01('0x1c')](function(){return _0x3b8fe5;});}else{return _0x3b8fe5;}})[_0x7f01('0x3f')](function(_0x443981){logger[_0x7f01('0x22')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x443981);throw _0x443981;});}return _0x3b8fe5;})[_0x7f01('0x1c')](respondWithResult(_0x350d10,null))[_0x7f01('0x3f')](handleError(_0x350d10,null));});_0x1d4d47[_0x7f01('0x75')]();}catch(_0x1e0644){_0x350d10[_0x7f01('0x14')](0x1f4)[_0x7f01('0x24')](_0x1e0644);}};exports[_0x7f01('0x1a')]=function(_0x501427,_0x2bf1f0,_0x3f4046){return db[_0x7f01('0x26')][_0x7f01('0x42')]({'where':{'id':_0x501427[_0x7f01('0x44')]['id']}})[_0x7f01('0x1c')](handleEntityNotFound(_0x2bf1f0,null))['then'](saveUpdates(_[_0x7f01('0x34')](_0x501427[_0x7f01('0x6e')],[_0x7f01('0x45'),_0x7f01('0x76')]),null))[_0x7f01('0x1c')](respondWithResult(_0x2bf1f0,null))[_0x7f01('0x3f')](handleError(_0x2bf1f0,null));};exports[_0x7f01('0x77')]=function(_0x24cb93,_0x27d29c,_0x3d6857){return db[_0x7f01('0x26')]['find']({'where':{'id':_0x24cb93[_0x7f01('0x44')]['id']}})['then'](function(_0x391943){if(_0x391943){return _0x391943[_0x7f01('0x1b')]();}})['then'](function(_0x3bfd56){var _0x44ca25=path['join'](config[_0x7f01('0x4a')],_0x7f01('0x78'),util[_0x7f01('0x50')](_0x7f01('0x4f'),_0x3bfd56[_0x7f01('0x46')],_0x3bfd56[_0x7f01('0x79')]));var _0x48ebc6=path[_0x7f01('0x49')](config[_0x7f01('0x4a')],_0x7f01('0x4b'),util[_0x7f01('0x50')](_0x7f01('0x4f'),_0x3bfd56['save_name'],_0x3bfd56[_0x7f01('0x47')]));fs[_0x7f01('0x5f')](_0x44ca25);fs['unlinkSync'](_0x48ebc6);return _0x3bfd56;})['then'](function(_0x649978){if(_0x649978){return _0x27d29c[_0x7f01('0x14')](0xcc)['end']();}})[_0x7f01('0x1c')](handleEntityNotFound(_0x27d29c,null))[_0x7f01('0x3f')](handleError(_0x27d29c,null));};