fd011c20404e7cbaece9c71c5160a2482853429b
[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 _0xb132=['query','attributes','fields','length','order','sort','where','filters','pick','merge','VIRTUAL','filter','options','include','rows','catch','params','keys','intersection','includeAll','download','save_name','converted_format','existsSync','join','server/files/sounds/converted','ValidationError','Sound\x20not\x20found','root','format','%s.%s','create','basename','originalname','extname','file','substring','filename','Sound\x20upload\x20filepath:\x20%s','0777','wav','unlinkSync','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','chmodSync','body','userProfileId','UserProfileSection','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','description','delete','find','destroy','server/files/sounds/original','original_format','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','to-csv','fs-extra','lodash','squel','crypto','desk.js','querystring','ioredis','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','offset','undefined','limit','count','set','json','apply','save','update','then','get','Sounds','end','error','stack','name','send','index','map','Sound','rawAttributes','fieldName','type','key','model'];(function(_0x24259d,_0x64a194){var _0x5a701=function(_0x204516){while(--_0x204516){_0x24259d['push'](_0x24259d['shift']());}};_0x5a701(++_0x64a194);}(_0xb132,0x13b));var _0x2b13=function(_0x548d7d,_0x2d41fc){_0x548d7d=_0x548d7d-0x0;var _0xb1c878=_0xb132[_0x548d7d];return _0xb1c878;};'use strict';var emlformat=require(_0x2b13('0x0'));var rimraf=require(_0x2b13('0x1'));var zipdir=require(_0x2b13('0x2'));var jsonpatch=require(_0x2b13('0x3'));var rp=require(_0x2b13('0x4'));var moment=require(_0x2b13('0x5'));var BPromise=require(_0x2b13('0x6'));var Mustache=require(_0x2b13('0x7'));var util=require(_0x2b13('0x8'));var path=require('path');var sox=require('sox');var csv=require(_0x2b13('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x2b13('0xa'));var _=require(_0x2b13('0xb'));var squel=require(_0x2b13('0xc'));var crypto=require(_0x2b13('0xd'));var jsforce=require('jsforce');var deskjs=require(_0x2b13('0xe'));var toCsv=require('to-csv');var querystring=require(_0x2b13('0xf'));var Papa=require('papaparse');var Redis=require(_0x2b13('0x10'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x2b13('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x2b13('0x12'))(_0x2b13('0x13'));var utils=require(_0x2b13('0x14'));var config=require(_0x2b13('0x15'));var licenseUtil=require(_0x2b13('0x16'));var db=require(_0x2b13('0x17'))['db'];function respondWithStatusCode(_0x3c8280,_0x3d3b72){_0x3d3b72=_0x3d3b72||0xcc;return function(_0x333599){if(_0x333599){return _0x3c8280[_0x2b13('0x18')](_0x3d3b72);}return _0x3c8280[_0x2b13('0x19')](_0x3d3b72)['end']();};}function respondWithResult(_0x49eacd,_0x232eef){_0x232eef=_0x232eef||0xc8;return function(_0x2624ca){if(_0x2624ca){return _0x49eacd['status'](_0x232eef)['json'](_0x2624ca);}};}function respondWithFilteredResult(_0x280ea4,_0xf84d8c){return function(_0x461012){if(_0x461012){var _0x39d6ba=typeof _0xf84d8c[_0x2b13('0x1a')]===_0x2b13('0x1b')&&typeof _0xf84d8c[_0x2b13('0x1c')]==='undefined';var _0x524fdd=_0x461012[_0x2b13('0x1d')];var _0x5686ee=_0x39d6ba?0x0:_0xf84d8c[_0x2b13('0x1a')];var _0x3bf903=_0x39d6ba?_0x461012['count']:_0xf84d8c[_0x2b13('0x1a')]+_0xf84d8c[_0x2b13('0x1c')];var _0x232a39;if(_0x3bf903>=_0x524fdd){_0x3bf903=_0x524fdd;_0x232a39=0xc8;}else{_0x232a39=0xce;}_0x280ea4[_0x2b13('0x19')](_0x232a39);return _0x280ea4[_0x2b13('0x1e')]('Content-Range',_0x5686ee+'-'+_0x3bf903+'/'+_0x524fdd)[_0x2b13('0x1f')](_0x461012);}return null;};}function patchUpdates(_0x318767){return function(_0x2f7f75){try{jsonpatch[_0x2b13('0x20')](_0x2f7f75,_0x318767,!![]);}catch(_0x2522e4){return BPromise['reject'](_0x2522e4);}return _0x2f7f75[_0x2b13('0x21')]();};}function saveUpdates(_0x1e06dd,_0x56ae71){return function(_0x1a2f3d){if(_0x1a2f3d){return _0x1a2f3d[_0x2b13('0x22')](_0x1e06dd)['then'](function(_0x2253e0){return _0x2253e0;});}return null;};}function removeEntity(_0x44f19b,_0x22d0ce){return function(_0x2db891){if(_0x2db891){return _0x2db891['destroy']()[_0x2b13('0x23')](function(){var _0x112de1=_0x2db891[_0x2b13('0x24')]({'plain':!![]});var _0x1dc0e1=_0x2b13('0x25');return db['UserProfileResource']['destroy']({'where':{'type':_0x1dc0e1,'resourceId':_0x112de1['id']}})[_0x2b13('0x23')](function(){return _0x2db891;});})['then'](function(){_0x44f19b[_0x2b13('0x19')](0xcc)[_0x2b13('0x26')]();});}};}function handleEntityNotFound(_0x1b7db0,_0x26ae32){return function(_0x54d870){if(!_0x54d870){_0x1b7db0[_0x2b13('0x18')](0x194);}return _0x54d870;};}function handleError(_0x469a66,_0x1133d3){_0x1133d3=_0x1133d3||0x1f4;return function(_0x2007f9){logger[_0x2b13('0x27')](_0x2007f9[_0x2b13('0x28')]);if(_0x2007f9['name']){delete _0x2007f9[_0x2b13('0x29')];}_0x469a66[_0x2b13('0x19')](_0x1133d3)[_0x2b13('0x2a')](_0x2007f9);};}exports[_0x2b13('0x2b')]=function(_0x238d08,_0x118534){var _0x83a6ac={},_0x263e28={},_0x18f07e={'count':0x0,'rows':[]};var _0xcd7b35=_[_0x2b13('0x2c')](db[_0x2b13('0x2d')][_0x2b13('0x2e')],function(_0x30ea36){return{'name':_0x30ea36[_0x2b13('0x2f')],'type':_0x30ea36[_0x2b13('0x30')][_0x2b13('0x31')]};});_0x263e28[_0x2b13('0x32')]=_[_0x2b13('0x2c')](_0xcd7b35,_0x2b13('0x29'));_0x263e28[_0x2b13('0x33')]=_['keys'](_0x238d08[_0x2b13('0x33')]);_0x263e28['filters']=_['intersection'](_0x263e28[_0x2b13('0x32')],_0x263e28[_0x2b13('0x33')]);_0x83a6ac[_0x2b13('0x34')]=_['intersection'](_0x263e28['model'],qs[_0x2b13('0x35')](_0x238d08[_0x2b13('0x33')][_0x2b13('0x35')]));_0x83a6ac[_0x2b13('0x34')]=_0x83a6ac[_0x2b13('0x34')][_0x2b13('0x36')]?_0x83a6ac[_0x2b13('0x34')]:_0x263e28[_0x2b13('0x32')];if(!_0x238d08[_0x2b13('0x33')]['hasOwnProperty']('nolimit')){_0x83a6ac[_0x2b13('0x1c')]=qs[_0x2b13('0x1c')](_0x238d08[_0x2b13('0x33')][_0x2b13('0x1c')]);_0x83a6ac['offset']=qs[_0x2b13('0x1a')](_0x238d08[_0x2b13('0x33')]['offset']);}_0x83a6ac[_0x2b13('0x37')]=qs[_0x2b13('0x38')](_0x238d08['query'][_0x2b13('0x38')]);_0x83a6ac[_0x2b13('0x39')]=qs[_0x2b13('0x3a')](_[_0x2b13('0x3b')](_0x238d08[_0x2b13('0x33')],_0x263e28['filters']),_0xcd7b35);if(_0x238d08[_0x2b13('0x33')]['filter']){_0x83a6ac[_0x2b13('0x39')]=_[_0x2b13('0x3c')](_0x83a6ac[_0x2b13('0x39')],{'$or':_[_0x2b13('0x2c')](_0xcd7b35,function(_0x21115d){if(_0x21115d[_0x2b13('0x30')]!==_0x2b13('0x3d')){var _0x2a903a={};_0x2a903a[_0x21115d['name']]={'$like':'%'+_0x238d08[_0x2b13('0x33')][_0x2b13('0x3e')]+'%'};return _0x2a903a;}})});}_0x83a6ac=_[_0x2b13('0x3c')]({},_0x83a6ac,_0x238d08[_0x2b13('0x3f')]);var _0x18ccd7={'where':_0x83a6ac['where']};return db[_0x2b13('0x2d')][_0x2b13('0x1d')](_0x18ccd7)['then'](function(_0xfcca67){_0x18f07e[_0x2b13('0x1d')]=_0xfcca67;if(_0x238d08[_0x2b13('0x33')]['includeAll']){_0x83a6ac[_0x2b13('0x40')]=[{'all':!![]}];}return db[_0x2b13('0x2d')]['findAll'](_0x83a6ac);})[_0x2b13('0x23')](function(_0x80d130){_0x18f07e[_0x2b13('0x41')]=_0x80d130;return _0x18f07e;})['then'](respondWithFilteredResult(_0x118534,_0x83a6ac))[_0x2b13('0x42')](handleError(_0x118534,null));};exports['show']=function(_0x5e802f,_0x5141f6){var _0x3345ac={'raw':!![],'where':{'id':_0x5e802f[_0x2b13('0x43')]['id']}},_0x4d55b6={};_0x4d55b6[_0x2b13('0x32')]=_[_0x2b13('0x44')](db[_0x2b13('0x2d')][_0x2b13('0x2e')]);_0x4d55b6[_0x2b13('0x33')]=_[_0x2b13('0x44')](_0x5e802f[_0x2b13('0x33')]);_0x4d55b6['filters']=_['intersection'](_0x4d55b6['model'],_0x4d55b6[_0x2b13('0x33')]);_0x3345ac['attributes']=_[_0x2b13('0x45')](_0x4d55b6[_0x2b13('0x32')],qs[_0x2b13('0x35')](_0x5e802f[_0x2b13('0x33')][_0x2b13('0x35')]));_0x3345ac[_0x2b13('0x34')]=_0x3345ac[_0x2b13('0x34')][_0x2b13('0x36')]?_0x3345ac[_0x2b13('0x34')]:_0x4d55b6[_0x2b13('0x32')];if(_0x5e802f['query'][_0x2b13('0x46')]){_0x3345ac['include']=[{'all':!![]}];}_0x3345ac=_['merge']({},_0x3345ac,_0x5e802f[_0x2b13('0x3f')]);return db[_0x2b13('0x2d')]['find'](_0x3345ac)[_0x2b13('0x23')](handleEntityNotFound(_0x5141f6,null))[_0x2b13('0x23')](respondWithResult(_0x5141f6,null))[_0x2b13('0x42')](handleError(_0x5141f6,null));};exports[_0x2b13('0x47')]=function(_0x1d6bea,_0x44b8c1,_0xfaec40){return db[_0x2b13('0x2d')]['find']({'where':{'id':_0x1d6bea['params']['id']},'attributes':[_0x2b13('0x29'),_0x2b13('0x48'),_0x2b13('0x49')],'raw':!![]})[_0x2b13('0x23')](handleEntityNotFound(_0x44b8c1,null))[_0x2b13('0x23')](function(_0x12bb2d){if(_0x12bb2d){if(!fs[_0x2b13('0x4a')](path[_0x2b13('0x4b')](config['root'],_0x2b13('0x4c'),_0x12bb2d[_0x2b13('0x48')]+'.'+_0x12bb2d['converted_format']))){throw new db['Sequelize'][(_0x2b13('0x4d'))](_0x2b13('0x4e'));}return _0x44b8c1[_0x2b13('0x47')](path[_0x2b13('0x4b')](config[_0x2b13('0x4f')],_0x2b13('0x4c'),util[_0x2b13('0x50')](_0x2b13('0x51'),_0x12bb2d[_0x2b13('0x48')],_0x12bb2d[_0x2b13('0x49')])),util[_0x2b13('0x50')](_0x2b13('0x51'),_0x12bb2d[_0x2b13('0x29')],_0x12bb2d[_0x2b13('0x49')]));}return null;})[_0x2b13('0x42')](handleError(_0x44b8c1,null));};exports[_0x2b13('0x52')]=function(_0x3faaa5,_0x4e7f00,_0x18812a){var _0x3de088={'name':path[_0x2b13('0x53')](_0x3faaa5['file'][_0x2b13('0x54')],path[_0x2b13('0x55')](_0x3faaa5['file'][_0x2b13('0x54')])),'save_name':path['basename'](_0x3faaa5[_0x2b13('0x56')]['filename'],path[_0x2b13('0x55')](_0x3faaa5[_0x2b13('0x56')]['filename'])),'original_format':path['extname'](_0x3faaa5['file'][_0x2b13('0x54')])[_0x2b13('0x57')](0x1),'converted_format':'wav'};var _0x1fd9ea=path[_0x2b13('0x4b')](config['root'],'server/files/sounds/original',_0x3faaa5['file'][_0x2b13('0x58')]);var _0x44e56b=path[_0x2b13('0x4b')](config[_0x2b13('0x4f')],_0x2b13('0x4c'),util[_0x2b13('0x50')](_0x2b13('0x51'),path[_0x2b13('0x53')](_0x3faaa5[_0x2b13('0x56')][_0x2b13('0x58')],path[_0x2b13('0x55')](_0x3faaa5[_0x2b13('0x56')][_0x2b13('0x58')])),_0x3de088[_0x2b13('0x49')]));logger['info'](_0x2b13('0x59'),_0x1fd9ea);logger['info']('Sound\x20upload\x20destination:\x20%s',_0x44e56b);try{fs['chmodSync'](_0x1fd9ea,parseInt(_0x2b13('0x5a'),0x8));var _0x1452c9=sox['transcode'](_0x1fd9ea,_0x44e56b,{'sampleRate':0x1f40,'format':_0x2b13('0x5b'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x1452c9['on']('error',function(_0x623faa){fs[_0x2b13('0x5c')](_0x1fd9ea);_0x4e7f00[_0x2b13('0x19')](0x1f4)[_0x2b13('0x2a')](_0x623faa);});_0x1452c9['on'](_0x2b13('0x5d'),function(_0x4b60d9){_0x3de088['original_duration']=isNaN(_0x4b60d9[_0x2b13('0x5e')])?null:_0x4b60d9[_0x2b13('0x5e')]*0x3e8;_0x3de088[_0x2b13('0x5f')]=isNaN(_0x4b60d9['sampleCount'])?null:_0x4b60d9[_0x2b13('0x60')];_0x3de088[_0x2b13('0x61')]=isNaN(_0x4b60d9[_0x2b13('0x62')])?null:_0x4b60d9[_0x2b13('0x62')];_0x3de088[_0x2b13('0x63')]=isNaN(_0x4b60d9[_0x2b13('0x64')])?null:_0x4b60d9[_0x2b13('0x64')];_0x3de088[_0x2b13('0x65')]=isNaN(_0x4b60d9[_0x2b13('0x66')])?null:_0x4b60d9[_0x2b13('0x66')];});_0x1452c9['on'](_0x2b13('0x67'),function(_0x41aa36){_0x3de088[_0x2b13('0x68')]=isNaN(_0x41aa36['duration'])?null:_0x41aa36[_0x2b13('0x5e')]*0x3e8;_0x3de088['converted_sampleCount']=isNaN(_0x41aa36[_0x2b13('0x60')])?null:_0x41aa36[_0x2b13('0x60')];_0x3de088[_0x2b13('0x69')]=isNaN(_0x41aa36[_0x2b13('0x62')])?null:_0x41aa36['channelCount'];_0x3de088[_0x2b13('0x6a')]=isNaN(_0x41aa36[_0x2b13('0x64')])?null:_0x41aa36[_0x2b13('0x64')];_0x3de088[_0x2b13('0x6b')]=isNaN(_0x41aa36[_0x2b13('0x66')])?null:_0x41aa36[_0x2b13('0x66')];});_0x1452c9['on'](_0x2b13('0x26'),function(){fs[_0x2b13('0x6c')](_0x44e56b,parseInt(_0x2b13('0x5a'),0x8));return db[_0x2b13('0x2d')][_0x2b13('0x52')](_0x3de088)[_0x2b13('0x23')](function(_0x3ac574){var _0x3e7309={'role':_0x3faaa5['body']['role'],'userProfileId':_0x3faaa5[_0x2b13('0x6d')][_0x2b13('0x6e')]};if(!_0x3e7309)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3e7309['role']==='user'){var _0x441780=_0x3ac574[_0x2b13('0x24')]({'plain':!![]});return db[_0x2b13('0x6f')]['find']({'where':{'name':_0x2b13('0x25'),'userProfileId':_0x3e7309[_0x2b13('0x6e')]},'raw':!![]})[_0x2b13('0x23')](function(_0x1bea5b){if(_0x1bea5b&&_0x1bea5b[_0x2b13('0x70')]===0x0){return db[_0x2b13('0x71')][_0x2b13('0x52')]({'name':_0x441780[_0x2b13('0x29')],'resourceId':_0x441780['id'],'type':_0x1bea5b[_0x2b13('0x29')],'sectionId':_0x1bea5b['id']},{})[_0x2b13('0x23')](function(){return _0x3ac574;});}else{return _0x3ac574;}})[_0x2b13('0x42')](function(_0x3228ea){logger['error'](_0x2b13('0x72'),_0x3228ea);throw _0x3228ea;});}return _0x3ac574;})['then'](respondWithResult(_0x4e7f00,null))['catch'](handleError(_0x4e7f00,null));});_0x1452c9[_0x2b13('0x73')]();}catch(_0x42641b){_0x4e7f00['status'](0x1f4)[_0x2b13('0x2a')](_0x42641b);}};exports[_0x2b13('0x22')]=function(_0x54ba0a,_0x18230a,_0x3f5ce9){return db[_0x2b13('0x2d')]['find']({'where':{'id':_0x54ba0a[_0x2b13('0x43')]['id']}})[_0x2b13('0x23')](handleEntityNotFound(_0x18230a,null))[_0x2b13('0x23')](saveUpdates(_[_0x2b13('0x3b')](_0x54ba0a['body'],[_0x2b13('0x29'),_0x2b13('0x74')]),null))['then'](respondWithResult(_0x18230a,null))[_0x2b13('0x42')](handleError(_0x18230a,null));};exports[_0x2b13('0x75')]=function(_0x2f18cf,_0x50d7ba,_0x1c0996){return db['Sound'][_0x2b13('0x76')]({'where':{'id':_0x2f18cf['params']['id']}})[_0x2b13('0x23')](function(_0x46d4e8){if(_0x46d4e8){return _0x46d4e8[_0x2b13('0x77')]();}})[_0x2b13('0x23')](function(_0x4b24a8){var _0x52a479=path[_0x2b13('0x4b')](config[_0x2b13('0x4f')],_0x2b13('0x78'),util['format'](_0x2b13('0x51'),_0x4b24a8[_0x2b13('0x48')],_0x4b24a8[_0x2b13('0x79')]));var _0x265be8=path[_0x2b13('0x4b')](config[_0x2b13('0x4f')],'server/files/sounds/converted',util[_0x2b13('0x50')]('%s.%s',_0x4b24a8['save_name'],_0x4b24a8[_0x2b13('0x49')]));fs[_0x2b13('0x5c')](_0x52a479);fs[_0x2b13('0x5c')](_0x265be8);return _0x4b24a8;})[_0x2b13('0x23')](function(_0x1e4b58){if(_0x1e4b58){return _0x50d7ba['status'](0xcc)['end']();}})[_0x2b13('0x23')](handleEntityNotFound(_0x50d7ba,null))[_0x2b13('0x42')](handleError(_0x50d7ba,null));};