d4a372afae5dd2ccc23f9e67959d4f6300e014f6
[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 _0x8fc0=['UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','pick','description','delete','original_format','html-pdf','eml-format','rimraf','request-promise','moment','mustache','path','ejs','lodash','squel','crypto','desk.js','to-csv','papaparse','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/environment','../../mysqldb','status','end','json','count','offset','limit','set','apply','save','update','destroy','then','Sounds','UserProfileResource','error','name','map','Sound','type','key','model','query','keys','intersection','fields','attributes','hasOwnProperty','order','sort','where','filters','filter','merge','VIRTUAL','options','includeAll','include','rows','catch','show','params','rawAttributes','length','find','download','save_name','converted_format','server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','root','format','%s.%s','basename','file','originalname','filename','extname','wav','join','Sound\x20upload\x20filepath:\x20%s','info','unlinkSync','send','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','0777','create','body','role','Unable\x20to\x20retrieve\x20the\x20current\x20user'];(function(_0x2da3ec,_0x13b6a5){var _0x3758f1=function(_0x5af356){while(--_0x5af356){_0x2da3ec['push'](_0x2da3ec['shift']());}};_0x3758f1(++_0x13b6a5);}(_0x8fc0,0x1d5));var _0x08fc=function(_0x49b207,_0x408ade){_0x49b207=_0x49b207-0x0;var _0x2dc8fa=_0x8fc0[_0x49b207];return _0x2dc8fa;};'use strict';var pdf=require(_0x08fc('0x0'));var emlformat=require(_0x08fc('0x1'));var rimraf=require(_0x08fc('0x2'));var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require(_0x08fc('0x3'));var moment=require(_0x08fc('0x4'));var BPromise=require('bluebird');var Mustache=require(_0x08fc('0x5'));var util=require('util');var path=require(_0x08fc('0x6'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x08fc('0x7'));var fs=require('fs');var _=require(_0x08fc('0x8'));var squel=require(_0x08fc('0x9'));var crypto=require(_0x08fc('0xa'));var jsforce=require('jsforce');var deskjs=require(_0x08fc('0xb'));var toCsv=require(_0x08fc('0xc'));var querystring=require('querystring');var Papa=require(_0x08fc('0xd'));var Redis=require('ioredis');var authService=require(_0x08fc('0xe'));var qs=require(_0x08fc('0xf'));var as=require(_0x08fc('0x10'));var hardwareService=require(_0x08fc('0x11'));var logger=require(_0x08fc('0x12'))('api');var utils=require('../../config/utils');var config=require(_0x08fc('0x13'));var licenseUtil=require('../../config/license/util');var db=require(_0x08fc('0x14'))['db'];function respondWithStatusCode(_0x58efd1,_0x4e1e48){_0x4e1e48=_0x4e1e48||0xcc;return function(_0x31021c){if(_0x31021c){return _0x58efd1['sendStatus'](_0x4e1e48);}return _0x58efd1[_0x08fc('0x15')](_0x4e1e48)[_0x08fc('0x16')]();};}function respondWithResult(_0x482f18,_0x9422d8){_0x9422d8=_0x9422d8||0xc8;return function(_0x373ba3){if(_0x373ba3){return _0x482f18[_0x08fc('0x15')](_0x9422d8)[_0x08fc('0x17')](_0x373ba3);}};}function respondWithFilteredResult(_0x1566e3,_0x703a69){return function(_0x553c20){if(_0x553c20){var _0x1dbc13=_0x553c20[_0x08fc('0x18')],_0x3a54a7=_0x703a69[_0x08fc('0x19')],_0x228e3e=_0x703a69[_0x08fc('0x19')]+_0x703a69[_0x08fc('0x1a')],_0x57d4ad;if(_0x228e3e>=_0x1dbc13){_0x228e3e=_0x1dbc13;_0x57d4ad=0xc8;}else{_0x57d4ad=0xce;}_0x1566e3['status'](_0x57d4ad);return _0x1566e3[_0x08fc('0x1b')]('Content-Range',_0x3a54a7+'-'+_0x228e3e+'/'+_0x1dbc13)[_0x08fc('0x17')](_0x553c20);}return null;};}function patchUpdates(_0x1a5ddb){return function(_0x5ba9c0){try{jsonpatch[_0x08fc('0x1c')](_0x5ba9c0,_0x1a5ddb,!![]);}catch(_0x3a6a95){return BPromise['reject'](_0x3a6a95);}return _0x5ba9c0[_0x08fc('0x1d')]();};}function saveUpdates(_0x1f6c53,_0x51561b){return function(_0x2442ef){if(_0x2442ef){return _0x2442ef[_0x08fc('0x1e')](_0x1f6c53)['then'](function(_0x61b801){return _0x61b801;});}return null;};}function removeEntity(_0x4aa4c1,_0x511b52){return function(_0x156570){if(_0x156570){return _0x156570[_0x08fc('0x1f')]()[_0x08fc('0x20')](function(){var _0x3d4fca=_0x156570['get']({'plain':!![]});var _0x42c0c1=_0x08fc('0x21');return db[_0x08fc('0x22')][_0x08fc('0x1f')]({'where':{'type':_0x42c0c1,'resourceId':_0x3d4fca['id']}})['then'](function(){return _0x156570;});})['then'](function(){_0x4aa4c1[_0x08fc('0x15')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4a3576,_0x34ec40){return function(_0x2489fb){if(!_0x2489fb){_0x4a3576['sendStatus'](0x194);}return _0x2489fb;};}function handleError(_0x2139e6,_0x25bf3d){_0x25bf3d=_0x25bf3d||0x1f4;return function(_0x228890){logger[_0x08fc('0x23')](_0x228890['stack']);if(_0x228890[_0x08fc('0x24')]){delete _0x228890[_0x08fc('0x24')];}_0x2139e6['status'](_0x25bf3d)['send'](_0x228890);};}exports['index']=function(_0x18c67a,_0x49f894){var _0x49ed4e={},_0x27c2e9={},_0x350630={'count':0x0,'rows':[]};var _0x42f67c=_[_0x08fc('0x25')](db[_0x08fc('0x26')]['rawAttributes'],function(_0x52a2cb){return{'name':_0x52a2cb['fieldName'],'type':_0x52a2cb[_0x08fc('0x27')][_0x08fc('0x28')]};});_0x27c2e9[_0x08fc('0x29')]=_[_0x08fc('0x25')](_0x42f67c,_0x08fc('0x24'));_0x27c2e9[_0x08fc('0x2a')]=_[_0x08fc('0x2b')](_0x18c67a[_0x08fc('0x2a')]);_0x27c2e9['filters']=_['intersection'](_0x27c2e9['model'],_0x27c2e9[_0x08fc('0x2a')]);_0x49ed4e['attributes']=_[_0x08fc('0x2c')](_0x27c2e9[_0x08fc('0x29')],qs[_0x08fc('0x2d')](_0x18c67a[_0x08fc('0x2a')]['fields']));_0x49ed4e[_0x08fc('0x2e')]=_0x49ed4e['attributes']['length']?_0x49ed4e[_0x08fc('0x2e')]:_0x27c2e9[_0x08fc('0x29')];if(!_0x18c67a[_0x08fc('0x2a')][_0x08fc('0x2f')]('nolimit')){_0x49ed4e[_0x08fc('0x1a')]=qs['limit'](_0x18c67a[_0x08fc('0x2a')]['limit']);_0x49ed4e[_0x08fc('0x19')]=qs[_0x08fc('0x19')](_0x18c67a[_0x08fc('0x2a')][_0x08fc('0x19')]);}_0x49ed4e[_0x08fc('0x30')]=qs[_0x08fc('0x31')](_0x18c67a['query']['sort']);_0x49ed4e[_0x08fc('0x32')]=qs[_0x08fc('0x33')](_['pick'](_0x18c67a[_0x08fc('0x2a')],_0x27c2e9[_0x08fc('0x33')]),_0x42f67c);if(_0x18c67a[_0x08fc('0x2a')][_0x08fc('0x34')]){_0x49ed4e['where']=_[_0x08fc('0x35')](_0x49ed4e[_0x08fc('0x32')],{'$or':_[_0x08fc('0x25')](_0x42f67c,function(_0x20a88d){if(_0x20a88d[_0x08fc('0x27')]!==_0x08fc('0x36')){var _0x5a4ce0={};_0x5a4ce0[_0x20a88d['name']]={'$like':'%'+_0x18c67a[_0x08fc('0x2a')][_0x08fc('0x34')]+'%'};return _0x5a4ce0;}})});}_0x49ed4e=_[_0x08fc('0x35')]({},_0x49ed4e,_0x18c67a[_0x08fc('0x37')]);var _0x1e4473={'where':_0x49ed4e[_0x08fc('0x32')]};return db['Sound'][_0x08fc('0x18')](_0x1e4473)[_0x08fc('0x20')](function(_0x438697){_0x350630[_0x08fc('0x18')]=_0x438697;if(_0x18c67a[_0x08fc('0x2a')][_0x08fc('0x38')]){_0x49ed4e[_0x08fc('0x39')]=[{'all':!![]}];}return db[_0x08fc('0x26')]['findAll'](_0x49ed4e);})[_0x08fc('0x20')](function(_0x48bca1){_0x350630[_0x08fc('0x3a')]=_0x48bca1;return _0x350630;})['then'](respondWithFilteredResult(_0x49f894,_0x49ed4e))[_0x08fc('0x3b')](handleError(_0x49f894,null));};exports[_0x08fc('0x3c')]=function(_0x5a68e0,_0x34cfea){var _0x20d470={'raw':!![],'where':{'id':_0x5a68e0[_0x08fc('0x3d')]['id']}},_0x5a19fa={};_0x5a19fa['model']=_[_0x08fc('0x2b')](db[_0x08fc('0x26')][_0x08fc('0x3e')]);_0x5a19fa['query']=_[_0x08fc('0x2b')](_0x5a68e0[_0x08fc('0x2a')]);_0x5a19fa[_0x08fc('0x33')]=_[_0x08fc('0x2c')](_0x5a19fa['model'],_0x5a19fa[_0x08fc('0x2a')]);_0x20d470['attributes']=_[_0x08fc('0x2c')](_0x5a19fa[_0x08fc('0x29')],qs[_0x08fc('0x2d')](_0x5a68e0[_0x08fc('0x2a')][_0x08fc('0x2d')]));_0x20d470[_0x08fc('0x2e')]=_0x20d470[_0x08fc('0x2e')][_0x08fc('0x3f')]?_0x20d470[_0x08fc('0x2e')]:_0x5a19fa[_0x08fc('0x29')];if(_0x5a68e0[_0x08fc('0x2a')][_0x08fc('0x38')]){_0x20d470['include']=[{'all':!![]}];}_0x20d470=_[_0x08fc('0x35')]({},_0x20d470,_0x5a68e0[_0x08fc('0x37')]);return db[_0x08fc('0x26')][_0x08fc('0x40')](_0x20d470)[_0x08fc('0x20')](handleEntityNotFound(_0x34cfea,null))[_0x08fc('0x20')](respondWithResult(_0x34cfea,null))[_0x08fc('0x3b')](handleError(_0x34cfea,null));};exports[_0x08fc('0x41')]=function(_0x19161b,_0x1689d4,_0x43a145){return db[_0x08fc('0x26')][_0x08fc('0x40')]({'where':{'id':_0x19161b[_0x08fc('0x3d')]['id']},'attributes':[_0x08fc('0x24'),_0x08fc('0x42'),_0x08fc('0x43')],'raw':!![]})[_0x08fc('0x20')](handleEntityNotFound(_0x1689d4,null))['then'](function(_0x557611){if(_0x557611){if(!fs['existsSync'](path['join'](config['root'],_0x08fc('0x44'),_0x557611[_0x08fc('0x42')]+'.'+_0x557611['converted_format']))){throw new db[(_0x08fc('0x45'))][(_0x08fc('0x46'))](_0x08fc('0x47'));}return _0x1689d4['download'](path['join'](config[_0x08fc('0x48')],_0x08fc('0x44'),util[_0x08fc('0x49')](_0x08fc('0x4a'),_0x557611[_0x08fc('0x42')],_0x557611[_0x08fc('0x43')])),util['format'](_0x08fc('0x4a'),_0x557611[_0x08fc('0x24')],_0x557611[_0x08fc('0x43')]));}return null;})[_0x08fc('0x3b')](handleError(_0x1689d4,null));};exports['create']=function(_0x2be50b,_0x1484b0,_0x58f58a){var _0x198667={'name':path[_0x08fc('0x4b')](_0x2be50b[_0x08fc('0x4c')][_0x08fc('0x4d')],path['extname'](_0x2be50b[_0x08fc('0x4c')][_0x08fc('0x4d')])),'save_name':path[_0x08fc('0x4b')](_0x2be50b[_0x08fc('0x4c')][_0x08fc('0x4e')],path[_0x08fc('0x4f')](_0x2be50b[_0x08fc('0x4c')]['filename'])),'original_format':path[_0x08fc('0x4f')](_0x2be50b[_0x08fc('0x4c')][_0x08fc('0x4d')])['substring'](0x1),'converted_format':_0x08fc('0x50')};var _0x4233f2=path[_0x08fc('0x51')](config['root'],'server/files/sounds/original',_0x2be50b[_0x08fc('0x4c')]['filename']);var _0x1d8a58=path[_0x08fc('0x51')](config[_0x08fc('0x48')],'server/files/sounds/converted',util[_0x08fc('0x49')]('%s.%s',path[_0x08fc('0x4b')](_0x2be50b[_0x08fc('0x4c')]['filename'],path[_0x08fc('0x4f')](_0x2be50b[_0x08fc('0x4c')][_0x08fc('0x4e')])),_0x198667[_0x08fc('0x43')]));logger['info'](_0x08fc('0x52'),_0x4233f2);logger[_0x08fc('0x53')]('Sound\x20upload\x20destination:\x20%s',_0x1d8a58);try{fs['chmodSync'](_0x4233f2,parseInt('0777',0x8));var _0x3ff160=sox['transcode'](_0x4233f2,_0x1d8a58,{'sampleRate':0x1f40,'format':_0x08fc('0x50'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x3ff160['on'](_0x08fc('0x23'),function(_0xd88ceb){fs[_0x08fc('0x54')](_0x4233f2);_0x1484b0['status'](0x1f4)[_0x08fc('0x55')](_0xd88ceb);});_0x3ff160['on'](_0x08fc('0x56'),function(_0x89a880){_0x198667['original_duration']=isNaN(_0x89a880[_0x08fc('0x57')])?null:_0x89a880[_0x08fc('0x57')]*0x3e8;_0x198667[_0x08fc('0x58')]=isNaN(_0x89a880[_0x08fc('0x59')])?null:_0x89a880['sampleCount'];_0x198667[_0x08fc('0x5a')]=isNaN(_0x89a880[_0x08fc('0x5b')])?null:_0x89a880[_0x08fc('0x5b')];_0x198667[_0x08fc('0x5c')]=isNaN(_0x89a880[_0x08fc('0x5d')])?null:_0x89a880[_0x08fc('0x5d')];_0x198667[_0x08fc('0x5e')]=isNaN(_0x89a880[_0x08fc('0x5f')])?null:_0x89a880[_0x08fc('0x5f')];});_0x3ff160['on'](_0x08fc('0x60'),function(_0x31f520){_0x198667[_0x08fc('0x61')]=isNaN(_0x31f520[_0x08fc('0x57')])?null:_0x31f520['duration']*0x3e8;_0x198667['converted_sampleCount']=isNaN(_0x31f520[_0x08fc('0x59')])?null:_0x31f520[_0x08fc('0x59')];_0x198667[_0x08fc('0x62')]=isNaN(_0x31f520[_0x08fc('0x5b')])?null:_0x31f520[_0x08fc('0x5b')];_0x198667[_0x08fc('0x63')]=isNaN(_0x31f520[_0x08fc('0x5d')])?null:_0x31f520[_0x08fc('0x5d')];_0x198667[_0x08fc('0x64')]=isNaN(_0x31f520[_0x08fc('0x5f')])?null:_0x31f520['sampleRate'];});_0x3ff160['on'](_0x08fc('0x16'),function(){fs['chmodSync'](_0x1d8a58,parseInt(_0x08fc('0x65'),0x8));return db[_0x08fc('0x26')][_0x08fc('0x66')](_0x198667)[_0x08fc('0x20')](function(_0x1d3120){var _0x55408a={'role':_0x2be50b[_0x08fc('0x67')][_0x08fc('0x68')],'userProfileId':_0x2be50b[_0x08fc('0x67')]['userProfileId']};if(!_0x55408a)throw new Error(_0x08fc('0x69'));if(_0x55408a[_0x08fc('0x68')]==='user'){var _0x3531bd=_0x1d3120['get']({'plain':!![]});return db[_0x08fc('0x6a')][_0x08fc('0x40')]({'where':{'name':_0x08fc('0x21'),'userProfileId':_0x55408a[_0x08fc('0x6b')]},'raw':!![]})['then'](function(_0x19399c){if(_0x19399c&&_0x19399c[_0x08fc('0x6c')]===0x0){return db['UserProfileResource']['create']({'name':_0x3531bd['name'],'resourceId':_0x3531bd['id'],'type':_0x19399c['name'],'sectionId':_0x19399c['id']},{})[_0x08fc('0x20')](function(){return _0x1d3120;});}else{return _0x1d3120;}})['catch'](function(_0x309700){logger[_0x08fc('0x23')](_0x08fc('0x6d'),_0x309700);throw _0x309700;});}return _0x1d3120;})[_0x08fc('0x20')](respondWithResult(_0x1484b0,null))[_0x08fc('0x3b')](handleError(_0x1484b0,null));});_0x3ff160[_0x08fc('0x6e')]();}catch(_0x32ff34){_0x1484b0[_0x08fc('0x15')](0x1f4)[_0x08fc('0x55')](_0x32ff34);}};exports['update']=function(_0x4fd2bc,_0x38c9cd,_0x551e1b){return db[_0x08fc('0x26')][_0x08fc('0x40')]({'where':{'id':_0x4fd2bc[_0x08fc('0x3d')]['id']}})[_0x08fc('0x20')](handleEntityNotFound(_0x38c9cd,null))[_0x08fc('0x20')](saveUpdates(_[_0x08fc('0x6f')](_0x4fd2bc['body'],[_0x08fc('0x24'),_0x08fc('0x70')]),null))[_0x08fc('0x20')](respondWithResult(_0x38c9cd,null))[_0x08fc('0x3b')](handleError(_0x38c9cd,null));};exports[_0x08fc('0x71')]=function(_0x4fe490,_0x4a44a6,_0x5ddea4){return db[_0x08fc('0x26')][_0x08fc('0x40')]({'where':{'id':_0x4fe490['params']['id']}})[_0x08fc('0x20')](function(_0x26e279){if(_0x26e279){return _0x26e279[_0x08fc('0x1f')]();}})[_0x08fc('0x20')](function(_0x540763){var _0x537308=path['join'](config[_0x08fc('0x48')],'server/files/sounds/original',util[_0x08fc('0x49')]('%s.%s',_0x540763[_0x08fc('0x42')],_0x540763[_0x08fc('0x72')]));var _0x48c228=path[_0x08fc('0x51')](config['root'],_0x08fc('0x44'),util[_0x08fc('0x49')](_0x08fc('0x4a'),_0x540763[_0x08fc('0x42')],_0x540763[_0x08fc('0x43')]));fs[_0x08fc('0x54')](_0x537308);fs[_0x08fc('0x54')](_0x48c228);return _0x540763;})['then'](function(_0x2f6534){if(_0x2f6534){return _0x4a44a6['status'](0xcc)['end']();}})[_0x08fc('0x20')](handleEntityNotFound(_0x4a44a6,null))[_0x08fc('0x3b')](handleError(_0x4a44a6,null));};