ad67357fa8acea9b104afcb8be30984a9082d019
[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 _0x8501=['fs-extra','lodash','squel','crypto','desk.js','to-csv','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','Content-Range','save','update','then','destroy','get','Sounds','UserProfileResource','error','name','index','rawAttributes','fieldName','type','key','model','keys','query','filters','intersection','attributes','fields','hasOwnProperty','sort','pick','where','map','VIRTUAL','merge','options','count','includeAll','include','findAll','rows','catch','show','Sound','find','download','params','save_name','converted_format','existsSync','join','root','server/files/sounds/converted','ValidationError','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','filename','substring','wav','server/files/sounds/original','extname','info','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','unlinkSync','src','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','send','description','delete','original_format','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','util','path','ejs'];(function(_0x13d07a,_0x41efc3){var _0x5ee0ee=function(_0x4ca94a){while(--_0x4ca94a){_0x13d07a['push'](_0x13d07a['shift']());}};_0x5ee0ee(++_0x41efc3);}(_0x8501,0x1ef));var _0x1850=function(_0x5ce3c8,_0x2503bd){_0x5ce3c8=_0x5ce3c8-0x0;var _0xa76867=_0x8501[_0x5ce3c8];return _0xa76867;};'use strict';var emlformat=require(_0x1850('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x1850('0x1'));var jsonpatch=require(_0x1850('0x2'));var rp=require(_0x1850('0x3'));var moment=require(_0x1850('0x4'));var BPromise=require(_0x1850('0x5'));var Mustache=require('mustache');var util=require(_0x1850('0x6'));var path=require(_0x1850('0x7'));var sox=require('sox');var csv=require('to-csv');var ejs=require(_0x1850('0x8'));var fs=require('fs');var fs_extra=require(_0x1850('0x9'));var _=require(_0x1850('0xa'));var squel=require(_0x1850('0xb'));var crypto=require(_0x1850('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x1850('0xd'));var toCsv=require(_0x1850('0xe'));var querystring=require(_0x1850('0xf'));var Papa=require('papaparse');var Redis=require(_0x1850('0x10'));var authService=require('../../components/auth/service');var qs=require(_0x1850('0x11'));var as=require(_0x1850('0x12'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x1850('0x13'))('api');var utils=require(_0x1850('0x14'));var config=require(_0x1850('0x15'));var licenseUtil=require(_0x1850('0x16'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x3596d5,_0x371a29){_0x371a29=_0x371a29||0xcc;return function(_0xc6ec4f){if(_0xc6ec4f){return _0x3596d5[_0x1850('0x17')](_0x371a29);}return _0x3596d5[_0x1850('0x18')](_0x371a29)[_0x1850('0x19')]();};}function respondWithResult(_0x2ac7e8,_0x2a43b5){_0x2a43b5=_0x2a43b5||0xc8;return function(_0x3c234d){if(_0x3c234d){return _0x2ac7e8[_0x1850('0x18')](_0x2a43b5)[_0x1850('0x1a')](_0x3c234d);}};}function respondWithFilteredResult(_0x554289,_0xd21b8){return function(_0x4f057a){if(_0x4f057a){var _0xaec353=typeof _0xd21b8[_0x1850('0x1b')]===_0x1850('0x1c')&&typeof _0xd21b8['limit']==='undefined';var _0x1f57f9=_0x4f057a['count'];var _0x5015c4=_0xaec353?0x0:_0xd21b8['offset'];var _0x26caa1=_0xaec353?_0x4f057a['count']:_0xd21b8[_0x1850('0x1b')]+_0xd21b8[_0x1850('0x1d')];var _0x484f86;if(_0x26caa1>=_0x1f57f9){_0x26caa1=_0x1f57f9;_0x484f86=0xc8;}else{_0x484f86=0xce;}_0x554289[_0x1850('0x18')](_0x484f86);return _0x554289['set'](_0x1850('0x1e'),_0x5015c4+'-'+_0x26caa1+'/'+_0x1f57f9)[_0x1850('0x1a')](_0x4f057a);}return null;};}function patchUpdates(_0x184a9c){return function(_0x201770){try{jsonpatch['apply'](_0x201770,_0x184a9c,!![]);}catch(_0x3f5383){return BPromise['reject'](_0x3f5383);}return _0x201770[_0x1850('0x1f')]();};}function saveUpdates(_0x1cecce,_0x101ec8){return function(_0x42d65a){if(_0x42d65a){return _0x42d65a[_0x1850('0x20')](_0x1cecce)[_0x1850('0x21')](function(_0x4e6255){return _0x4e6255;});}return null;};}function removeEntity(_0x5d5892,_0x434574){return function(_0x4e1aa9){if(_0x4e1aa9){return _0x4e1aa9[_0x1850('0x22')]()[_0x1850('0x21')](function(){var _0x2f65c1=_0x4e1aa9[_0x1850('0x23')]({'plain':!![]});var _0x5daff6=_0x1850('0x24');return db[_0x1850('0x25')]['destroy']({'where':{'type':_0x5daff6,'resourceId':_0x2f65c1['id']}})[_0x1850('0x21')](function(){return _0x4e1aa9;});})[_0x1850('0x21')](function(){_0x5d5892[_0x1850('0x18')](0xcc)[_0x1850('0x19')]();});}};}function handleEntityNotFound(_0x5daf7e,_0x25d559){return function(_0x3e689b){if(!_0x3e689b){_0x5daf7e[_0x1850('0x17')](0x194);}return _0x3e689b;};}function handleError(_0x266b00,_0xf26d31){_0xf26d31=_0xf26d31||0x1f4;return function(_0x5278cc){logger[_0x1850('0x26')](_0x5278cc['stack']);if(_0x5278cc[_0x1850('0x27')]){delete _0x5278cc[_0x1850('0x27')];}_0x266b00[_0x1850('0x18')](_0xf26d31)['send'](_0x5278cc);};}exports[_0x1850('0x28')]=function(_0x466ad1,_0x500c9a){var _0x471d05={},_0x51772a={},_0x2ffcf8={'count':0x0,'rows':[]};var _0x10e671=_['map'](db['Sound'][_0x1850('0x29')],function(_0x38f340){return{'name':_0x38f340[_0x1850('0x2a')],'type':_0x38f340[_0x1850('0x2b')][_0x1850('0x2c')]};});_0x51772a[_0x1850('0x2d')]=_['map'](_0x10e671,_0x1850('0x27'));_0x51772a['query']=_[_0x1850('0x2e')](_0x466ad1[_0x1850('0x2f')]);_0x51772a[_0x1850('0x30')]=_[_0x1850('0x31')](_0x51772a[_0x1850('0x2d')],_0x51772a[_0x1850('0x2f')]);_0x471d05[_0x1850('0x32')]=_[_0x1850('0x31')](_0x51772a[_0x1850('0x2d')],qs[_0x1850('0x33')](_0x466ad1[_0x1850('0x2f')][_0x1850('0x33')]));_0x471d05[_0x1850('0x32')]=_0x471d05['attributes']['length']?_0x471d05[_0x1850('0x32')]:_0x51772a[_0x1850('0x2d')];if(!_0x466ad1[_0x1850('0x2f')][_0x1850('0x34')]('nolimit')){_0x471d05[_0x1850('0x1d')]=qs[_0x1850('0x1d')](_0x466ad1[_0x1850('0x2f')][_0x1850('0x1d')]);_0x471d05[_0x1850('0x1b')]=qs[_0x1850('0x1b')](_0x466ad1[_0x1850('0x2f')][_0x1850('0x1b')]);}_0x471d05['order']=qs[_0x1850('0x35')](_0x466ad1[_0x1850('0x2f')]['sort']);_0x471d05['where']=qs[_0x1850('0x30')](_[_0x1850('0x36')](_0x466ad1[_0x1850('0x2f')],_0x51772a[_0x1850('0x30')]),_0x10e671);if(_0x466ad1[_0x1850('0x2f')]['filter']){_0x471d05[_0x1850('0x37')]=_['merge'](_0x471d05[_0x1850('0x37')],{'$or':_[_0x1850('0x38')](_0x10e671,function(_0x5eb472){if(_0x5eb472['type']!==_0x1850('0x39')){var _0x365fb4={};_0x365fb4[_0x5eb472[_0x1850('0x27')]]={'$like':'%'+_0x466ad1[_0x1850('0x2f')]['filter']+'%'};return _0x365fb4;}})});}_0x471d05=_[_0x1850('0x3a')]({},_0x471d05,_0x466ad1[_0x1850('0x3b')]);var _0x251748={'where':_0x471d05['where']};return db['Sound']['count'](_0x251748)[_0x1850('0x21')](function(_0x5eeb80){_0x2ffcf8[_0x1850('0x3c')]=_0x5eeb80;if(_0x466ad1[_0x1850('0x2f')][_0x1850('0x3d')]){_0x471d05[_0x1850('0x3e')]=[{'all':!![]}];}return db['Sound'][_0x1850('0x3f')](_0x471d05);})[_0x1850('0x21')](function(_0x561cd1){_0x2ffcf8[_0x1850('0x40')]=_0x561cd1;return _0x2ffcf8;})[_0x1850('0x21')](respondWithFilteredResult(_0x500c9a,_0x471d05))[_0x1850('0x41')](handleError(_0x500c9a,null));};exports[_0x1850('0x42')]=function(_0x125e78,_0x2b7d18){var _0x397197={'raw':!![],'where':{'id':_0x125e78['params']['id']}},_0x564383={};_0x564383[_0x1850('0x2d')]=_[_0x1850('0x2e')](db[_0x1850('0x43')][_0x1850('0x29')]);_0x564383[_0x1850('0x2f')]=_[_0x1850('0x2e')](_0x125e78[_0x1850('0x2f')]);_0x564383[_0x1850('0x30')]=_[_0x1850('0x31')](_0x564383[_0x1850('0x2d')],_0x564383['query']);_0x397197[_0x1850('0x32')]=_[_0x1850('0x31')](_0x564383[_0x1850('0x2d')],qs[_0x1850('0x33')](_0x125e78[_0x1850('0x2f')]['fields']));_0x397197[_0x1850('0x32')]=_0x397197[_0x1850('0x32')]['length']?_0x397197[_0x1850('0x32')]:_0x564383['model'];if(_0x125e78[_0x1850('0x2f')]['includeAll']){_0x397197[_0x1850('0x3e')]=[{'all':!![]}];}_0x397197=_[_0x1850('0x3a')]({},_0x397197,_0x125e78[_0x1850('0x3b')]);return db[_0x1850('0x43')][_0x1850('0x44')](_0x397197)['then'](handleEntityNotFound(_0x2b7d18,null))[_0x1850('0x21')](respondWithResult(_0x2b7d18,null))[_0x1850('0x41')](handleError(_0x2b7d18,null));};exports[_0x1850('0x45')]=function(_0x4bbad3,_0x113493,_0x1c5143){return db[_0x1850('0x43')][_0x1850('0x44')]({'where':{'id':_0x4bbad3[_0x1850('0x46')]['id']},'attributes':[_0x1850('0x27'),_0x1850('0x47'),_0x1850('0x48')],'raw':!![]})[_0x1850('0x21')](handleEntityNotFound(_0x113493,null))[_0x1850('0x21')](function(_0x39902b){if(_0x39902b){if(!fs[_0x1850('0x49')](path[_0x1850('0x4a')](config[_0x1850('0x4b')],_0x1850('0x4c'),_0x39902b['save_name']+'.'+_0x39902b['converted_format']))){throw new db['Sequelize'][(_0x1850('0x4d'))](_0x1850('0x4e'));}return _0x113493[_0x1850('0x45')](path[_0x1850('0x4a')](config[_0x1850('0x4b')],_0x1850('0x4c'),util[_0x1850('0x4f')]('%s.%s',_0x39902b[_0x1850('0x47')],_0x39902b[_0x1850('0x48')])),util['format'](_0x1850('0x50'),_0x39902b[_0x1850('0x27')],_0x39902b[_0x1850('0x48')]));}return null;})['catch'](handleError(_0x113493,null));};exports[_0x1850('0x51')]=function(_0x1c93fd,_0x2d4015,_0xdb73bf){var _0x219edf={'name':path[_0x1850('0x52')](_0x1c93fd[_0x1850('0x53')][_0x1850('0x54')],path['extname'](_0x1c93fd['file'][_0x1850('0x54')])),'save_name':path[_0x1850('0x52')](_0x1c93fd[_0x1850('0x53')]['filename'],path['extname'](_0x1c93fd[_0x1850('0x53')][_0x1850('0x55')])),'original_format':path['extname'](_0x1c93fd['file'][_0x1850('0x54')])[_0x1850('0x56')](0x1),'converted_format':_0x1850('0x57')};var _0x115d53=path[_0x1850('0x4a')](config[_0x1850('0x4b')],_0x1850('0x58'),_0x1c93fd[_0x1850('0x53')]['filename']);var _0x1530c6=path[_0x1850('0x4a')](config[_0x1850('0x4b')],_0x1850('0x4c'),util[_0x1850('0x4f')]('%s.%s',path[_0x1850('0x52')](_0x1c93fd['file']['filename'],path[_0x1850('0x59')](_0x1c93fd[_0x1850('0x53')][_0x1850('0x55')])),_0x219edf[_0x1850('0x48')]));logger[_0x1850('0x5a')]('Sound\x20upload\x20filepath:\x20%s',_0x115d53);logger[_0x1850('0x5a')](_0x1850('0x5b'),_0x1530c6);try{fs[_0x1850('0x5c')](_0x115d53,parseInt(_0x1850('0x5d'),0x8));var _0x2aa2f8=sox[_0x1850('0x5e')](_0x115d53,_0x1530c6,{'sampleRate':0x1f40,'format':_0x1850('0x57'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x2aa2f8['on']('error',function(_0x19d587){fs[_0x1850('0x5f')](_0x115d53);_0x2d4015[_0x1850('0x18')](0x1f4)['send'](_0x19d587);});_0x2aa2f8['on'](_0x1850('0x60'),function(_0x1ea49e){_0x219edf[_0x1850('0x61')]=isNaN(_0x1ea49e[_0x1850('0x62')])?null:_0x1ea49e[_0x1850('0x62')]*0x3e8;_0x219edf[_0x1850('0x63')]=isNaN(_0x1ea49e[_0x1850('0x64')])?null:_0x1ea49e[_0x1850('0x64')];_0x219edf[_0x1850('0x65')]=isNaN(_0x1ea49e['channelCount'])?null:_0x1ea49e[_0x1850('0x66')];_0x219edf[_0x1850('0x67')]=isNaN(_0x1ea49e[_0x1850('0x68')])?null:_0x1ea49e[_0x1850('0x68')];_0x219edf[_0x1850('0x69')]=isNaN(_0x1ea49e[_0x1850('0x6a')])?null:_0x1ea49e[_0x1850('0x6a')];});_0x2aa2f8['on'](_0x1850('0x6b'),function(_0x2e4137){_0x219edf[_0x1850('0x6c')]=isNaN(_0x2e4137['duration'])?null:_0x2e4137[_0x1850('0x62')]*0x3e8;_0x219edf[_0x1850('0x6d')]=isNaN(_0x2e4137[_0x1850('0x64')])?null:_0x2e4137[_0x1850('0x64')];_0x219edf[_0x1850('0x6e')]=isNaN(_0x2e4137[_0x1850('0x66')])?null:_0x2e4137['channelCount'];_0x219edf[_0x1850('0x6f')]=isNaN(_0x2e4137[_0x1850('0x68')])?null:_0x2e4137[_0x1850('0x68')];_0x219edf[_0x1850('0x70')]=isNaN(_0x2e4137[_0x1850('0x6a')])?null:_0x2e4137[_0x1850('0x6a')];});_0x2aa2f8['on']('end',function(){fs[_0x1850('0x5c')](_0x1530c6,parseInt(_0x1850('0x5d'),0x8));return db[_0x1850('0x43')][_0x1850('0x51')](_0x219edf)[_0x1850('0x21')](function(_0x342fe1){var _0x55ca56={'role':_0x1c93fd[_0x1850('0x71')][_0x1850('0x72')],'userProfileId':_0x1c93fd[_0x1850('0x71')][_0x1850('0x73')]};if(!_0x55ca56)throw new Error(_0x1850('0x74'));if(_0x55ca56[_0x1850('0x72')]===_0x1850('0x75')){var _0x4c9315=_0x342fe1[_0x1850('0x23')]({'plain':!![]});return db[_0x1850('0x76')][_0x1850('0x44')]({'where':{'name':_0x1850('0x24'),'userProfileId':_0x55ca56[_0x1850('0x73')]},'raw':!![]})[_0x1850('0x21')](function(_0x5a3269){if(_0x5a3269&&_0x5a3269[_0x1850('0x77')]===0x0){return db[_0x1850('0x25')][_0x1850('0x51')]({'name':_0x4c9315[_0x1850('0x27')],'resourceId':_0x4c9315['id'],'type':_0x5a3269[_0x1850('0x27')],'sectionId':_0x5a3269['id']},{})['then'](function(){return _0x342fe1;});}else{return _0x342fe1;}})[_0x1850('0x41')](function(_0x43edc4){logger[_0x1850('0x26')](_0x1850('0x78'),_0x43edc4);throw _0x43edc4;});}return _0x342fe1;})['then'](respondWithResult(_0x2d4015,null))[_0x1850('0x41')](handleError(_0x2d4015,null));});_0x2aa2f8[_0x1850('0x79')]();}catch(_0x869e01){_0x2d4015['status'](0x1f4)[_0x1850('0x7a')](_0x869e01);}};exports[_0x1850('0x20')]=function(_0x38d378,_0x452a16,_0x2472da){return db[_0x1850('0x43')]['find']({'where':{'id':_0x38d378['params']['id']}})[_0x1850('0x21')](handleEntityNotFound(_0x452a16,null))[_0x1850('0x21')](saveUpdates(_[_0x1850('0x36')](_0x38d378[_0x1850('0x71')],[_0x1850('0x27'),_0x1850('0x7b')]),null))[_0x1850('0x21')](respondWithResult(_0x452a16,null))[_0x1850('0x41')](handleError(_0x452a16,null));};exports[_0x1850('0x7c')]=function(_0x1afbef,_0x13814e,_0x5ad637){return db[_0x1850('0x43')][_0x1850('0x44')]({'where':{'id':_0x1afbef[_0x1850('0x46')]['id']}})[_0x1850('0x21')](function(_0x2b6f2c){if(_0x2b6f2c){return _0x2b6f2c[_0x1850('0x22')]();}})[_0x1850('0x21')](function(_0x4f84b8){var _0xec9d14=path[_0x1850('0x4a')](config[_0x1850('0x4b')],_0x1850('0x58'),util['format'](_0x1850('0x50'),_0x4f84b8[_0x1850('0x47')],_0x4f84b8[_0x1850('0x7d')]));var _0x1881b5=path['join'](config[_0x1850('0x4b')],_0x1850('0x4c'),util[_0x1850('0x4f')]('%s.%s',_0x4f84b8[_0x1850('0x47')],_0x4f84b8[_0x1850('0x48')]));fs[_0x1850('0x5f')](_0xec9d14);fs[_0x1850('0x5f')](_0x1881b5);return _0x4f84b8;})['then'](function(_0x487087){if(_0x487087){return _0x13814e[_0x1850('0x18')](0xcc)[_0x1850('0x19')]();}})['then'](handleEntityNotFound(_0x13814e,null))[_0x1850('0x41')](handleError(_0x13814e,null));};