74d648ebecc3fc3b4d1ab7877016a26080047529
[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 _0x93f1=['keys','filters','intersection','attributes','fields','hasOwnProperty','order','sort','where','pick','filter','merge','VIRTUAL','options','includeAll','findAll','rows','catch','show','find','params','save_name','converted_format','existsSync','join','root','server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','download','format','%s.%s','create','basename','file','originalname','extname','filename','substring','wav','info','chmodSync','0777','transcode','unlinkSync','send','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','UserProfileSection','Sounds','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','delete','server/files/sounds/original','original_format','eml-format','rimraf','zip-dir','fast-json-patch','moment','bluebird','mustache','util','sox','to-csv','fs-extra','lodash','squel','crypto','desk.js','querystring','papaparse','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','offset','undefined','limit','count','set','reject','save','update','then','destroy','get','error','name','index','map','Sound','rawAttributes','fieldName','type','model','query'];(function(_0x3d0016,_0x4a37e0){var _0x1dcb28=function(_0x57bef9){while(--_0x57bef9){_0x3d0016['push'](_0x3d0016['shift']());}};_0x1dcb28(++_0x4a37e0);}(_0x93f1,0x1c5));var _0x193f=function(_0x306a37,_0x192495){_0x306a37=_0x306a37-0x0;var _0x213407=_0x93f1[_0x306a37];return _0x213407;};'use strict';var emlformat=require(_0x193f('0x0'));var rimraf=require(_0x193f('0x1'));var zipdir=require(_0x193f('0x2'));var jsonpatch=require(_0x193f('0x3'));var rp=require('request-promise');var moment=require(_0x193f('0x4'));var BPromise=require(_0x193f('0x5'));var Mustache=require(_0x193f('0x6'));var util=require(_0x193f('0x7'));var path=require('path');var sox=require(_0x193f('0x8'));var csv=require(_0x193f('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x193f('0xa'));var _=require(_0x193f('0xb'));var squel=require(_0x193f('0xc'));var crypto=require(_0x193f('0xd'));var jsforce=require('jsforce');var deskjs=require(_0x193f('0xe'));var toCsv=require(_0x193f('0x9'));var querystring=require(_0x193f('0xf'));var Papa=require(_0x193f('0x10'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0x193f('0x11'));var as=require(_0x193f('0x12'));var hardwareService=require(_0x193f('0x13'));var logger=require(_0x193f('0x14'))(_0x193f('0x15'));var utils=require(_0x193f('0x16'));var config=require(_0x193f('0x17'));var licenseUtil=require(_0x193f('0x18'));var db=require(_0x193f('0x19'))['db'];function respondWithStatusCode(_0x54ecf0,_0x318743){_0x318743=_0x318743||0xcc;return function(_0x1ef519){if(_0x1ef519){return _0x54ecf0[_0x193f('0x1a')](_0x318743);}return _0x54ecf0['status'](_0x318743)[_0x193f('0x1b')]();};}function respondWithResult(_0x2e872d,_0x15ceff){_0x15ceff=_0x15ceff||0xc8;return function(_0x22a477){if(_0x22a477){return _0x2e872d[_0x193f('0x1c')](_0x15ceff)[_0x193f('0x1d')](_0x22a477);}};}function respondWithFilteredResult(_0x476606,_0xdb14e0){return function(_0x325e91){if(_0x325e91){var _0x1a0518=typeof _0xdb14e0[_0x193f('0x1e')]===_0x193f('0x1f')&&typeof _0xdb14e0[_0x193f('0x20')]===_0x193f('0x1f');var _0x9e73f5=_0x325e91[_0x193f('0x21')];var _0x6469d0=_0x1a0518?0x0:_0xdb14e0[_0x193f('0x1e')];var _0x478f27=_0x1a0518?_0x325e91[_0x193f('0x21')]:_0xdb14e0[_0x193f('0x1e')]+_0xdb14e0['limit'];var _0x4d9d2c;if(_0x478f27>=_0x9e73f5){_0x478f27=_0x9e73f5;_0x4d9d2c=0xc8;}else{_0x4d9d2c=0xce;}_0x476606[_0x193f('0x1c')](_0x4d9d2c);return _0x476606[_0x193f('0x22')]('Content-Range',_0x6469d0+'-'+_0x478f27+'/'+_0x9e73f5)[_0x193f('0x1d')](_0x325e91);}return null;};}function patchUpdates(_0x3ade22){return function(_0x3afc9d){try{jsonpatch['apply'](_0x3afc9d,_0x3ade22,!![]);}catch(_0x3a4692){return BPromise[_0x193f('0x23')](_0x3a4692);}return _0x3afc9d[_0x193f('0x24')]();};}function saveUpdates(_0x19d131,_0xb0af84){return function(_0xfaa622){if(_0xfaa622){return _0xfaa622[_0x193f('0x25')](_0x19d131)[_0x193f('0x26')](function(_0xdebd01){return _0xdebd01;});}return null;};}function removeEntity(_0x27e867,_0x524ad1){return function(_0x3967e9){if(_0x3967e9){return _0x3967e9[_0x193f('0x27')]()[_0x193f('0x26')](function(){var _0x3ae59a=_0x3967e9[_0x193f('0x28')]({'plain':!![]});var _0x2a9a0d='Sounds';return db['UserProfileResource'][_0x193f('0x27')]({'where':{'type':_0x2a9a0d,'resourceId':_0x3ae59a['id']}})[_0x193f('0x26')](function(){return _0x3967e9;});})[_0x193f('0x26')](function(){_0x27e867['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x149d40,_0x2dc715){return function(_0x3b81d0){if(!_0x3b81d0){_0x149d40[_0x193f('0x1a')](0x194);}return _0x3b81d0;};}function handleError(_0x4254e5,_0x27fe1d){_0x27fe1d=_0x27fe1d||0x1f4;return function(_0x114054){logger[_0x193f('0x29')](_0x114054['stack']);if(_0x114054[_0x193f('0x2a')]){delete _0x114054[_0x193f('0x2a')];}_0x4254e5['status'](_0x27fe1d)['send'](_0x114054);};}exports[_0x193f('0x2b')]=function(_0xf20c5b,_0x4d9b16){var _0x2e1055={},_0x4e08d7={},_0x36d931={'count':0x0,'rows':[]};var _0x330548=_[_0x193f('0x2c')](db[_0x193f('0x2d')][_0x193f('0x2e')],function(_0x4e4593){return{'name':_0x4e4593[_0x193f('0x2f')],'type':_0x4e4593[_0x193f('0x30')]['key']};});_0x4e08d7[_0x193f('0x31')]=_[_0x193f('0x2c')](_0x330548,_0x193f('0x2a'));_0x4e08d7[_0x193f('0x32')]=_[_0x193f('0x33')](_0xf20c5b[_0x193f('0x32')]);_0x4e08d7[_0x193f('0x34')]=_[_0x193f('0x35')](_0x4e08d7[_0x193f('0x31')],_0x4e08d7['query']);_0x2e1055[_0x193f('0x36')]=_[_0x193f('0x35')](_0x4e08d7['model'],qs[_0x193f('0x37')](_0xf20c5b[_0x193f('0x32')][_0x193f('0x37')]));_0x2e1055[_0x193f('0x36')]=_0x2e1055[_0x193f('0x36')]['length']?_0x2e1055[_0x193f('0x36')]:_0x4e08d7[_0x193f('0x31')];if(!_0xf20c5b[_0x193f('0x32')][_0x193f('0x38')]('nolimit')){_0x2e1055[_0x193f('0x20')]=qs['limit'](_0xf20c5b['query']['limit']);_0x2e1055[_0x193f('0x1e')]=qs[_0x193f('0x1e')](_0xf20c5b[_0x193f('0x32')][_0x193f('0x1e')]);}_0x2e1055[_0x193f('0x39')]=qs[_0x193f('0x3a')](_0xf20c5b[_0x193f('0x32')][_0x193f('0x3a')]);_0x2e1055[_0x193f('0x3b')]=qs['filters'](_[_0x193f('0x3c')](_0xf20c5b[_0x193f('0x32')],_0x4e08d7[_0x193f('0x34')]),_0x330548);if(_0xf20c5b['query'][_0x193f('0x3d')]){_0x2e1055[_0x193f('0x3b')]=_[_0x193f('0x3e')](_0x2e1055[_0x193f('0x3b')],{'$or':_[_0x193f('0x2c')](_0x330548,function(_0x3b943f){if(_0x3b943f['type']!==_0x193f('0x3f')){var _0x8e6071={};_0x8e6071[_0x3b943f[_0x193f('0x2a')]]={'$like':'%'+_0xf20c5b['query'][_0x193f('0x3d')]+'%'};return _0x8e6071;}})});}_0x2e1055=_[_0x193f('0x3e')]({},_0x2e1055,_0xf20c5b[_0x193f('0x40')]);var _0x5ade1f={'where':_0x2e1055[_0x193f('0x3b')]};return db['Sound'][_0x193f('0x21')](_0x5ade1f)[_0x193f('0x26')](function(_0x110c14){_0x36d931[_0x193f('0x21')]=_0x110c14;if(_0xf20c5b[_0x193f('0x32')][_0x193f('0x41')]){_0x2e1055['include']=[{'all':!![]}];}return db[_0x193f('0x2d')][_0x193f('0x42')](_0x2e1055);})[_0x193f('0x26')](function(_0x37caa9){_0x36d931[_0x193f('0x43')]=_0x37caa9;return _0x36d931;})['then'](respondWithFilteredResult(_0x4d9b16,_0x2e1055))[_0x193f('0x44')](handleError(_0x4d9b16,null));};exports[_0x193f('0x45')]=function(_0x31946a,_0x38da9a){var _0x59145c={'raw':!![],'where':{'id':_0x31946a['params']['id']}},_0x4aeb5c={};_0x4aeb5c[_0x193f('0x31')]=_['keys'](db['Sound'][_0x193f('0x2e')]);_0x4aeb5c['query']=_[_0x193f('0x33')](_0x31946a[_0x193f('0x32')]);_0x4aeb5c['filters']=_[_0x193f('0x35')](_0x4aeb5c[_0x193f('0x31')],_0x4aeb5c['query']);_0x59145c[_0x193f('0x36')]=_[_0x193f('0x35')](_0x4aeb5c[_0x193f('0x31')],qs[_0x193f('0x37')](_0x31946a[_0x193f('0x32')][_0x193f('0x37')]));_0x59145c[_0x193f('0x36')]=_0x59145c[_0x193f('0x36')]['length']?_0x59145c[_0x193f('0x36')]:_0x4aeb5c[_0x193f('0x31')];if(_0x31946a[_0x193f('0x32')][_0x193f('0x41')]){_0x59145c['include']=[{'all':!![]}];}_0x59145c=_[_0x193f('0x3e')]({},_0x59145c,_0x31946a[_0x193f('0x40')]);return db[_0x193f('0x2d')]['find'](_0x59145c)['then'](handleEntityNotFound(_0x38da9a,null))[_0x193f('0x26')](respondWithResult(_0x38da9a,null))[_0x193f('0x44')](handleError(_0x38da9a,null));};exports['download']=function(_0x5e7a79,_0x22e208,_0xf1c9d3){return db[_0x193f('0x2d')][_0x193f('0x46')]({'where':{'id':_0x5e7a79[_0x193f('0x47')]['id']},'attributes':[_0x193f('0x2a'),_0x193f('0x48'),_0x193f('0x49')],'raw':!![]})[_0x193f('0x26')](handleEntityNotFound(_0x22e208,null))[_0x193f('0x26')](function(_0x19fa0f){if(_0x19fa0f){if(!fs[_0x193f('0x4a')](path[_0x193f('0x4b')](config[_0x193f('0x4c')],_0x193f('0x4d'),_0x19fa0f[_0x193f('0x48')]+'.'+_0x19fa0f['converted_format']))){throw new db[(_0x193f('0x4e'))][(_0x193f('0x4f'))](_0x193f('0x50'));}return _0x22e208[_0x193f('0x51')](path[_0x193f('0x4b')](config[_0x193f('0x4c')],_0x193f('0x4d'),util[_0x193f('0x52')](_0x193f('0x53'),_0x19fa0f[_0x193f('0x48')],_0x19fa0f[_0x193f('0x49')])),util[_0x193f('0x52')]('%s.%s',_0x19fa0f[_0x193f('0x2a')],_0x19fa0f[_0x193f('0x49')]));}return null;})[_0x193f('0x44')](handleError(_0x22e208,null));};exports[_0x193f('0x54')]=function(_0x81dd77,_0x19c705,_0x333661){var _0x2de4a2={'name':path[_0x193f('0x55')](_0x81dd77[_0x193f('0x56')][_0x193f('0x57')],path[_0x193f('0x58')](_0x81dd77[_0x193f('0x56')][_0x193f('0x57')])),'save_name':path[_0x193f('0x55')](_0x81dd77['file'][_0x193f('0x59')],path[_0x193f('0x58')](_0x81dd77['file']['filename'])),'original_format':path['extname'](_0x81dd77[_0x193f('0x56')][_0x193f('0x57')])[_0x193f('0x5a')](0x1),'converted_format':_0x193f('0x5b')};var _0x4faaf8=path[_0x193f('0x4b')](config[_0x193f('0x4c')],'server/files/sounds/original',_0x81dd77[_0x193f('0x56')][_0x193f('0x59')]);var _0x5e810c=path[_0x193f('0x4b')](config[_0x193f('0x4c')],'server/files/sounds/converted',util[_0x193f('0x52')]('%s.%s',path[_0x193f('0x55')](_0x81dd77[_0x193f('0x56')][_0x193f('0x59')],path['extname'](_0x81dd77[_0x193f('0x56')]['filename'])),_0x2de4a2[_0x193f('0x49')]));logger['info']('Sound\x20upload\x20filepath:\x20%s',_0x4faaf8);logger[_0x193f('0x5c')]('Sound\x20upload\x20destination:\x20%s',_0x5e810c);try{fs[_0x193f('0x5d')](_0x4faaf8,parseInt(_0x193f('0x5e'),0x8));var _0x125b47=sox[_0x193f('0x5f')](_0x4faaf8,_0x5e810c,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x125b47['on'](_0x193f('0x29'),function(_0x4160ea){fs[_0x193f('0x60')](_0x4faaf8);_0x19c705[_0x193f('0x1c')](0x1f4)[_0x193f('0x61')](_0x4160ea);});_0x125b47['on']('src',function(_0x330494){_0x2de4a2['original_duration']=isNaN(_0x330494[_0x193f('0x62')])?null:_0x330494[_0x193f('0x62')]*0x3e8;_0x2de4a2[_0x193f('0x63')]=isNaN(_0x330494[_0x193f('0x64')])?null:_0x330494[_0x193f('0x64')];_0x2de4a2[_0x193f('0x65')]=isNaN(_0x330494[_0x193f('0x66')])?null:_0x330494['channelCount'];_0x2de4a2[_0x193f('0x67')]=isNaN(_0x330494[_0x193f('0x68')])?null:_0x330494[_0x193f('0x68')];_0x2de4a2[_0x193f('0x69')]=isNaN(_0x330494[_0x193f('0x6a')])?null:_0x330494[_0x193f('0x6a')];});_0x125b47['on'](_0x193f('0x6b'),function(_0x377d6f){_0x2de4a2[_0x193f('0x6c')]=isNaN(_0x377d6f[_0x193f('0x62')])?null:_0x377d6f['duration']*0x3e8;_0x2de4a2[_0x193f('0x6d')]=isNaN(_0x377d6f[_0x193f('0x64')])?null:_0x377d6f[_0x193f('0x64')];_0x2de4a2[_0x193f('0x6e')]=isNaN(_0x377d6f[_0x193f('0x66')])?null:_0x377d6f[_0x193f('0x66')];_0x2de4a2[_0x193f('0x6f')]=isNaN(_0x377d6f[_0x193f('0x68')])?null:_0x377d6f['bitRate'];_0x2de4a2[_0x193f('0x70')]=isNaN(_0x377d6f[_0x193f('0x6a')])?null:_0x377d6f['sampleRate'];});_0x125b47['on'](_0x193f('0x1b'),function(){fs[_0x193f('0x5d')](_0x5e810c,parseInt('0777',0x8));return db[_0x193f('0x2d')][_0x193f('0x54')](_0x2de4a2)[_0x193f('0x26')](function(_0x428c46){var _0x4419ad={'role':_0x81dd77[_0x193f('0x71')][_0x193f('0x72')],'userProfileId':_0x81dd77[_0x193f('0x71')][_0x193f('0x73')]};if(!_0x4419ad)throw new Error(_0x193f('0x74'));if(_0x4419ad[_0x193f('0x72')]==='user'){var _0x5494c2=_0x428c46['get']({'plain':!![]});return db[_0x193f('0x75')][_0x193f('0x46')]({'where':{'name':_0x193f('0x76'),'userProfileId':_0x4419ad[_0x193f('0x73')]},'raw':!![]})[_0x193f('0x26')](function(_0x27202e){if(_0x27202e&&_0x27202e[_0x193f('0x77')]===0x0){return db[_0x193f('0x78')][_0x193f('0x54')]({'name':_0x5494c2['name'],'resourceId':_0x5494c2['id'],'type':_0x27202e[_0x193f('0x2a')],'sectionId':_0x27202e['id']},{})[_0x193f('0x26')](function(){return _0x428c46;});}else{return _0x428c46;}})[_0x193f('0x44')](function(_0x47a84b){logger[_0x193f('0x29')](_0x193f('0x79'),_0x47a84b);throw _0x47a84b;});}return _0x428c46;})[_0x193f('0x26')](respondWithResult(_0x19c705,null))['catch'](handleError(_0x19c705,null));});_0x125b47[_0x193f('0x7a')]();}catch(_0x234079){_0x19c705[_0x193f('0x1c')](0x1f4)[_0x193f('0x61')](_0x234079);}};exports['update']=function(_0x582823,_0x1bbe5d,_0x279b28){return db[_0x193f('0x2d')]['find']({'where':{'id':_0x582823['params']['id']}})[_0x193f('0x26')](handleEntityNotFound(_0x1bbe5d,null))[_0x193f('0x26')](saveUpdates(_[_0x193f('0x3c')](_0x582823[_0x193f('0x71')],[_0x193f('0x2a'),'description']),null))[_0x193f('0x26')](respondWithResult(_0x1bbe5d,null))[_0x193f('0x44')](handleError(_0x1bbe5d,null));};exports[_0x193f('0x7b')]=function(_0xce0825,_0x34f4cc,_0x21a8d1){return db[_0x193f('0x2d')]['find']({'where':{'id':_0xce0825[_0x193f('0x47')]['id']}})[_0x193f('0x26')](function(_0x53d3bf){if(_0x53d3bf){return _0x53d3bf[_0x193f('0x27')]();}})['then'](function(_0x36aa68){var _0x387411=path[_0x193f('0x4b')](config[_0x193f('0x4c')],_0x193f('0x7c'),util[_0x193f('0x52')](_0x193f('0x53'),_0x36aa68[_0x193f('0x48')],_0x36aa68[_0x193f('0x7d')]));var _0x5bbe58=path[_0x193f('0x4b')](config[_0x193f('0x4c')],_0x193f('0x4d'),util[_0x193f('0x52')](_0x193f('0x53'),_0x36aa68[_0x193f('0x48')],_0x36aa68[_0x193f('0x49')]));fs[_0x193f('0x60')](_0x387411);fs[_0x193f('0x60')](_0x5bbe58);return _0x36aa68;})[_0x193f('0x26')](function(_0xe1dc11){if(_0xe1dc11){return _0x34f4cc[_0x193f('0x1c')](0xcc)[_0x193f('0x1b')]();}})['then'](handleEntityNotFound(_0x34f4cc,null))[_0x193f('0x44')](handleError(_0x34f4cc,null));};