c42484d7c7f41be91560c267eb9ae9e048b441ba
[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 _0xfef2=['fields','hasOwnProperty','nolimit','limit','order','where','pick','filter','merge','map','type','VIRTUAL','options','Sound','includeAll','include','findAll','rows','catch','show','params','keys','length','find','download','save_name','existsSync','join','root','converted_format','Sequelize','Sound\x20not\x20found','server/files/sounds/converted','%s.%s','format','basename','originalname','extname','file','filename','substring','wav','server/files/sounds/original','info','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s','transcode','unlinkSync','src','original_duration','duration','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_sampleCount','converted_channelCount','converted_sampleRate','chmodSync','create','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','Sounds','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','update','description','original_format','eml-format','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','count','offset','status','set','Content-Range','apply','reject','save','then','destroy','get','UserProfileResource','error','name','send','index','rawAttributes','key','query','filters','intersection','model','attributes'];(function(_0x5dbee4,_0x2a8739){var _0x55501e=function(_0x43ead5){while(--_0x43ead5){_0x5dbee4['push'](_0x5dbee4['shift']());}};_0x55501e(++_0x2a8739);}(_0xfef2,0xc8));var _0x2fef=function(_0x2a1185,_0x2740c5){_0x2a1185=_0x2a1185-0x0;var _0x5588d9=_0xfef2[_0x2a1185];return _0x5588d9;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x2fef('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x2fef('0x1'));var jsonpatch=require(_0x2fef('0x2'));var rp=require(_0x2fef('0x3'));var moment=require(_0x2fef('0x4'));var BPromise=require(_0x2fef('0x5'));var Mustache=require(_0x2fef('0x6'));var util=require('util');var path=require(_0x2fef('0x7'));var sox=require('sox');var csv=require(_0x2fef('0x8'));var ejs=require('ejs');var fs=require('fs');var _=require(_0x2fef('0x9'));var squel=require(_0x2fef('0xa'));var crypto=require(_0x2fef('0xb'));var jsforce=require(_0x2fef('0xc'));var deskjs=require(_0x2fef('0xd'));var toCsv=require(_0x2fef('0x8'));var querystring=require(_0x2fef('0xe'));var Papa=require('papaparse');var Redis=require(_0x2fef('0xf'));var authService=require('../../components/auth/service');var qs=require(_0x2fef('0x10'));var as=require(_0x2fef('0x11'));var hardwareService=require(_0x2fef('0x12'));var logger=require(_0x2fef('0x13'))(_0x2fef('0x14'));var utils=require(_0x2fef('0x15'));var config=require(_0x2fef('0x16'));var licenseUtil=require(_0x2fef('0x17'));var db=require(_0x2fef('0x18'))['db'];function respondWithStatusCode(_0x2a281a,_0x175e76){_0x175e76=_0x175e76||0xcc;return function(_0x401b6d){if(_0x401b6d){return _0x2a281a[_0x2fef('0x19')](_0x175e76);}return _0x2a281a['status'](_0x175e76)[_0x2fef('0x1a')]();};}function respondWithResult(_0x3e16c5,_0xdd72b6){_0xdd72b6=_0xdd72b6||0xc8;return function(_0x335fe7){if(_0x335fe7){return _0x3e16c5['status'](_0xdd72b6)['json'](_0x335fe7);}};}function respondWithFilteredResult(_0x4b2002,_0x4d94a8){return function(_0x559543){if(_0x559543){var _0x14c41f=_0x559543[_0x2fef('0x1b')],_0x2dd32e=_0x4d94a8[_0x2fef('0x1c')],_0x7d9b4a=_0x4d94a8[_0x2fef('0x1c')]+_0x4d94a8['limit'],_0x185b23;if(_0x7d9b4a>=_0x14c41f){_0x7d9b4a=_0x14c41f;_0x185b23=0xc8;}else{_0x185b23=0xce;}_0x4b2002[_0x2fef('0x1d')](_0x185b23);return _0x4b2002[_0x2fef('0x1e')](_0x2fef('0x1f'),_0x2dd32e+'-'+_0x7d9b4a+'/'+_0x14c41f)['json'](_0x559543);}return null;};}function patchUpdates(_0x7aaa6d){return function(_0x11878e){try{jsonpatch[_0x2fef('0x20')](_0x11878e,_0x7aaa6d,!![]);}catch(_0x2ecf51){return BPromise[_0x2fef('0x21')](_0x2ecf51);}return _0x11878e[_0x2fef('0x22')]();};}function saveUpdates(_0x2c9660,_0x568574){return function(_0x1622f3){if(_0x1622f3){return _0x1622f3['update'](_0x2c9660)[_0x2fef('0x23')](function(_0x1af890){return _0x1af890;});}return null;};}function removeEntity(_0x3a2607,_0x314076){return function(_0x5a55fb){if(_0x5a55fb){return _0x5a55fb[_0x2fef('0x24')]()[_0x2fef('0x23')](function(){var _0x3cec72=_0x5a55fb[_0x2fef('0x25')]({'plain':!![]});var _0x5290df='Sounds';return db[_0x2fef('0x26')][_0x2fef('0x24')]({'where':{'type':_0x5290df,'resourceId':_0x3cec72['id']}})[_0x2fef('0x23')](function(){return _0x5a55fb;});})['then'](function(){_0x3a2607[_0x2fef('0x1d')](0xcc)[_0x2fef('0x1a')]();});}};}function handleEntityNotFound(_0x3851cb,_0x27e2f7){return function(_0x57f3fb){if(!_0x57f3fb){_0x3851cb['sendStatus'](0x194);}return _0x57f3fb;};}function handleError(_0xa77310,_0x5d791f){_0x5d791f=_0x5d791f||0x1f4;return function(_0x3de48f){logger[_0x2fef('0x27')](_0x3de48f['stack']);if(_0x3de48f[_0x2fef('0x28')]){delete _0x3de48f[_0x2fef('0x28')];}_0xa77310['status'](_0x5d791f)[_0x2fef('0x29')](_0x3de48f);};}exports[_0x2fef('0x2a')]=function(_0x5e40e1,_0x9ef539){var _0x1c3859={},_0x201d12={},_0x344ae9={'count':0x0,'rows':[]};var _0x17d6cf=_['map'](db['Sound'][_0x2fef('0x2b')],function(_0x185b22){return{'name':_0x185b22['fieldName'],'type':_0x185b22['type'][_0x2fef('0x2c')]};});_0x201d12['model']=_['map'](_0x17d6cf,_0x2fef('0x28'));_0x201d12[_0x2fef('0x2d')]=_['keys'](_0x5e40e1[_0x2fef('0x2d')]);_0x201d12[_0x2fef('0x2e')]=_[_0x2fef('0x2f')](_0x201d12[_0x2fef('0x30')],_0x201d12[_0x2fef('0x2d')]);_0x1c3859[_0x2fef('0x31')]=_['intersection'](_0x201d12[_0x2fef('0x30')],qs[_0x2fef('0x32')](_0x5e40e1['query'][_0x2fef('0x32')]));_0x1c3859[_0x2fef('0x31')]=_0x1c3859[_0x2fef('0x31')]['length']?_0x1c3859['attributes']:_0x201d12[_0x2fef('0x30')];if(!_0x5e40e1['query'][_0x2fef('0x33')](_0x2fef('0x34'))){_0x1c3859[_0x2fef('0x35')]=qs['limit'](_0x5e40e1[_0x2fef('0x2d')][_0x2fef('0x35')]);_0x1c3859[_0x2fef('0x1c')]=qs['offset'](_0x5e40e1[_0x2fef('0x2d')][_0x2fef('0x1c')]);}_0x1c3859[_0x2fef('0x36')]=qs['sort'](_0x5e40e1['query']['sort']);_0x1c3859[_0x2fef('0x37')]=qs[_0x2fef('0x2e')](_[_0x2fef('0x38')](_0x5e40e1[_0x2fef('0x2d')],_0x201d12[_0x2fef('0x2e')]),_0x17d6cf);if(_0x5e40e1['query'][_0x2fef('0x39')]){_0x1c3859['where']=_[_0x2fef('0x3a')](_0x1c3859[_0x2fef('0x37')],{'$or':_[_0x2fef('0x3b')](_0x17d6cf,function(_0x13c767){if(_0x13c767[_0x2fef('0x3c')]!==_0x2fef('0x3d')){var _0x1969dc={};_0x1969dc[_0x13c767[_0x2fef('0x28')]]={'$like':'%'+_0x5e40e1[_0x2fef('0x2d')][_0x2fef('0x39')]+'%'};return _0x1969dc;}})});}_0x1c3859=_[_0x2fef('0x3a')]({},_0x1c3859,_0x5e40e1[_0x2fef('0x3e')]);var _0x2871d5={'where':_0x1c3859['where']};return db[_0x2fef('0x3f')][_0x2fef('0x1b')](_0x2871d5)[_0x2fef('0x23')](function(_0x250948){_0x344ae9[_0x2fef('0x1b')]=_0x250948;if(_0x5e40e1[_0x2fef('0x2d')][_0x2fef('0x40')]){_0x1c3859[_0x2fef('0x41')]=[{'all':!![]}];}return db[_0x2fef('0x3f')][_0x2fef('0x42')](_0x1c3859);})[_0x2fef('0x23')](function(_0x354f9c){_0x344ae9[_0x2fef('0x43')]=_0x354f9c;return _0x344ae9;})['then'](respondWithFilteredResult(_0x9ef539,_0x1c3859))[_0x2fef('0x44')](handleError(_0x9ef539,null));};exports[_0x2fef('0x45')]=function(_0x171b55,_0x362297){var _0x26833b={'raw':!![],'where':{'id':_0x171b55[_0x2fef('0x46')]['id']}},_0x135806={};_0x135806[_0x2fef('0x30')]=_[_0x2fef('0x47')](db[_0x2fef('0x3f')][_0x2fef('0x2b')]);_0x135806[_0x2fef('0x2d')]=_[_0x2fef('0x47')](_0x171b55[_0x2fef('0x2d')]);_0x135806[_0x2fef('0x2e')]=_[_0x2fef('0x2f')](_0x135806[_0x2fef('0x30')],_0x135806[_0x2fef('0x2d')]);_0x26833b[_0x2fef('0x31')]=_[_0x2fef('0x2f')](_0x135806[_0x2fef('0x30')],qs[_0x2fef('0x32')](_0x171b55[_0x2fef('0x2d')][_0x2fef('0x32')]));_0x26833b[_0x2fef('0x31')]=_0x26833b[_0x2fef('0x31')][_0x2fef('0x48')]?_0x26833b[_0x2fef('0x31')]:_0x135806['model'];if(_0x171b55[_0x2fef('0x2d')]['includeAll']){_0x26833b[_0x2fef('0x41')]=[{'all':!![]}];}_0x26833b=_['merge']({},_0x26833b,_0x171b55['options']);return db[_0x2fef('0x3f')][_0x2fef('0x49')](_0x26833b)[_0x2fef('0x23')](handleEntityNotFound(_0x362297,null))[_0x2fef('0x23')](respondWithResult(_0x362297,null))[_0x2fef('0x44')](handleError(_0x362297,null));};exports[_0x2fef('0x4a')]=function(_0x20a518,_0xad4dad,_0x3100fa){return db['Sound'][_0x2fef('0x49')]({'where':{'id':_0x20a518[_0x2fef('0x46')]['id']},'attributes':[_0x2fef('0x28'),_0x2fef('0x4b'),'converted_format'],'raw':!![]})[_0x2fef('0x23')](handleEntityNotFound(_0xad4dad,null))['then'](function(_0x1645d1){if(_0x1645d1){if(!fs[_0x2fef('0x4c')](path[_0x2fef('0x4d')](config[_0x2fef('0x4e')],'server/files/sounds/converted',_0x1645d1[_0x2fef('0x4b')]+'.'+_0x1645d1[_0x2fef('0x4f')]))){throw new db[(_0x2fef('0x50'))]['ValidationError'](_0x2fef('0x51'));}return _0xad4dad[_0x2fef('0x4a')](path['join'](config[_0x2fef('0x4e')],_0x2fef('0x52'),util['format'](_0x2fef('0x53'),_0x1645d1[_0x2fef('0x4b')],_0x1645d1[_0x2fef('0x4f')])),util[_0x2fef('0x54')]('%s.%s',_0x1645d1[_0x2fef('0x28')],_0x1645d1[_0x2fef('0x4f')]));}return null;})['catch'](handleError(_0xad4dad,null));};exports['create']=function(_0x43482b,_0x3c728d,_0x4d5e62){var _0x18db56={'name':path[_0x2fef('0x55')](_0x43482b['file'][_0x2fef('0x56')],path[_0x2fef('0x57')](_0x43482b[_0x2fef('0x58')][_0x2fef('0x56')])),'save_name':path['basename'](_0x43482b[_0x2fef('0x58')][_0x2fef('0x59')],path[_0x2fef('0x57')](_0x43482b[_0x2fef('0x58')][_0x2fef('0x59')])),'original_format':path[_0x2fef('0x57')](_0x43482b[_0x2fef('0x58')]['originalname'])[_0x2fef('0x5a')](0x1),'converted_format':_0x2fef('0x5b')};var _0x43ef3b=path[_0x2fef('0x4d')](config['root'],_0x2fef('0x5c'),_0x43482b['file']['filename']);var _0x1bcb92=path[_0x2fef('0x4d')](config[_0x2fef('0x4e')],_0x2fef('0x52'),util[_0x2fef('0x54')](_0x2fef('0x53'),path[_0x2fef('0x55')](_0x43482b[_0x2fef('0x58')][_0x2fef('0x59')],path[_0x2fef('0x57')](_0x43482b[_0x2fef('0x58')][_0x2fef('0x59')])),_0x18db56[_0x2fef('0x4f')]));logger[_0x2fef('0x5d')](_0x2fef('0x5e'),_0x43ef3b);logger['info'](_0x2fef('0x5f'),_0x1bcb92);try{fs['chmodSync'](_0x43ef3b,parseInt('0777',0x8));var _0x40f284=sox[_0x2fef('0x60')](_0x43ef3b,_0x1bcb92,{'sampleRate':0x1f40,'format':_0x2fef('0x5b'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x40f284['on'](_0x2fef('0x27'),function(_0x41189a){fs[_0x2fef('0x61')](_0x43ef3b);_0x3c728d[_0x2fef('0x1d')](0x1f4)[_0x2fef('0x29')](_0x41189a);});_0x40f284['on'](_0x2fef('0x62'),function(_0xb5b155){_0x18db56[_0x2fef('0x63')]=isNaN(_0xb5b155[_0x2fef('0x64')])?null:_0xb5b155[_0x2fef('0x64')]*0x3e8;_0x18db56['original_sampleCount']=isNaN(_0xb5b155[_0x2fef('0x65')])?null:_0xb5b155[_0x2fef('0x65')];_0x18db56[_0x2fef('0x66')]=isNaN(_0xb5b155[_0x2fef('0x67')])?null:_0xb5b155[_0x2fef('0x67')];_0x18db56[_0x2fef('0x68')]=isNaN(_0xb5b155[_0x2fef('0x69')])?null:_0xb5b155[_0x2fef('0x69')];_0x18db56[_0x2fef('0x6a')]=isNaN(_0xb5b155[_0x2fef('0x6b')])?null:_0xb5b155['sampleRate'];});_0x40f284['on'](_0x2fef('0x6c'),function(_0x17c7ec){_0x18db56['converted_duration']=isNaN(_0x17c7ec[_0x2fef('0x64')])?null:_0x17c7ec['duration']*0x3e8;_0x18db56[_0x2fef('0x6d')]=isNaN(_0x17c7ec[_0x2fef('0x65')])?null:_0x17c7ec['sampleCount'];_0x18db56[_0x2fef('0x6e')]=isNaN(_0x17c7ec[_0x2fef('0x67')])?null:_0x17c7ec['channelCount'];_0x18db56['converted_bitRate']=isNaN(_0x17c7ec['bitRate'])?null:_0x17c7ec['bitRate'];_0x18db56[_0x2fef('0x6f')]=isNaN(_0x17c7ec[_0x2fef('0x6b')])?null:_0x17c7ec['sampleRate'];});_0x40f284['on'](_0x2fef('0x1a'),function(){fs[_0x2fef('0x70')](_0x1bcb92,parseInt('0777',0x8));return db['Sound'][_0x2fef('0x71')](_0x18db56)['then'](function(_0x336d12){var _0x1c4261={'role':_0x43482b['body'][_0x2fef('0x72')],'userProfileId':_0x43482b['body'][_0x2fef('0x73')]};if(!_0x1c4261)throw new Error(_0x2fef('0x74'));if(_0x1c4261['role']===_0x2fef('0x75')){var _0x583ff0=_0x336d12['get']({'plain':!![]});return db[_0x2fef('0x76')][_0x2fef('0x49')]({'where':{'name':_0x2fef('0x77'),'userProfileId':_0x1c4261[_0x2fef('0x73')]},'raw':!![]})['then'](function(_0x4db1e1){if(_0x4db1e1&&_0x4db1e1[_0x2fef('0x78')]===0x0){return db[_0x2fef('0x26')][_0x2fef('0x71')]({'name':_0x583ff0[_0x2fef('0x28')],'resourceId':_0x583ff0['id'],'type':_0x4db1e1[_0x2fef('0x28')],'sectionId':_0x4db1e1['id']},{})[_0x2fef('0x23')](function(){return _0x336d12;});}else{return _0x336d12;}})['catch'](function(_0x3647e4){logger[_0x2fef('0x27')](_0x2fef('0x79'),_0x3647e4);throw _0x3647e4;});}return _0x336d12;})[_0x2fef('0x23')](respondWithResult(_0x3c728d,null))[_0x2fef('0x44')](handleError(_0x3c728d,null));});_0x40f284['start']();}catch(_0x74e29c){_0x3c728d[_0x2fef('0x1d')](0x1f4)['send'](_0x74e29c);}};exports[_0x2fef('0x7a')]=function(_0x5191ee,_0x37ec7b,_0xd57859){return db[_0x2fef('0x3f')][_0x2fef('0x49')]({'where':{'id':_0x5191ee[_0x2fef('0x46')]['id']}})[_0x2fef('0x23')](handleEntityNotFound(_0x37ec7b,null))[_0x2fef('0x23')](saveUpdates(_[_0x2fef('0x38')](_0x5191ee['body'],[_0x2fef('0x28'),_0x2fef('0x7b')]),null))[_0x2fef('0x23')](respondWithResult(_0x37ec7b,null))[_0x2fef('0x44')](handleError(_0x37ec7b,null));};exports['delete']=function(_0xec4790,_0x1908ff,_0x496a71){return db['Sound']['find']({'where':{'id':_0xec4790[_0x2fef('0x46')]['id']}})[_0x2fef('0x23')](function(_0x4b1b36){if(_0x4b1b36){return _0x4b1b36[_0x2fef('0x24')]();}})[_0x2fef('0x23')](function(_0x1730de){var _0x193782=path[_0x2fef('0x4d')](config[_0x2fef('0x4e')],_0x2fef('0x5c'),util[_0x2fef('0x54')](_0x2fef('0x53'),_0x1730de[_0x2fef('0x4b')],_0x1730de[_0x2fef('0x7c')]));var _0x37d63f=path[_0x2fef('0x4d')](config['root'],_0x2fef('0x52'),util[_0x2fef('0x54')](_0x2fef('0x53'),_0x1730de[_0x2fef('0x4b')],_0x1730de[_0x2fef('0x4f')]));fs[_0x2fef('0x61')](_0x193782);fs[_0x2fef('0x61')](_0x37d63f);return _0x1730de;})[_0x2fef('0x23')](function(_0x23920c){if(_0x23920c){return _0x1908ff[_0x2fef('0x1d')](0xcc)[_0x2fef('0x1a')]();}})[_0x2fef('0x23')](handleEntityNotFound(_0x1908ff,null))[_0x2fef('0x44')](handleError(_0x1908ff,null));};