cbae50af9960424c2fcb066209576a5be6f7a38e
[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 _0x5346=['destroy','then','get','Sounds','UserProfileResource','error','stack','name','index','map','rawAttributes','fieldName','type','key','keys','query','model','attributes','intersection','fields','hasOwnProperty','nolimit','order','sort','where','filters','pick','filter','VIRTUAL','merge','include','findAll','rows','catch','show','params','Sound','length','options','download','find','save_name','existsSync','join','root','server/files/sounds/converted','converted_format','Sequelize','ValidationError','format','%s.%s','basename','file','originalname','extname','filename','substring','server/files/sounds/original','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','wav','unlinkSync','send','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_sampleRate','sampleRate','dest','converted_sampleCount','converted_bitRate','bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','update','description','delete','original_format','fast-json-patch','moment','bluebird','mustache','path','sox','to-csv','fs-extra','lodash','squel','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','sendStatus','status','end','json','offset','undefined','count','limit','set','Content-Range','reject','save'];(function(_0x4e635d,_0x311617){var _0x8933d5=function(_0x47ef28){while(--_0x47ef28){_0x4e635d['push'](_0x4e635d['shift']());}};_0x8933d5(++_0x311617);}(_0x5346,0xd4));var _0x6534=function(_0x3c0179,_0x3d6a81){_0x3c0179=_0x3c0179-0x0;var _0x3e070b=_0x5346[_0x3c0179];return _0x3e070b;};'use strict';var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x6534('0x0'));var rp=require('request-promise');var moment=require(_0x6534('0x1'));var BPromise=require(_0x6534('0x2'));var Mustache=require(_0x6534('0x3'));var util=require('util');var path=require(_0x6534('0x4'));var sox=require(_0x6534('0x5'));var csv=require(_0x6534('0x6'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x6534('0x7'));var _=require(_0x6534('0x8'));var squel=require(_0x6534('0x9'));var crypto=require(_0x6534('0xa'));var jsforce=require(_0x6534('0xb'));var deskjs=require('desk.js');var toCsv=require(_0x6534('0x6'));var querystring=require(_0x6534('0xc'));var Papa=require(_0x6534('0xd'));var Redis=require(_0x6534('0xe'));var authService=require(_0x6534('0xf'));var qs=require('../../components/parsers/qs');var as=require(_0x6534('0x10'));var hardwareService=require(_0x6534('0x11'));var logger=require(_0x6534('0x12'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x6534('0x13'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x31e4d4,_0x3cbbe8){_0x3cbbe8=_0x3cbbe8||0xcc;return function(_0x482f3e){if(_0x482f3e){return _0x31e4d4[_0x6534('0x14')](_0x3cbbe8);}return _0x31e4d4[_0x6534('0x15')](_0x3cbbe8)[_0x6534('0x16')]();};}function respondWithResult(_0x50d091,_0xd39b48){_0xd39b48=_0xd39b48||0xc8;return function(_0x2f3053){if(_0x2f3053){return _0x50d091[_0x6534('0x15')](_0xd39b48)[_0x6534('0x17')](_0x2f3053);}};}function respondWithFilteredResult(_0x4ad47b,_0x5152db){return function(_0x404420){if(_0x404420){var _0x59ce1e=typeof _0x5152db[_0x6534('0x18')]===_0x6534('0x19')&&typeof _0x5152db['limit']===_0x6534('0x19');var _0x48a6ad=_0x404420['count'];var _0x402f84=_0x59ce1e?0x0:_0x5152db['offset'];var _0x564c0e=_0x59ce1e?_0x404420[_0x6534('0x1a')]:_0x5152db[_0x6534('0x18')]+_0x5152db[_0x6534('0x1b')];var _0x457ccf;if(_0x564c0e>=_0x48a6ad){_0x564c0e=_0x48a6ad;_0x457ccf=0xc8;}else{_0x457ccf=0xce;}_0x4ad47b[_0x6534('0x15')](_0x457ccf);return _0x4ad47b[_0x6534('0x1c')](_0x6534('0x1d'),_0x402f84+'-'+_0x564c0e+'/'+_0x48a6ad)[_0x6534('0x17')](_0x404420);}return null;};}function patchUpdates(_0x25f6d2){return function(_0x1552c8){try{jsonpatch['apply'](_0x1552c8,_0x25f6d2,!![]);}catch(_0x3b0341){return BPromise[_0x6534('0x1e')](_0x3b0341);}return _0x1552c8[_0x6534('0x1f')]();};}function saveUpdates(_0x24ba37,_0x354b2b){return function(_0x29d5c3){if(_0x29d5c3){return _0x29d5c3['update'](_0x24ba37)['then'](function(_0x475dc2){return _0x475dc2;});}return null;};}function removeEntity(_0x5bbf64,_0x149111){return function(_0x389fc3){if(_0x389fc3){return _0x389fc3[_0x6534('0x20')]()[_0x6534('0x21')](function(){var _0x31bbcc=_0x389fc3[_0x6534('0x22')]({'plain':!![]});var _0x24ef7b=_0x6534('0x23');return db[_0x6534('0x24')][_0x6534('0x20')]({'where':{'type':_0x24ef7b,'resourceId':_0x31bbcc['id']}})[_0x6534('0x21')](function(){return _0x389fc3;});})[_0x6534('0x21')](function(){_0x5bbf64[_0x6534('0x15')](0xcc)[_0x6534('0x16')]();});}};}function handleEntityNotFound(_0x4a8c9a,_0x3baa92){return function(_0x5ba0dd){if(!_0x5ba0dd){_0x4a8c9a[_0x6534('0x14')](0x194);}return _0x5ba0dd;};}function handleError(_0x522a01,_0x41fea9){_0x41fea9=_0x41fea9||0x1f4;return function(_0x5bcd9e){logger[_0x6534('0x25')](_0x5bcd9e[_0x6534('0x26')]);if(_0x5bcd9e[_0x6534('0x27')]){delete _0x5bcd9e[_0x6534('0x27')];}_0x522a01[_0x6534('0x15')](_0x41fea9)['send'](_0x5bcd9e);};}exports[_0x6534('0x28')]=function(_0x10748a,_0x5606b0){var _0xee1bf2={},_0x11c103={},_0x28b5c5={'count':0x0,'rows':[]};var _0x5c0485=_[_0x6534('0x29')](db['Sound'][_0x6534('0x2a')],function(_0x4535d7){return{'name':_0x4535d7[_0x6534('0x2b')],'type':_0x4535d7[_0x6534('0x2c')][_0x6534('0x2d')]};});_0x11c103['model']=_[_0x6534('0x29')](_0x5c0485,_0x6534('0x27'));_0x11c103['query']=_[_0x6534('0x2e')](_0x10748a[_0x6534('0x2f')]);_0x11c103['filters']=_['intersection'](_0x11c103[_0x6534('0x30')],_0x11c103[_0x6534('0x2f')]);_0xee1bf2[_0x6534('0x31')]=_[_0x6534('0x32')](_0x11c103[_0x6534('0x30')],qs['fields'](_0x10748a[_0x6534('0x2f')][_0x6534('0x33')]));_0xee1bf2['attributes']=_0xee1bf2[_0x6534('0x31')]['length']?_0xee1bf2[_0x6534('0x31')]:_0x11c103['model'];if(!_0x10748a[_0x6534('0x2f')][_0x6534('0x34')](_0x6534('0x35'))){_0xee1bf2['limit']=qs[_0x6534('0x1b')](_0x10748a[_0x6534('0x2f')]['limit']);_0xee1bf2['offset']=qs[_0x6534('0x18')](_0x10748a[_0x6534('0x2f')][_0x6534('0x18')]);}_0xee1bf2[_0x6534('0x36')]=qs[_0x6534('0x37')](_0x10748a[_0x6534('0x2f')][_0x6534('0x37')]);_0xee1bf2[_0x6534('0x38')]=qs[_0x6534('0x39')](_[_0x6534('0x3a')](_0x10748a[_0x6534('0x2f')],_0x11c103['filters']),_0x5c0485);if(_0x10748a['query'][_0x6534('0x3b')]){_0xee1bf2[_0x6534('0x38')]=_['merge'](_0xee1bf2['where'],{'$or':_[_0x6534('0x29')](_0x5c0485,function(_0x5563c3){if(_0x5563c3['type']!==_0x6534('0x3c')){var _0x44c6bc={};_0x44c6bc[_0x5563c3['name']]={'$like':'%'+_0x10748a[_0x6534('0x2f')][_0x6534('0x3b')]+'%'};return _0x44c6bc;}})});}_0xee1bf2=_[_0x6534('0x3d')]({},_0xee1bf2,_0x10748a['options']);var _0x2888b6={'where':_0xee1bf2[_0x6534('0x38')]};return db['Sound']['count'](_0x2888b6)['then'](function(_0x58f849){_0x28b5c5[_0x6534('0x1a')]=_0x58f849;if(_0x10748a[_0x6534('0x2f')]['includeAll']){_0xee1bf2[_0x6534('0x3e')]=[{'all':!![]}];}return db['Sound'][_0x6534('0x3f')](_0xee1bf2);})[_0x6534('0x21')](function(_0x418395){_0x28b5c5[_0x6534('0x40')]=_0x418395;return _0x28b5c5;})[_0x6534('0x21')](respondWithFilteredResult(_0x5606b0,_0xee1bf2))[_0x6534('0x41')](handleError(_0x5606b0,null));};exports[_0x6534('0x42')]=function(_0x36e0d9,_0x746839){var _0x4f8500={'raw':!![],'where':{'id':_0x36e0d9[_0x6534('0x43')]['id']}},_0x5a5937={};_0x5a5937[_0x6534('0x30')]=_[_0x6534('0x2e')](db[_0x6534('0x44')][_0x6534('0x2a')]);_0x5a5937[_0x6534('0x2f')]=_[_0x6534('0x2e')](_0x36e0d9[_0x6534('0x2f')]);_0x5a5937['filters']=_['intersection'](_0x5a5937[_0x6534('0x30')],_0x5a5937[_0x6534('0x2f')]);_0x4f8500[_0x6534('0x31')]=_[_0x6534('0x32')](_0x5a5937[_0x6534('0x30')],qs[_0x6534('0x33')](_0x36e0d9['query'][_0x6534('0x33')]));_0x4f8500[_0x6534('0x31')]=_0x4f8500['attributes'][_0x6534('0x45')]?_0x4f8500[_0x6534('0x31')]:_0x5a5937['model'];if(_0x36e0d9[_0x6534('0x2f')]['includeAll']){_0x4f8500['include']=[{'all':!![]}];}_0x4f8500=_[_0x6534('0x3d')]({},_0x4f8500,_0x36e0d9[_0x6534('0x46')]);return db[_0x6534('0x44')]['find'](_0x4f8500)['then'](handleEntityNotFound(_0x746839,null))[_0x6534('0x21')](respondWithResult(_0x746839,null))[_0x6534('0x41')](handleError(_0x746839,null));};exports[_0x6534('0x47')]=function(_0x54c781,_0x3e28ed,_0x5819ac){return db['Sound'][_0x6534('0x48')]({'where':{'id':_0x54c781[_0x6534('0x43')]['id']},'attributes':[_0x6534('0x27'),_0x6534('0x49'),'converted_format'],'raw':!![]})[_0x6534('0x21')](handleEntityNotFound(_0x3e28ed,null))[_0x6534('0x21')](function(_0x319f){if(_0x319f){if(!fs[_0x6534('0x4a')](path[_0x6534('0x4b')](config[_0x6534('0x4c')],_0x6534('0x4d'),_0x319f[_0x6534('0x49')]+'.'+_0x319f[_0x6534('0x4e')]))){throw new db[(_0x6534('0x4f'))][(_0x6534('0x50'))]('Sound\x20not\x20found');}return _0x3e28ed[_0x6534('0x47')](path[_0x6534('0x4b')](config[_0x6534('0x4c')],_0x6534('0x4d'),util[_0x6534('0x51')](_0x6534('0x52'),_0x319f['save_name'],_0x319f[_0x6534('0x4e')])),util[_0x6534('0x51')]('%s.%s',_0x319f[_0x6534('0x27')],_0x319f[_0x6534('0x4e')]));}return null;})['catch'](handleError(_0x3e28ed,null));};exports['create']=function(_0x1bfc47,_0x3d86e2,_0x218b7c){var _0x4176af={'name':path[_0x6534('0x53')](_0x1bfc47[_0x6534('0x54')][_0x6534('0x55')],path['extname'](_0x1bfc47[_0x6534('0x54')][_0x6534('0x55')])),'save_name':path[_0x6534('0x53')](_0x1bfc47['file']['filename'],path[_0x6534('0x56')](_0x1bfc47[_0x6534('0x54')][_0x6534('0x57')])),'original_format':path[_0x6534('0x56')](_0x1bfc47[_0x6534('0x54')][_0x6534('0x55')])[_0x6534('0x58')](0x1),'converted_format':'wav'};var _0x33b0b5=path['join'](config[_0x6534('0x4c')],_0x6534('0x59'),_0x1bfc47[_0x6534('0x54')][_0x6534('0x57')]);var _0x54a006=path[_0x6534('0x4b')](config[_0x6534('0x4c')],_0x6534('0x4d'),util[_0x6534('0x51')]('%s.%s',path[_0x6534('0x53')](_0x1bfc47[_0x6534('0x54')][_0x6534('0x57')],path['extname'](_0x1bfc47[_0x6534('0x54')]['filename'])),_0x4176af[_0x6534('0x4e')]));logger['info'](_0x6534('0x5a'),_0x33b0b5);logger['info'](_0x6534('0x5b'),_0x54a006);try{fs[_0x6534('0x5c')](_0x33b0b5,parseInt(_0x6534('0x5d'),0x8));var _0x5a5e1a=sox['transcode'](_0x33b0b5,_0x54a006,{'sampleRate':0x1f40,'format':_0x6534('0x5e'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x5a5e1a['on'](_0x6534('0x25'),function(_0x5df2d2){fs[_0x6534('0x5f')](_0x33b0b5);_0x3d86e2['status'](0x1f4)[_0x6534('0x60')](_0x5df2d2);});_0x5a5e1a['on'](_0x6534('0x61'),function(_0x178a83){_0x4176af['original_duration']=isNaN(_0x178a83['duration'])?null:_0x178a83[_0x6534('0x62')]*0x3e8;_0x4176af[_0x6534('0x63')]=isNaN(_0x178a83[_0x6534('0x64')])?null:_0x178a83['sampleCount'];_0x4176af[_0x6534('0x65')]=isNaN(_0x178a83[_0x6534('0x66')])?null:_0x178a83[_0x6534('0x66')];_0x4176af['original_bitRate']=isNaN(_0x178a83['bitRate'])?null:_0x178a83['bitRate'];_0x4176af[_0x6534('0x67')]=isNaN(_0x178a83[_0x6534('0x68')])?null:_0x178a83['sampleRate'];});_0x5a5e1a['on'](_0x6534('0x69'),function(_0x916fe5){_0x4176af['converted_duration']=isNaN(_0x916fe5[_0x6534('0x62')])?null:_0x916fe5[_0x6534('0x62')]*0x3e8;_0x4176af[_0x6534('0x6a')]=isNaN(_0x916fe5[_0x6534('0x64')])?null:_0x916fe5[_0x6534('0x64')];_0x4176af['converted_channelCount']=isNaN(_0x916fe5[_0x6534('0x66')])?null:_0x916fe5['channelCount'];_0x4176af[_0x6534('0x6b')]=isNaN(_0x916fe5[_0x6534('0x6c')])?null:_0x916fe5[_0x6534('0x6c')];_0x4176af[_0x6534('0x6d')]=isNaN(_0x916fe5[_0x6534('0x68')])?null:_0x916fe5[_0x6534('0x68')];});_0x5a5e1a['on']('end',function(){fs[_0x6534('0x5c')](_0x54a006,parseInt(_0x6534('0x5d'),0x8));return db['Sound']['create'](_0x4176af)[_0x6534('0x21')](function(_0x8748a){var _0x479dfe={'role':_0x1bfc47[_0x6534('0x6e')][_0x6534('0x6f')],'userProfileId':_0x1bfc47[_0x6534('0x6e')][_0x6534('0x70')]};if(!_0x479dfe)throw new Error(_0x6534('0x71'));if(_0x479dfe[_0x6534('0x6f')]===_0x6534('0x72')){var _0x96e7c8=_0x8748a[_0x6534('0x22')]({'plain':!![]});return db['UserProfileSection'][_0x6534('0x48')]({'where':{'name':_0x6534('0x23'),'userProfileId':_0x479dfe[_0x6534('0x70')]},'raw':!![]})[_0x6534('0x21')](function(_0x3e96ee){if(_0x3e96ee&&_0x3e96ee[_0x6534('0x73')]===0x0){return db['UserProfileResource']['create']({'name':_0x96e7c8['name'],'resourceId':_0x96e7c8['id'],'type':_0x3e96ee[_0x6534('0x27')],'sectionId':_0x3e96ee['id']},{})['then'](function(){return _0x8748a;});}else{return _0x8748a;}})[_0x6534('0x41')](function(_0x3cc631){logger['error'](_0x6534('0x74'),_0x3cc631);throw _0x3cc631;});}return _0x8748a;})['then'](respondWithResult(_0x3d86e2,null))[_0x6534('0x41')](handleError(_0x3d86e2,null));});_0x5a5e1a[_0x6534('0x75')]();}catch(_0x331c85){_0x3d86e2[_0x6534('0x15')](0x1f4)[_0x6534('0x60')](_0x331c85);}};exports[_0x6534('0x76')]=function(_0x37d562,_0x1ac828,_0x45bb83){return db[_0x6534('0x44')][_0x6534('0x48')]({'where':{'id':_0x37d562[_0x6534('0x43')]['id']}})['then'](handleEntityNotFound(_0x1ac828,null))[_0x6534('0x21')](saveUpdates(_['pick'](_0x37d562[_0x6534('0x6e')],[_0x6534('0x27'),_0x6534('0x77')]),null))['then'](respondWithResult(_0x1ac828,null))[_0x6534('0x41')](handleError(_0x1ac828,null));};exports[_0x6534('0x78')]=function(_0x206f7,_0x5d3c10,_0x57c6b2){return db['Sound']['find']({'where':{'id':_0x206f7[_0x6534('0x43')]['id']}})[_0x6534('0x21')](function(_0x102b94){if(_0x102b94){return _0x102b94[_0x6534('0x20')]();}})[_0x6534('0x21')](function(_0x1c3ab9){var _0x59b4c0=path[_0x6534('0x4b')](config[_0x6534('0x4c')],_0x6534('0x59'),util[_0x6534('0x51')](_0x6534('0x52'),_0x1c3ab9['save_name'],_0x1c3ab9[_0x6534('0x79')]));var _0x5733d3=path[_0x6534('0x4b')](config[_0x6534('0x4c')],_0x6534('0x4d'),util[_0x6534('0x51')](_0x6534('0x52'),_0x1c3ab9[_0x6534('0x49')],_0x1c3ab9[_0x6534('0x4e')]));fs['unlinkSync'](_0x59b4c0);fs['unlinkSync'](_0x5733d3);return _0x1c3ab9;})['then'](function(_0x1f0392){if(_0x1f0392){return _0x5d3c10[_0x6534('0x15')](0xcc)[_0x6534('0x16')]();}})[_0x6534('0x21')](handleEntityNotFound(_0x5d3c10,null))[_0x6534('0x41')](handleError(_0x5d3c10,null));};