544c07d4c7c7d264e9f4ac21084e34a32c937032
[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 _0x374a=['map','Sound','rawAttributes','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','order','where','filter','type','VIRTUAL','merge','options','include','catch','show','params','includeAll','find','download','save_name','converted_format','existsSync','join','server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','root','format','%s.%s','create','file','originalname','extname','filename','substring','wav','server/files/sounds/original','basename','info','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','error','unlinkSync','src','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','bitRate','original_sampleRate','sampleRate','converted_duration','converted_sampleCount','channelCount','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','update','pick','delete','rimraf','fast-json-patch','request-promise','mustache','util','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','apply','then','get','Sounds','UserProfileResource','destroy','name','send','index'];(function(_0x3147de,_0x98105f){var _0x4ad14c=function(_0x411a8b){while(--_0x411a8b){_0x3147de['push'](_0x3147de['shift']());}};_0x4ad14c(++_0x98105f);}(_0x374a,0x137));var _0xa374=function(_0x3eaa53,_0x2ab4b1){_0x3eaa53=_0x3eaa53-0x0;var _0x33b017=_0x374a[_0x3eaa53];return _0x33b017;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xa374('0x0'));var zipdir=require('zip-dir');var jsonpatch=require(_0xa374('0x1'));var rp=require(_0xa374('0x2'));var moment=require('moment');var BPromise=require('bluebird');var Mustache=require(_0xa374('0x3'));var util=require(_0xa374('0x4'));var path=require('path');var sox=require('sox');var csv=require(_0xa374('0x5'));var ejs=require(_0xa374('0x6'));var fs=require('fs');var fs_extra=require(_0xa374('0x7'));var _=require('lodash');var squel=require(_0xa374('0x8'));var crypto=require(_0xa374('0x9'));var jsforce=require(_0xa374('0xa'));var deskjs=require(_0xa374('0xb'));var toCsv=require(_0xa374('0x5'));var querystring=require(_0xa374('0xc'));var Papa=require('papaparse');var Redis=require(_0xa374('0xd'));var authService=require('../../components/auth/service');var qs=require(_0xa374('0xe'));var as=require(_0xa374('0xf'));var hardwareService=require(_0xa374('0x10'));var logger=require('../../config/logger')(_0xa374('0x11'));var utils=require(_0xa374('0x12'));var config=require(_0xa374('0x13'));var licenseUtil=require(_0xa374('0x14'));var db=require(_0xa374('0x15'))['db'];function respondWithStatusCode(_0x3dd114,_0x4dd569){_0x4dd569=_0x4dd569||0xcc;return function(_0x753faf){if(_0x753faf){return _0x3dd114[_0xa374('0x16')](_0x4dd569);}return _0x3dd114[_0xa374('0x17')](_0x4dd569)[_0xa374('0x18')]();};}function respondWithResult(_0xcf828c,_0xca8bab){_0xca8bab=_0xca8bab||0xc8;return function(_0x3cde74){if(_0x3cde74){return _0xcf828c[_0xa374('0x17')](_0xca8bab)[_0xa374('0x19')](_0x3cde74);}};}function respondWithFilteredResult(_0x3f42ba,_0xf736c7){return function(_0x4c7307){if(_0x4c7307){var _0x29259b=typeof _0xf736c7[_0xa374('0x1a')]===_0xa374('0x1b')&&typeof _0xf736c7[_0xa374('0x1c')]===_0xa374('0x1b');var _0x137eff=_0x4c7307['count'];var _0x4205f0=_0x29259b?0x0:_0xf736c7['offset'];var _0x1df622=_0x29259b?_0x4c7307[_0xa374('0x1d')]:_0xf736c7[_0xa374('0x1a')]+_0xf736c7['limit'];var _0x2be8e2;if(_0x1df622>=_0x137eff){_0x1df622=_0x137eff;_0x2be8e2=0xc8;}else{_0x2be8e2=0xce;}_0x3f42ba[_0xa374('0x17')](_0x2be8e2);return _0x3f42ba[_0xa374('0x1e')]('Content-Range',_0x4205f0+'-'+_0x1df622+'/'+_0x137eff)[_0xa374('0x19')](_0x4c7307);}return null;};}function patchUpdates(_0x474612){return function(_0x429062){try{jsonpatch[_0xa374('0x1f')](_0x429062,_0x474612,!![]);}catch(_0x2db728){return BPromise['reject'](_0x2db728);}return _0x429062['save']();};}function saveUpdates(_0x226200,_0x3b51e6){return function(_0x59200a){if(_0x59200a){return _0x59200a['update'](_0x226200)[_0xa374('0x20')](function(_0x521408){return _0x521408;});}return null;};}function removeEntity(_0x29083f,_0x9800fc){return function(_0x199758){if(_0x199758){return _0x199758['destroy']()['then'](function(){var _0x1ca529=_0x199758[_0xa374('0x21')]({'plain':!![]});var _0x267a69=_0xa374('0x22');return db[_0xa374('0x23')][_0xa374('0x24')]({'where':{'type':_0x267a69,'resourceId':_0x1ca529['id']}})[_0xa374('0x20')](function(){return _0x199758;});})[_0xa374('0x20')](function(){_0x29083f['status'](0xcc)[_0xa374('0x18')]();});}};}function handleEntityNotFound(_0x13330a,_0x3f6c62){return function(_0x3220b4){if(!_0x3220b4){_0x13330a[_0xa374('0x16')](0x194);}return _0x3220b4;};}function handleError(_0x36cc29,_0x33fe33){_0x33fe33=_0x33fe33||0x1f4;return function(_0x29091b){logger['error'](_0x29091b['stack']);if(_0x29091b['name']){delete _0x29091b[_0xa374('0x25')];}_0x36cc29[_0xa374('0x17')](_0x33fe33)[_0xa374('0x26')](_0x29091b);};}exports[_0xa374('0x27')]=function(_0xb37785,_0xb526e2){var _0x313ccb={},_0x34f932={},_0x1125eb={'count':0x0,'rows':[]};var _0x884a40=_[_0xa374('0x28')](db[_0xa374('0x29')][_0xa374('0x2a')],function(_0x24a952){return{'name':_0x24a952['fieldName'],'type':_0x24a952['type']['key']};});_0x34f932[_0xa374('0x2b')]=_[_0xa374('0x28')](_0x884a40,'name');_0x34f932[_0xa374('0x2c')]=_[_0xa374('0x2d')](_0xb37785[_0xa374('0x2c')]);_0x34f932[_0xa374('0x2e')]=_[_0xa374('0x2f')](_0x34f932[_0xa374('0x2b')],_0x34f932['query']);_0x313ccb[_0xa374('0x30')]=_[_0xa374('0x2f')](_0x34f932['model'],qs['fields'](_0xb37785[_0xa374('0x2c')][_0xa374('0x31')]));_0x313ccb[_0xa374('0x30')]=_0x313ccb['attributes'][_0xa374('0x32')]?_0x313ccb[_0xa374('0x30')]:_0x34f932[_0xa374('0x2b')];if(!_0xb37785[_0xa374('0x2c')][_0xa374('0x33')]('nolimit')){_0x313ccb['limit']=qs[_0xa374('0x1c')](_0xb37785[_0xa374('0x2c')][_0xa374('0x1c')]);_0x313ccb[_0xa374('0x1a')]=qs['offset'](_0xb37785[_0xa374('0x2c')][_0xa374('0x1a')]);}_0x313ccb[_0xa374('0x34')]=qs['sort'](_0xb37785[_0xa374('0x2c')]['sort']);_0x313ccb[_0xa374('0x35')]=qs[_0xa374('0x2e')](_['pick'](_0xb37785[_0xa374('0x2c')],_0x34f932[_0xa374('0x2e')]),_0x884a40);if(_0xb37785[_0xa374('0x2c')][_0xa374('0x36')]){_0x313ccb[_0xa374('0x35')]=_['merge'](_0x313ccb[_0xa374('0x35')],{'$or':_['map'](_0x884a40,function(_0x11d584){if(_0x11d584[_0xa374('0x37')]!==_0xa374('0x38')){var _0x259b57={};_0x259b57[_0x11d584[_0xa374('0x25')]]={'$like':'%'+_0xb37785[_0xa374('0x2c')][_0xa374('0x36')]+'%'};return _0x259b57;}})});}_0x313ccb=_[_0xa374('0x39')]({},_0x313ccb,_0xb37785[_0xa374('0x3a')]);var _0x489d04={'where':_0x313ccb[_0xa374('0x35')]};return db['Sound'][_0xa374('0x1d')](_0x489d04)[_0xa374('0x20')](function(_0x2234bf){_0x1125eb[_0xa374('0x1d')]=_0x2234bf;if(_0xb37785['query']['includeAll']){_0x313ccb[_0xa374('0x3b')]=[{'all':!![]}];}return db[_0xa374('0x29')]['findAll'](_0x313ccb);})['then'](function(_0x10837a){_0x1125eb['rows']=_0x10837a;return _0x1125eb;})['then'](respondWithFilteredResult(_0xb526e2,_0x313ccb))[_0xa374('0x3c')](handleError(_0xb526e2,null));};exports[_0xa374('0x3d')]=function(_0x4b4ef4,_0x3b03ec){var _0x5aec88={'raw':!![],'where':{'id':_0x4b4ef4[_0xa374('0x3e')]['id']}},_0x5863ed={};_0x5863ed['model']=_[_0xa374('0x2d')](db[_0xa374('0x29')][_0xa374('0x2a')]);_0x5863ed[_0xa374('0x2c')]=_['keys'](_0x4b4ef4[_0xa374('0x2c')]);_0x5863ed[_0xa374('0x2e')]=_[_0xa374('0x2f')](_0x5863ed[_0xa374('0x2b')],_0x5863ed[_0xa374('0x2c')]);_0x5aec88['attributes']=_[_0xa374('0x2f')](_0x5863ed[_0xa374('0x2b')],qs[_0xa374('0x31')](_0x4b4ef4[_0xa374('0x2c')]['fields']));_0x5aec88[_0xa374('0x30')]=_0x5aec88['attributes']['length']?_0x5aec88[_0xa374('0x30')]:_0x5863ed['model'];if(_0x4b4ef4[_0xa374('0x2c')][_0xa374('0x3f')]){_0x5aec88[_0xa374('0x3b')]=[{'all':!![]}];}_0x5aec88=_['merge']({},_0x5aec88,_0x4b4ef4['options']);return db[_0xa374('0x29')][_0xa374('0x40')](_0x5aec88)[_0xa374('0x20')](handleEntityNotFound(_0x3b03ec,null))[_0xa374('0x20')](respondWithResult(_0x3b03ec,null))[_0xa374('0x3c')](handleError(_0x3b03ec,null));};exports[_0xa374('0x41')]=function(_0x313a70,_0xd491bd,_0x102055){return db[_0xa374('0x29')][_0xa374('0x40')]({'where':{'id':_0x313a70[_0xa374('0x3e')]['id']},'attributes':['name',_0xa374('0x42'),_0xa374('0x43')],'raw':!![]})[_0xa374('0x20')](handleEntityNotFound(_0xd491bd,null))[_0xa374('0x20')](function(_0x30ee99){if(_0x30ee99){if(!fs[_0xa374('0x44')](path[_0xa374('0x45')](config['root'],_0xa374('0x46'),_0x30ee99['save_name']+'.'+_0x30ee99[_0xa374('0x43')]))){throw new db[(_0xa374('0x47'))][(_0xa374('0x48'))](_0xa374('0x49'));}return _0xd491bd['download'](path[_0xa374('0x45')](config[_0xa374('0x4a')],_0xa374('0x46'),util[_0xa374('0x4b')](_0xa374('0x4c'),_0x30ee99['save_name'],_0x30ee99[_0xa374('0x43')])),util[_0xa374('0x4b')](_0xa374('0x4c'),_0x30ee99[_0xa374('0x25')],_0x30ee99[_0xa374('0x43')]));}return null;})[_0xa374('0x3c')](handleError(_0xd491bd,null));};exports[_0xa374('0x4d')]=function(_0x1e8e9b,_0x54dacf,_0x5c5b86){var _0x3d7eaa={'name':path['basename'](_0x1e8e9b[_0xa374('0x4e')][_0xa374('0x4f')],path[_0xa374('0x50')](_0x1e8e9b[_0xa374('0x4e')][_0xa374('0x4f')])),'save_name':path['basename'](_0x1e8e9b['file'][_0xa374('0x51')],path[_0xa374('0x50')](_0x1e8e9b[_0xa374('0x4e')]['filename'])),'original_format':path[_0xa374('0x50')](_0x1e8e9b[_0xa374('0x4e')][_0xa374('0x4f')])[_0xa374('0x52')](0x1),'converted_format':_0xa374('0x53')};var _0x510bf7=path[_0xa374('0x45')](config['root'],_0xa374('0x54'),_0x1e8e9b[_0xa374('0x4e')][_0xa374('0x51')]);var _0x22fbdc=path['join'](config[_0xa374('0x4a')],_0xa374('0x46'),util[_0xa374('0x4b')](_0xa374('0x4c'),path[_0xa374('0x55')](_0x1e8e9b[_0xa374('0x4e')]['filename'],path[_0xa374('0x50')](_0x1e8e9b[_0xa374('0x4e')][_0xa374('0x51')])),_0x3d7eaa[_0xa374('0x43')]));logger[_0xa374('0x56')]('Sound\x20upload\x20filepath:\x20%s',_0x510bf7);logger[_0xa374('0x56')](_0xa374('0x57'),_0x22fbdc);try{fs[_0xa374('0x58')](_0x510bf7,parseInt(_0xa374('0x59'),0x8));var _0xb127b8=sox[_0xa374('0x5a')](_0x510bf7,_0x22fbdc,{'sampleRate':0x1f40,'format':_0xa374('0x53'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0xb127b8['on'](_0xa374('0x5b'),function(_0x4263e9){fs[_0xa374('0x5c')](_0x510bf7);_0x54dacf[_0xa374('0x17')](0x1f4)[_0xa374('0x26')](_0x4263e9);});_0xb127b8['on'](_0xa374('0x5d'),function(_0x46fad4){_0x3d7eaa[_0xa374('0x5e')]=isNaN(_0x46fad4[_0xa374('0x5f')])?null:_0x46fad4['duration']*0x3e8;_0x3d7eaa[_0xa374('0x60')]=isNaN(_0x46fad4[_0xa374('0x61')])?null:_0x46fad4[_0xa374('0x61')];_0x3d7eaa[_0xa374('0x62')]=isNaN(_0x46fad4['channelCount'])?null:_0x46fad4['channelCount'];_0x3d7eaa['original_bitRate']=isNaN(_0x46fad4[_0xa374('0x63')])?null:_0x46fad4[_0xa374('0x63')];_0x3d7eaa[_0xa374('0x64')]=isNaN(_0x46fad4[_0xa374('0x65')])?null:_0x46fad4[_0xa374('0x65')];});_0xb127b8['on']('dest',function(_0x149912){_0x3d7eaa[_0xa374('0x66')]=isNaN(_0x149912[_0xa374('0x5f')])?null:_0x149912[_0xa374('0x5f')]*0x3e8;_0x3d7eaa[_0xa374('0x67')]=isNaN(_0x149912[_0xa374('0x61')])?null:_0x149912[_0xa374('0x61')];_0x3d7eaa['converted_channelCount']=isNaN(_0x149912[_0xa374('0x68')])?null:_0x149912['channelCount'];_0x3d7eaa['converted_bitRate']=isNaN(_0x149912[_0xa374('0x63')])?null:_0x149912['bitRate'];_0x3d7eaa['converted_sampleRate']=isNaN(_0x149912[_0xa374('0x65')])?null:_0x149912[_0xa374('0x65')];});_0xb127b8['on']('end',function(){fs[_0xa374('0x58')](_0x22fbdc,parseInt('0777',0x8));return db[_0xa374('0x29')][_0xa374('0x4d')](_0x3d7eaa)[_0xa374('0x20')](function(_0x18d3ec){var _0x4bb157={'role':_0x1e8e9b[_0xa374('0x69')][_0xa374('0x6a')],'userProfileId':_0x1e8e9b['body'][_0xa374('0x6b')]};if(!_0x4bb157)throw new Error(_0xa374('0x6c'));if(_0x4bb157[_0xa374('0x6a')]===_0xa374('0x6d')){var _0x5e3456=_0x18d3ec[_0xa374('0x21')]({'plain':!![]});return db[_0xa374('0x6e')]['find']({'where':{'name':'Sounds','userProfileId':_0x4bb157[_0xa374('0x6b')]},'raw':!![]})[_0xa374('0x20')](function(_0x1287bf){if(_0x1287bf&&_0x1287bf[_0xa374('0x6f')]===0x0){return db['UserProfileResource']['create']({'name':_0x5e3456[_0xa374('0x25')],'resourceId':_0x5e3456['id'],'type':_0x1287bf['name'],'sectionId':_0x1287bf['id']},{})[_0xa374('0x20')](function(){return _0x18d3ec;});}else{return _0x18d3ec;}})[_0xa374('0x3c')](function(_0x191d0c){logger[_0xa374('0x5b')](_0xa374('0x70'),_0x191d0c);throw _0x191d0c;});}return _0x18d3ec;})['then'](respondWithResult(_0x54dacf,null))[_0xa374('0x3c')](handleError(_0x54dacf,null));});_0xb127b8[_0xa374('0x71')]();}catch(_0x3a065b){_0x54dacf[_0xa374('0x17')](0x1f4)['send'](_0x3a065b);}};exports[_0xa374('0x72')]=function(_0xa8f755,_0x292f59,_0x4f6b61){return db[_0xa374('0x29')][_0xa374('0x40')]({'where':{'id':_0xa8f755[_0xa374('0x3e')]['id']}})[_0xa374('0x20')](handleEntityNotFound(_0x292f59,null))[_0xa374('0x20')](saveUpdates(_[_0xa374('0x73')](_0xa8f755[_0xa374('0x69')],[_0xa374('0x25'),'description']),null))[_0xa374('0x20')](respondWithResult(_0x292f59,null))[_0xa374('0x3c')](handleError(_0x292f59,null));};exports[_0xa374('0x74')]=function(_0x24aaa3,_0x22bb5e,_0x4330e1){return db['Sound'][_0xa374('0x40')]({'where':{'id':_0x24aaa3[_0xa374('0x3e')]['id']}})[_0xa374('0x20')](function(_0x35c4c7){if(_0x35c4c7){return _0x35c4c7[_0xa374('0x24')]();}})[_0xa374('0x20')](function(_0x14669f){var _0x3a5403=path[_0xa374('0x45')](config['root'],_0xa374('0x54'),util[_0xa374('0x4b')](_0xa374('0x4c'),_0x14669f[_0xa374('0x42')],_0x14669f['original_format']));var _0x311ee1=path[_0xa374('0x45')](config[_0xa374('0x4a')],_0xa374('0x46'),util[_0xa374('0x4b')]('%s.%s',_0x14669f[_0xa374('0x42')],_0x14669f[_0xa374('0x43')]));fs[_0xa374('0x5c')](_0x3a5403);fs[_0xa374('0x5c')](_0x311ee1);return _0x14669f;})['then'](function(_0x353d45){if(_0x353d45){return _0x22bb5e[_0xa374('0x17')](0xcc)['end']();}})[_0xa374('0x20')](handleEntityNotFound(_0x22bb5e,null))[_0xa374('0x3c')](handleError(_0x22bb5e,null));};