fb4dea7f600b9bdf3972e2a4a5a4990465dfaea3
[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 _0x53e3=['body','role','user','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','%s.%s','original_format','eml-format','zip-dir','request-promise','moment','bluebird','mustache','util','sox','ejs','lodash','squel','jsforce','desk.js','to-csv','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../mysqldb','sendStatus','status','end','json','count','offset','set','Content-Range','apply','save','update','then','destroy','get','Sounds','UserProfileResource','error','stack','name','send','index','map','Sound','rawAttributes','fieldName','type','model','query','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','catch','show','params','keys','find','download','save_name','existsSync','join','Sequelize','ValidationError','Sound\x20not\x20found','server/files/sounds/converted','format','converted_format','create','file','originalname','basename','filename','extname','wav','root','server/files/sounds/original','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','sampleRate','dest','converted_duration','converted_channelCount','converted_sampleRate'];(function(_0x309378,_0x51581c){var _0xad3422=function(_0x2b6c0e){while(--_0x2b6c0e){_0x309378['push'](_0x309378['shift']());}};_0xad3422(++_0x51581c);}(_0x53e3,0x82));var _0x353e=function(_0x246490,_0x529de9){_0x246490=_0x246490-0x0;var _0xff2551=_0x53e3[_0x246490];return _0xff2551;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x353e('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x353e('0x1'));var jsonpatch=require('fast-json-patch');var rp=require(_0x353e('0x2'));var moment=require(_0x353e('0x3'));var BPromise=require(_0x353e('0x4'));var Mustache=require(_0x353e('0x5'));var util=require(_0x353e('0x6'));var path=require('path');var sox=require(_0x353e('0x7'));var csv=require('to-csv');var ejs=require(_0x353e('0x8'));var fs=require('fs');var _=require(_0x353e('0x9'));var squel=require(_0x353e('0xa'));var crypto=require('crypto');var jsforce=require(_0x353e('0xb'));var deskjs=require(_0x353e('0xc'));var toCsv=require(_0x353e('0xd'));var querystring=require(_0x353e('0xe'));var Papa=require(_0x353e('0xf'));var Redis=require('ioredis');var authService=require(_0x353e('0x10'));var qs=require(_0x353e('0x11'));var as=require(_0x353e('0x12'));var hardwareService=require(_0x353e('0x13'));var logger=require('../../config/logger')(_0x353e('0x14'));var utils=require('../../config/utils');var config=require(_0x353e('0x15'));var db=require(_0x353e('0x16'))['db'];function respondWithStatusCode(_0x30b0ca,_0x1bb707){_0x1bb707=_0x1bb707||0xcc;return function(_0x4f3de4){if(_0x4f3de4){return _0x30b0ca[_0x353e('0x17')](_0x1bb707);}return _0x30b0ca[_0x353e('0x18')](_0x1bb707)[_0x353e('0x19')]();};}function respondWithResult(_0x201a90,_0x434031){_0x434031=_0x434031||0xc8;return function(_0x5b0c19){if(_0x5b0c19){return _0x201a90['status'](_0x434031)[_0x353e('0x1a')](_0x5b0c19);}};}function respondWithFilteredResult(_0x4ebe88,_0x1c21cb){return function(_0x268edb){if(_0x268edb){var _0x51c05e=_0x268edb[_0x353e('0x1b')],_0xf12589=_0x1c21cb[_0x353e('0x1c')],_0x5c4825=_0x1c21cb[_0x353e('0x1c')]+_0x1c21cb['limit'],_0x5ac5df;if(_0x5c4825>=_0x51c05e){_0x5c4825=_0x51c05e;_0x5ac5df=0xc8;}else{_0x5ac5df=0xce;}_0x4ebe88['status'](_0x5ac5df);return _0x4ebe88[_0x353e('0x1d')](_0x353e('0x1e'),_0xf12589+'-'+_0x5c4825+'/'+_0x51c05e)[_0x353e('0x1a')](_0x268edb);}return null;};}function patchUpdates(_0x47f0f7){return function(_0x538f44){try{jsonpatch[_0x353e('0x1f')](_0x538f44,_0x47f0f7,!![]);}catch(_0x299b88){return BPromise['reject'](_0x299b88);}return _0x538f44[_0x353e('0x20')]();};}function saveUpdates(_0x31e4ad,_0x5a67e7){return function(_0x4be1dc){if(_0x4be1dc){return _0x4be1dc[_0x353e('0x21')](_0x31e4ad)[_0x353e('0x22')](function(_0x54e62f){return _0x54e62f;});}return null;};}function removeEntity(_0x54dcd3,_0x17a7fe){return function(_0x48189e){if(_0x48189e){return _0x48189e[_0x353e('0x23')]()['then'](function(){var _0x348dba=_0x48189e[_0x353e('0x24')]({'plain':!![]});var _0x1b3734=_0x353e('0x25');return db[_0x353e('0x26')]['destroy']({'where':{'type':_0x1b3734,'resourceId':_0x348dba['id']}})[_0x353e('0x22')](function(){return _0x48189e;});})[_0x353e('0x22')](function(){_0x54dcd3[_0x353e('0x18')](0xcc)[_0x353e('0x19')]();});}};}function handleEntityNotFound(_0x4a8c05,_0x7bd336){return function(_0x3e4cff){if(!_0x3e4cff){_0x4a8c05[_0x353e('0x17')](0x194);}return _0x3e4cff;};}function handleError(_0x25122b,_0x6bfc87){_0x6bfc87=_0x6bfc87||0x1f4;return function(_0x4ed9f1){logger[_0x353e('0x27')](_0x4ed9f1[_0x353e('0x28')]);if(_0x4ed9f1[_0x353e('0x29')]){delete _0x4ed9f1[_0x353e('0x29')];}_0x25122b['status'](_0x6bfc87)[_0x353e('0x2a')](_0x4ed9f1);};}exports[_0x353e('0x2b')]=function(_0x744eb7,_0x38f7e9){var _0x58bcdd={},_0x37dfc0={},_0x5392e1={'count':0x0,'rows':[]};var _0x15103c=_[_0x353e('0x2c')](db[_0x353e('0x2d')][_0x353e('0x2e')],function(_0x23efee){return{'name':_0x23efee[_0x353e('0x2f')],'type':_0x23efee[_0x353e('0x30')]['key']};});_0x37dfc0[_0x353e('0x31')]=_['map'](_0x15103c,_0x353e('0x29'));_0x37dfc0[_0x353e('0x32')]=_['keys'](_0x744eb7['query']);_0x37dfc0[_0x353e('0x33')]=_[_0x353e('0x34')](_0x37dfc0[_0x353e('0x31')],_0x37dfc0['query']);_0x58bcdd[_0x353e('0x35')]=_[_0x353e('0x34')](_0x37dfc0[_0x353e('0x31')],qs[_0x353e('0x36')](_0x744eb7[_0x353e('0x32')][_0x353e('0x36')]));_0x58bcdd['attributes']=_0x58bcdd[_0x353e('0x35')][_0x353e('0x37')]?_0x58bcdd['attributes']:_0x37dfc0[_0x353e('0x31')];if(!_0x744eb7[_0x353e('0x32')][_0x353e('0x38')](_0x353e('0x39'))){_0x58bcdd['limit']=qs['limit'](_0x744eb7[_0x353e('0x32')]['limit']);_0x58bcdd[_0x353e('0x1c')]=qs[_0x353e('0x1c')](_0x744eb7[_0x353e('0x32')][_0x353e('0x1c')]);}_0x58bcdd['order']=qs[_0x353e('0x3a')](_0x744eb7[_0x353e('0x32')][_0x353e('0x3a')]);_0x58bcdd[_0x353e('0x3b')]=qs[_0x353e('0x33')](_[_0x353e('0x3c')](_0x744eb7[_0x353e('0x32')],_0x37dfc0['filters']),_0x15103c);if(_0x744eb7['query'][_0x353e('0x3d')]){_0x58bcdd[_0x353e('0x3b')]=_[_0x353e('0x3e')](_0x58bcdd[_0x353e('0x3b')],{'$or':_[_0x353e('0x2c')](_0x15103c,function(_0x1adff1){if(_0x1adff1[_0x353e('0x30')]!==_0x353e('0x3f')){var _0x50e0ca={};_0x50e0ca[_0x1adff1[_0x353e('0x29')]]={'$like':'%'+_0x744eb7[_0x353e('0x32')]['filter']+'%'};return _0x50e0ca;}})});}_0x58bcdd=_[_0x353e('0x3e')]({},_0x58bcdd,_0x744eb7[_0x353e('0x40')]);var _0x2fca0a={'where':_0x58bcdd[_0x353e('0x3b')]};return db[_0x353e('0x2d')][_0x353e('0x1b')](_0x2fca0a)[_0x353e('0x22')](function(_0xab0009){_0x5392e1['count']=_0xab0009;if(_0x744eb7[_0x353e('0x32')][_0x353e('0x41')]){_0x58bcdd['include']=[{'all':!![]}];}return db[_0x353e('0x2d')][_0x353e('0x42')](_0x58bcdd);})['then'](function(_0x21649a){_0x5392e1[_0x353e('0x43')]=_0x21649a;return _0x5392e1;})[_0x353e('0x22')](respondWithFilteredResult(_0x38f7e9,_0x58bcdd))[_0x353e('0x44')](handleError(_0x38f7e9,null));};exports[_0x353e('0x45')]=function(_0x29efe2,_0x19c7f3){var _0x3f53d1={'raw':!![],'where':{'id':_0x29efe2[_0x353e('0x46')]['id']}},_0x4f6193={};_0x4f6193[_0x353e('0x31')]=_['keys'](db[_0x353e('0x2d')][_0x353e('0x2e')]);_0x4f6193[_0x353e('0x32')]=_[_0x353e('0x47')](_0x29efe2[_0x353e('0x32')]);_0x4f6193[_0x353e('0x33')]=_['intersection'](_0x4f6193['model'],_0x4f6193['query']);_0x3f53d1['attributes']=_[_0x353e('0x34')](_0x4f6193[_0x353e('0x31')],qs['fields'](_0x29efe2[_0x353e('0x32')][_0x353e('0x36')]));_0x3f53d1[_0x353e('0x35')]=_0x3f53d1['attributes'][_0x353e('0x37')]?_0x3f53d1[_0x353e('0x35')]:_0x4f6193['model'];if(_0x29efe2[_0x353e('0x32')][_0x353e('0x41')]){_0x3f53d1['include']=[{'all':!![]}];}_0x3f53d1=_['merge']({},_0x3f53d1,_0x29efe2[_0x353e('0x40')]);return db['Sound'][_0x353e('0x48')](_0x3f53d1)['then'](handleEntityNotFound(_0x19c7f3,null))[_0x353e('0x22')](respondWithResult(_0x19c7f3,null))['catch'](handleError(_0x19c7f3,null));};exports[_0x353e('0x49')]=function(_0x42e15b,_0x20ffd3,_0x580ca9){return db['Sound']['find']({'where':{'id':_0x42e15b[_0x353e('0x46')]['id']},'attributes':['name',_0x353e('0x4a'),'converted_format'],'raw':!![]})[_0x353e('0x22')](handleEntityNotFound(_0x20ffd3,null))['then'](function(_0x9e0dd0){if(_0x9e0dd0){if(!fs[_0x353e('0x4b')](path[_0x353e('0x4c')](config['root'],'server/files/sounds/converted',_0x9e0dd0[_0x353e('0x4a')]+'.'+_0x9e0dd0['converted_format']))){throw new db[(_0x353e('0x4d'))][(_0x353e('0x4e'))](_0x353e('0x4f'));}return _0x20ffd3[_0x353e('0x49')](path[_0x353e('0x4c')](config['root'],_0x353e('0x50'),util[_0x353e('0x51')]('%s.%s',_0x9e0dd0[_0x353e('0x4a')],_0x9e0dd0[_0x353e('0x52')])),util[_0x353e('0x51')]('%s.%s',_0x9e0dd0['name'],_0x9e0dd0[_0x353e('0x52')]));}return null;})['catch'](handleError(_0x20ffd3,null));};exports[_0x353e('0x53')]=function(_0x45417f,_0xd6d2d2,_0x42c27c){var _0x53dc3b={'name':path['basename'](_0x45417f[_0x353e('0x54')][_0x353e('0x55')],path['extname'](_0x45417f[_0x353e('0x54')][_0x353e('0x55')])),'save_name':path[_0x353e('0x56')](_0x45417f[_0x353e('0x54')][_0x353e('0x57')],path[_0x353e('0x58')](_0x45417f[_0x353e('0x54')][_0x353e('0x57')])),'original_format':path['extname'](_0x45417f[_0x353e('0x54')]['originalname'])['substring'](0x1),'converted_format':_0x353e('0x59')};var _0x5182d1=path['join'](config[_0x353e('0x5a')],_0x353e('0x5b'),_0x45417f[_0x353e('0x54')]['filename']);var _0x485294=path[_0x353e('0x4c')](config[_0x353e('0x5a')],_0x353e('0x50'),util['format']('%s.%s',path[_0x353e('0x56')](_0x45417f[_0x353e('0x54')][_0x353e('0x57')],path['extname'](_0x45417f[_0x353e('0x54')][_0x353e('0x57')])),_0x53dc3b[_0x353e('0x52')]));logger[_0x353e('0x5c')](_0x353e('0x5d'),_0x5182d1);logger[_0x353e('0x5c')](_0x353e('0x5e'),_0x485294);try{fs[_0x353e('0x5f')](_0x5182d1,parseInt(_0x353e('0x60'),0x8));var _0x442d31=sox[_0x353e('0x61')](_0x5182d1,_0x485294,{'sampleRate':0x1f40,'format':_0x353e('0x59'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x442d31['on'](_0x353e('0x27'),function(_0x3df3be){fs[_0x353e('0x62')](_0x5182d1);_0xd6d2d2[_0x353e('0x18')](0x1f4)[_0x353e('0x2a')](_0x3df3be);});_0x442d31['on'](_0x353e('0x63'),function(_0xb68970){_0x53dc3b['original_duration']=isNaN(_0xb68970[_0x353e('0x64')])?null:_0xb68970['duration']*0x3e8;_0x53dc3b[_0x353e('0x65')]=isNaN(_0xb68970[_0x353e('0x66')])?null:_0xb68970[_0x353e('0x66')];_0x53dc3b[_0x353e('0x67')]=isNaN(_0xb68970[_0x353e('0x68')])?null:_0xb68970[_0x353e('0x68')];_0x53dc3b[_0x353e('0x69')]=isNaN(_0xb68970['bitRate'])?null:_0xb68970[_0x353e('0x6a')];_0x53dc3b['original_sampleRate']=isNaN(_0xb68970[_0x353e('0x6b')])?null:_0xb68970['sampleRate'];});_0x442d31['on'](_0x353e('0x6c'),function(_0x410832){_0x53dc3b[_0x353e('0x6d')]=isNaN(_0x410832[_0x353e('0x64')])?null:_0x410832['duration']*0x3e8;_0x53dc3b['converted_sampleCount']=isNaN(_0x410832['sampleCount'])?null:_0x410832[_0x353e('0x66')];_0x53dc3b[_0x353e('0x6e')]=isNaN(_0x410832[_0x353e('0x68')])?null:_0x410832[_0x353e('0x68')];_0x53dc3b['converted_bitRate']=isNaN(_0x410832[_0x353e('0x6a')])?null:_0x410832[_0x353e('0x6a')];_0x53dc3b[_0x353e('0x6f')]=isNaN(_0x410832[_0x353e('0x6b')])?null:_0x410832[_0x353e('0x6b')];});_0x442d31['on'](_0x353e('0x19'),function(){fs[_0x353e('0x5f')](_0x485294,parseInt(_0x353e('0x60'),0x8));return db['Sound'][_0x353e('0x53')](_0x53dc3b)[_0x353e('0x22')](function(_0x5d83d5){var _0x49a046={'role':_0x45417f[_0x353e('0x70')]['role'],'userProfileId':_0x45417f[_0x353e('0x70')]['userProfileId']};if(!_0x49a046)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x49a046[_0x353e('0x71')]===_0x353e('0x72')){var _0x1d8f12=_0x5d83d5[_0x353e('0x24')]({'plain':!![]});return db['UserProfileSection'][_0x353e('0x48')]({'where':{'name':'Sounds','userProfileId':_0x49a046[_0x353e('0x73')]},'raw':!![]})['then'](function(_0x24934f){if(_0x24934f&&_0x24934f[_0x353e('0x74')]===0x0){return db[_0x353e('0x26')]['create']({'name':_0x1d8f12[_0x353e('0x29')],'resourceId':_0x1d8f12['id'],'type':_0x24934f[_0x353e('0x29')],'sectionId':_0x24934f['id']},{})[_0x353e('0x22')](function(){return _0x5d83d5;});}else{return _0x5d83d5;}})[_0x353e('0x44')](function(_0x22310a){logger['error'](_0x353e('0x75'),_0x22310a);throw _0x22310a;});}return _0x5d83d5;})[_0x353e('0x22')](respondWithResult(_0xd6d2d2,null))[_0x353e('0x44')](handleError(_0xd6d2d2,null));});_0x442d31[_0x353e('0x76')]();}catch(_0x450201){_0xd6d2d2[_0x353e('0x18')](0x1f4)[_0x353e('0x2a')](_0x450201);}};exports[_0x353e('0x21')]=function(_0x254042,_0xacb72a,_0x1675e7){return db[_0x353e('0x2d')][_0x353e('0x48')]({'where':{'id':_0x254042[_0x353e('0x46')]['id']}})['then'](handleEntityNotFound(_0xacb72a,null))[_0x353e('0x22')](saveUpdates(_[_0x353e('0x3c')](_0x254042[_0x353e('0x70')],['name','description']),null))[_0x353e('0x22')](respondWithResult(_0xacb72a,null))[_0x353e('0x44')](handleError(_0xacb72a,null));};exports['delete']=function(_0x744993,_0x15c7a5,_0xcbb737){return db[_0x353e('0x2d')]['find']({'where':{'id':_0x744993['params']['id']}})[_0x353e('0x22')](function(_0x37283c){if(_0x37283c){return _0x37283c[_0x353e('0x23')]();}})[_0x353e('0x22')](function(_0x2af531){var _0x3e54fd=path[_0x353e('0x4c')](config[_0x353e('0x5a')],_0x353e('0x5b'),util[_0x353e('0x51')](_0x353e('0x77'),_0x2af531[_0x353e('0x4a')],_0x2af531[_0x353e('0x78')]));var _0xeab4e1=path[_0x353e('0x4c')](config[_0x353e('0x5a')],_0x353e('0x50'),util['format'](_0x353e('0x77'),_0x2af531[_0x353e('0x4a')],_0x2af531['converted_format']));fs[_0x353e('0x62')](_0x3e54fd);fs[_0x353e('0x62')](_0xeab4e1);return _0x2af531;})[_0x353e('0x22')](function(_0x36d0e4){if(_0x36d0e4){return _0x15c7a5[_0x353e('0x18')](0xcc)[_0x353e('0x19')]();}})[_0x353e('0x22')](handleEntityNotFound(_0x15c7a5,null))[_0x353e('0x44')](handleError(_0x15c7a5,null));};