c587f616560440af8f3e94b316fb59f8bf1f3bf6
[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 _0x57c9=['filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','offset','order','sort','where','pick','merge','type','VIRTUAL','filter','options','includeAll','include','rows','catch','show','params','rawAttributes','find','download','save_name','converted_format','existsSync','join','root','Sequelize','Sound\x20not\x20found','server/files/sounds/converted','format','%s.%s','create','basename','file','extname','filename','originalname','substring','wav','Sound\x20upload\x20filepath:\x20%s','info','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','get','UserProfileSection','autoAssociation','UserProfileResource','error','start','update','description','delete','server/files/sounds/original','original_format','unlinkSync','html-pdf','zip-dir','fast-json-patch','request-promise','bluebird','util','path','ejs','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','sendStatus','status','end','json','count','limit','set','apply','reject','save','then','destroy','Sounds','stack','name','send','index','map','Sound','fieldName','key','model','keys','query'];(function(_0x52e363,_0x48062a){var _0x3fc2b5=function(_0x1b0cf9){while(--_0x1b0cf9){_0x52e363['push'](_0x52e363['shift']());}};_0x3fc2b5(++_0x48062a);}(_0x57c9,0x1ce));var _0x957c=function(_0x1facfa,_0x1acf7d){_0x1facfa=_0x1facfa-0x0;var _0x9ec701=_0x57c9[_0x1facfa];return _0x9ec701;};'use strict';var pdf=require(_0x957c('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require(_0x957c('0x1'));var jsonpatch=require(_0x957c('0x2'));var rp=require(_0x957c('0x3'));var moment=require('moment');var BPromise=require(_0x957c('0x4'));var Mustache=require('mustache');var util=require(_0x957c('0x5'));var path=require(_0x957c('0x6'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x957c('0x7'));var fs=require('fs');var _=require(_0x957c('0x8'));var squel=require(_0x957c('0x9'));var crypto=require(_0x957c('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x957c('0xb'));var toCsv=require('to-csv');var querystring=require(_0x957c('0xc'));var Papa=require(_0x957c('0xd'));var Redis=require(_0x957c('0xe'));var authService=require(_0x957c('0xf'));var qs=require(_0x957c('0x10'));var as=require(_0x957c('0x11'));var hardwareService=require(_0x957c('0x12'));var logger=require('../../config/logger')(_0x957c('0x13'));var utils=require(_0x957c('0x14'));var config=require(_0x957c('0x15'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x83b853,_0x482228){_0x482228=_0x482228||0xcc;return function(_0x1317b7){if(_0x1317b7){return _0x83b853[_0x957c('0x16')](_0x482228);}return _0x83b853[_0x957c('0x17')](_0x482228)[_0x957c('0x18')]();};}function respondWithResult(_0x4e346c,_0x57b613){_0x57b613=_0x57b613||0xc8;return function(_0x39b963){if(_0x39b963){return _0x4e346c[_0x957c('0x17')](_0x57b613)[_0x957c('0x19')](_0x39b963);}};}function respondWithFilteredResult(_0x2b4577,_0x2d91c4){return function(_0x4943b2){if(_0x4943b2){var _0x21d94f=_0x4943b2[_0x957c('0x1a')],_0x7f1525=_0x2d91c4['offset'],_0xfd2bb7=_0x2d91c4['offset']+_0x2d91c4[_0x957c('0x1b')],_0x1b584e;if(_0xfd2bb7>=_0x21d94f){_0xfd2bb7=_0x21d94f;_0x1b584e=0xc8;}else{_0x1b584e=0xce;}_0x2b4577[_0x957c('0x17')](_0x1b584e);return _0x2b4577[_0x957c('0x1c')]('Content-Range',_0x7f1525+'-'+_0xfd2bb7+'/'+_0x21d94f)[_0x957c('0x19')](_0x4943b2);}return null;};}function patchUpdates(_0x54f0b9){return function(_0x24c900){try{jsonpatch[_0x957c('0x1d')](_0x24c900,_0x54f0b9,!![]);}catch(_0x415633){return BPromise[_0x957c('0x1e')](_0x415633);}return _0x24c900[_0x957c('0x1f')]();};}function saveUpdates(_0xafd83c,_0x1aec8e){return function(_0x133f5b){if(_0x133f5b){return _0x133f5b['update'](_0xafd83c)[_0x957c('0x20')](function(_0x18aa0d){return _0x18aa0d;});}return null;};}function removeEntity(_0x51a66c,_0x578c17){return function(_0x4790c6){if(_0x4790c6){return _0x4790c6[_0x957c('0x21')]()[_0x957c('0x20')](function(){var _0x2003b6=_0x4790c6['get']({'plain':!![]});var _0x4c6262=_0x957c('0x22');return db['UserProfileResource'][_0x957c('0x21')]({'where':{'type':_0x4c6262,'resourceId':_0x2003b6['id']}})[_0x957c('0x20')](function(){return _0x4790c6;});})['then'](function(){_0x51a66c['status'](0xcc)[_0x957c('0x18')]();});}};}function handleEntityNotFound(_0x15aa15,_0x37d9e4){return function(_0xd0ed85){if(!_0xd0ed85){_0x15aa15[_0x957c('0x16')](0x194);}return _0xd0ed85;};}function handleError(_0x397cd9,_0x45ed54){_0x45ed54=_0x45ed54||0x1f4;return function(_0x64d634){logger['error'](_0x64d634[_0x957c('0x23')]);if(_0x64d634['name']){delete _0x64d634[_0x957c('0x24')];}_0x397cd9[_0x957c('0x17')](_0x45ed54)[_0x957c('0x25')](_0x64d634);};}exports[_0x957c('0x26')]=function(_0xd13271,_0x1bf2c4){var _0x319e86={},_0x180a8d={},_0xdb1b69={'count':0x0,'rows':[]};var _0x5e723c=_[_0x957c('0x27')](db[_0x957c('0x28')]['rawAttributes'],function(_0x1e16e9){return{'name':_0x1e16e9[_0x957c('0x29')],'type':_0x1e16e9['type'][_0x957c('0x2a')]};});_0x180a8d[_0x957c('0x2b')]=_[_0x957c('0x27')](_0x5e723c,_0x957c('0x24'));_0x180a8d['query']=_[_0x957c('0x2c')](_0xd13271[_0x957c('0x2d')]);_0x180a8d[_0x957c('0x2e')]=_[_0x957c('0x2f')](_0x180a8d[_0x957c('0x2b')],_0x180a8d['query']);_0x319e86['attributes']=_['intersection'](_0x180a8d[_0x957c('0x2b')],qs[_0x957c('0x30')](_0xd13271[_0x957c('0x2d')][_0x957c('0x30')]));_0x319e86['attributes']=_0x319e86[_0x957c('0x31')][_0x957c('0x32')]?_0x319e86['attributes']:_0x180a8d[_0x957c('0x2b')];if(!_0xd13271[_0x957c('0x2d')][_0x957c('0x33')](_0x957c('0x34'))){_0x319e86[_0x957c('0x1b')]=qs[_0x957c('0x1b')](_0xd13271[_0x957c('0x2d')][_0x957c('0x1b')]);_0x319e86[_0x957c('0x35')]=qs[_0x957c('0x35')](_0xd13271[_0x957c('0x2d')]['offset']);}_0x319e86[_0x957c('0x36')]=qs['sort'](_0xd13271[_0x957c('0x2d')][_0x957c('0x37')]);_0x319e86[_0x957c('0x38')]=qs[_0x957c('0x2e')](_[_0x957c('0x39')](_0xd13271[_0x957c('0x2d')],_0x180a8d[_0x957c('0x2e')]),_0x5e723c);if(_0xd13271['query']['filter']){_0x319e86[_0x957c('0x38')]=_[_0x957c('0x3a')](_0x319e86[_0x957c('0x38')],{'$or':_[_0x957c('0x27')](_0x5e723c,function(_0x5d88ac){if(_0x5d88ac[_0x957c('0x3b')]!==_0x957c('0x3c')){var _0x2384e3={};_0x2384e3[_0x5d88ac[_0x957c('0x24')]]={'$like':'%'+_0xd13271[_0x957c('0x2d')][_0x957c('0x3d')]+'%'};return _0x2384e3;}})});}_0x319e86=_[_0x957c('0x3a')]({},_0x319e86,_0xd13271[_0x957c('0x3e')]);var _0x3e6d25={'where':_0x319e86[_0x957c('0x38')]};return db[_0x957c('0x28')][_0x957c('0x1a')](_0x3e6d25)['then'](function(_0xeafe28){_0xdb1b69[_0x957c('0x1a')]=_0xeafe28;if(_0xd13271[_0x957c('0x2d')][_0x957c('0x3f')]){_0x319e86[_0x957c('0x40')]=[{'all':!![]}];}return db[_0x957c('0x28')]['findAll'](_0x319e86);})['then'](function(_0x3bba6e){_0xdb1b69[_0x957c('0x41')]=_0x3bba6e;return _0xdb1b69;})[_0x957c('0x20')](respondWithFilteredResult(_0x1bf2c4,_0x319e86))[_0x957c('0x42')](handleError(_0x1bf2c4,null));};exports[_0x957c('0x43')]=function(_0xe052b8,_0x3974ab){var _0x351a20={'raw':!![],'where':{'id':_0xe052b8[_0x957c('0x44')]['id']}},_0x1c0d80={};_0x1c0d80['model']=_[_0x957c('0x2c')](db[_0x957c('0x28')][_0x957c('0x45')]);_0x1c0d80[_0x957c('0x2d')]=_['keys'](_0xe052b8['query']);_0x1c0d80[_0x957c('0x2e')]=_['intersection'](_0x1c0d80[_0x957c('0x2b')],_0x1c0d80['query']);_0x351a20[_0x957c('0x31')]=_[_0x957c('0x2f')](_0x1c0d80[_0x957c('0x2b')],qs[_0x957c('0x30')](_0xe052b8[_0x957c('0x2d')]['fields']));_0x351a20[_0x957c('0x31')]=_0x351a20['attributes'][_0x957c('0x32')]?_0x351a20[_0x957c('0x31')]:_0x1c0d80[_0x957c('0x2b')];if(_0xe052b8[_0x957c('0x2d')][_0x957c('0x3f')]){_0x351a20[_0x957c('0x40')]=[{'all':!![]}];}_0x351a20=_[_0x957c('0x3a')]({},_0x351a20,_0xe052b8['options']);return db[_0x957c('0x28')][_0x957c('0x46')](_0x351a20)['then'](handleEntityNotFound(_0x3974ab,null))[_0x957c('0x20')](respondWithResult(_0x3974ab,null))[_0x957c('0x42')](handleError(_0x3974ab,null));};exports[_0x957c('0x47')]=function(_0x1a2e52,_0x26ae9b,_0x7086d4){return db['Sound'][_0x957c('0x46')]({'where':{'id':_0x1a2e52['params']['id']},'attributes':[_0x957c('0x24'),_0x957c('0x48'),_0x957c('0x49')],'raw':!![]})[_0x957c('0x20')](handleEntityNotFound(_0x26ae9b,null))[_0x957c('0x20')](function(_0x16ffa6){if(_0x16ffa6){if(!fs[_0x957c('0x4a')](path[_0x957c('0x4b')](config[_0x957c('0x4c')],'server/files/sounds/converted',_0x16ffa6[_0x957c('0x48')]+'.'+_0x16ffa6[_0x957c('0x49')]))){throw new db[(_0x957c('0x4d'))]['ValidationError'](_0x957c('0x4e'));}return _0x26ae9b[_0x957c('0x47')](path['join'](config[_0x957c('0x4c')],_0x957c('0x4f'),util[_0x957c('0x50')](_0x957c('0x51'),_0x16ffa6['save_name'],_0x16ffa6[_0x957c('0x49')])),util[_0x957c('0x50')]('%s.%s',_0x16ffa6[_0x957c('0x24')],_0x16ffa6[_0x957c('0x49')]));}return null;})[_0x957c('0x42')](handleError(_0x26ae9b,null));};exports[_0x957c('0x52')]=function(_0x4daef5,_0x3e147f,_0x13801f){var _0x3c2992={'name':path[_0x957c('0x53')](_0x4daef5[_0x957c('0x54')]['originalname'],path[_0x957c('0x55')](_0x4daef5[_0x957c('0x54')]['originalname'])),'save_name':path[_0x957c('0x53')](_0x4daef5['file'][_0x957c('0x56')],path[_0x957c('0x55')](_0x4daef5[_0x957c('0x54')]['filename'])),'original_format':path['extname'](_0x4daef5[_0x957c('0x54')][_0x957c('0x57')])[_0x957c('0x58')](0x1),'converted_format':_0x957c('0x59')};var _0x1e726d=path[_0x957c('0x4b')](config['root'],'server/files/sounds/original',_0x4daef5['file'][_0x957c('0x56')]);var _0x1986a9=path[_0x957c('0x4b')](config[_0x957c('0x4c')],_0x957c('0x4f'),util['format'](_0x957c('0x51'),path[_0x957c('0x53')](_0x4daef5[_0x957c('0x54')][_0x957c('0x56')],path['extname'](_0x4daef5['file'][_0x957c('0x56')])),_0x3c2992[_0x957c('0x49')]));logger['info'](_0x957c('0x5a'),_0x1e726d);logger[_0x957c('0x5b')](_0x957c('0x5c'),_0x1986a9);try{fs[_0x957c('0x5d')](_0x1e726d,parseInt(_0x957c('0x5e'),0x8));var _0x754a64=sox[_0x957c('0x5f')](_0x1e726d,_0x1986a9,{'sampleRate':0x1f40,'format':_0x957c('0x59'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x754a64['on']('error',function(_0x38f203){fs['unlinkSync'](_0x1e726d);_0x3e147f[_0x957c('0x17')](0x1f4)[_0x957c('0x25')](_0x38f203);});_0x754a64['on']('src',function(_0xe1836f){_0x3c2992[_0x957c('0x60')]=isNaN(_0xe1836f[_0x957c('0x61')])?null:_0xe1836f[_0x957c('0x61')]*0x3e8;_0x3c2992[_0x957c('0x62')]=isNaN(_0xe1836f['sampleCount'])?null:_0xe1836f[_0x957c('0x63')];_0x3c2992[_0x957c('0x64')]=isNaN(_0xe1836f[_0x957c('0x65')])?null:_0xe1836f[_0x957c('0x65')];_0x3c2992['original_bitRate']=isNaN(_0xe1836f[_0x957c('0x66')])?null:_0xe1836f[_0x957c('0x66')];_0x3c2992[_0x957c('0x67')]=isNaN(_0xe1836f[_0x957c('0x68')])?null:_0xe1836f[_0x957c('0x68')];});_0x754a64['on'](_0x957c('0x69'),function(_0x540fa2){_0x3c2992[_0x957c('0x6a')]=isNaN(_0x540fa2['duration'])?null:_0x540fa2[_0x957c('0x61')]*0x3e8;_0x3c2992[_0x957c('0x6b')]=isNaN(_0x540fa2[_0x957c('0x63')])?null:_0x540fa2['sampleCount'];_0x3c2992[_0x957c('0x6c')]=isNaN(_0x540fa2[_0x957c('0x65')])?null:_0x540fa2['channelCount'];_0x3c2992['converted_bitRate']=isNaN(_0x540fa2[_0x957c('0x66')])?null:_0x540fa2[_0x957c('0x66')];_0x3c2992[_0x957c('0x6d')]=isNaN(_0x540fa2['sampleRate'])?null:_0x540fa2[_0x957c('0x68')];});_0x754a64['on']('end',function(){fs[_0x957c('0x5d')](_0x1986a9,parseInt(_0x957c('0x5e'),0x8));return db[_0x957c('0x28')][_0x957c('0x52')](_0x3c2992)[_0x957c('0x20')](function(_0x4bbdec){var _0x3dbe32={'role':_0x4daef5[_0x957c('0x6e')][_0x957c('0x6f')],'userProfileId':_0x4daef5[_0x957c('0x6e')][_0x957c('0x70')]};if(!_0x3dbe32)throw new Error(_0x957c('0x71'));if(_0x3dbe32[_0x957c('0x6f')]===_0x957c('0x72')){var _0x4590b4=_0x4bbdec[_0x957c('0x73')]({'plain':!![]});return db[_0x957c('0x74')]['find']({'where':{'name':_0x957c('0x22'),'userProfileId':_0x3dbe32[_0x957c('0x70')]},'raw':!![]})['then'](function(_0x531b07){if(_0x531b07&&_0x531b07[_0x957c('0x75')]===0x0){return db[_0x957c('0x76')][_0x957c('0x52')]({'name':_0x4590b4[_0x957c('0x24')],'resourceId':_0x4590b4['id'],'type':_0x531b07['name'],'sectionId':_0x531b07['id']},{})[_0x957c('0x20')](function(){return _0x4bbdec;});}else{return _0x4bbdec;}})[_0x957c('0x42')](function(_0x40e743){logger[_0x957c('0x77')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x40e743);throw _0x40e743;});}return _0x4bbdec;})[_0x957c('0x20')](respondWithResult(_0x3e147f,null))[_0x957c('0x42')](handleError(_0x3e147f,null));});_0x754a64[_0x957c('0x78')]();}catch(_0x25b2f7){_0x3e147f[_0x957c('0x17')](0x1f4)[_0x957c('0x25')](_0x25b2f7);}};exports[_0x957c('0x79')]=function(_0x29c245,_0x40ee34,_0x3970a7){return db[_0x957c('0x28')]['find']({'where':{'id':_0x29c245['params']['id']}})[_0x957c('0x20')](handleEntityNotFound(_0x40ee34,null))[_0x957c('0x20')](saveUpdates(_['pick'](_0x29c245[_0x957c('0x6e')],[_0x957c('0x24'),_0x957c('0x7a')]),null))[_0x957c('0x20')](respondWithResult(_0x40ee34,null))[_0x957c('0x42')](handleError(_0x40ee34,null));};exports[_0x957c('0x7b')]=function(_0x2a1ae3,_0xa281c4,_0x2c4bde){return db[_0x957c('0x28')][_0x957c('0x46')]({'where':{'id':_0x2a1ae3[_0x957c('0x44')]['id']}})[_0x957c('0x20')](function(_0x5c5fc4){if(_0x5c5fc4){return _0x5c5fc4['destroy']();}})[_0x957c('0x20')](function(_0x16f788){var _0x4fd383=path[_0x957c('0x4b')](config[_0x957c('0x4c')],_0x957c('0x7c'),util['format'](_0x957c('0x51'),_0x16f788[_0x957c('0x48')],_0x16f788[_0x957c('0x7d')]));var _0x3cc950=path[_0x957c('0x4b')](config[_0x957c('0x4c')],'server/files/sounds/converted',util[_0x957c('0x50')](_0x957c('0x51'),_0x16f788[_0x957c('0x48')],_0x16f788[_0x957c('0x49')]));fs[_0x957c('0x7e')](_0x4fd383);fs[_0x957c('0x7e')](_0x3cc950);return _0x16f788;})[_0x957c('0x20')](function(_0x5f4151){if(_0x5f4151){return _0xa281c4[_0x957c('0x17')](0xcc)[_0x957c('0x18')]();}})[_0x957c('0x20')](handleEntityNotFound(_0xa281c4,null))[_0x957c('0x42')](handleError(_0xa281c4,null));};