Built motion from commit 14588770.|2.5.38
[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 _0xf98f=['ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','update','then','destroy','get','Sounds','UserProfileResource','end','error','stack','name','send','map','rawAttributes','fieldName','type','model','query','filters','intersection','fields','attributes','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','options','Sound','includeAll','include','findAll','catch','show','keys','length','find','download','params','save_name','converted_format','existsSync','join','root','server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','extname','filename','substring','wav','info','Sound\x20upload\x20destination:\x20%s','chmodSync','transcode','unlinkSync','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','bitRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','0777','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','autoAssociation','start','description','server/files/sounds/original','original_format','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','path','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse'];(function(_0x397e5f,_0x454c8f){var _0x5302b8=function(_0x1a29ef){while(--_0x1a29ef){_0x397e5f['push'](_0x397e5f['shift']());}};_0x5302b8(++_0x454c8f);}(_0xf98f,0x171));var _0xff98=function(_0xbf2555,_0x273b27){_0xbf2555=_0xbf2555-0x0;var _0xe00596=_0xf98f[_0xbf2555];return _0xe00596;};'use strict';var emlformat=require(_0xff98('0x0'));var rimraf=require(_0xff98('0x1'));var zipdir=require(_0xff98('0x2'));var jsonpatch=require(_0xff98('0x3'));var rp=require(_0xff98('0x4'));var moment=require(_0xff98('0x5'));var BPromise=require(_0xff98('0x6'));var Mustache=require(_0xff98('0x7'));var util=require('util');var path=require(_0xff98('0x8'));var sox=require('sox');var csv=require(_0xff98('0x9'));var ejs=require(_0xff98('0xa'));var fs=require('fs');var fs_extra=require(_0xff98('0xb'));var _=require('lodash');var squel=require(_0xff98('0xc'));var crypto=require(_0xff98('0xd'));var jsforce=require(_0xff98('0xe'));var deskjs=require(_0xff98('0xf'));var toCsv=require('to-csv');var querystring=require(_0xff98('0x10'));var Papa=require(_0xff98('0x11'));var Redis=require(_0xff98('0x12'));var authService=require(_0xff98('0x13'));var qs=require(_0xff98('0x14'));var as=require(_0xff98('0x15'));var hardwareService=require('../../config/license/hardware');var logger=require(_0xff98('0x16'))(_0xff98('0x17'));var utils=require(_0xff98('0x18'));var config=require(_0xff98('0x19'));var licenseUtil=require(_0xff98('0x1a'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x2f3b11,_0x1b6d76){_0x1b6d76=_0x1b6d76||0xcc;return function(_0x1768ba){if(_0x1768ba){return _0x2f3b11[_0xff98('0x1b')](_0x1b6d76);}return _0x2f3b11[_0xff98('0x1c')](_0x1b6d76)['end']();};}function respondWithResult(_0x40f16d,_0xd05480){_0xd05480=_0xd05480||0xc8;return function(_0x5ed24d){if(_0x5ed24d){return _0x40f16d[_0xff98('0x1c')](_0xd05480)[_0xff98('0x1d')](_0x5ed24d);}};}function respondWithFilteredResult(_0x3e2e68,_0x434eab){return function(_0x87b2cf){if(_0x87b2cf){var _0x569502=typeof _0x434eab[_0xff98('0x1e')]===_0xff98('0x1f')&&typeof _0x434eab[_0xff98('0x20')]===_0xff98('0x1f');var _0x17e874=_0x87b2cf[_0xff98('0x21')];var _0x5b8f9c=_0x569502?0x0:_0x434eab[_0xff98('0x1e')];var _0xdec99a=_0x569502?_0x87b2cf[_0xff98('0x21')]:_0x434eab[_0xff98('0x1e')]+_0x434eab['limit'];var _0x484cf1;if(_0xdec99a>=_0x17e874){_0xdec99a=_0x17e874;_0x484cf1=0xc8;}else{_0x484cf1=0xce;}_0x3e2e68[_0xff98('0x1c')](_0x484cf1);return _0x3e2e68[_0xff98('0x22')](_0xff98('0x23'),_0x5b8f9c+'-'+_0xdec99a+'/'+_0x17e874)[_0xff98('0x1d')](_0x87b2cf);}return null;};}function patchUpdates(_0x3b96a7){return function(_0x4b3566){try{jsonpatch[_0xff98('0x24')](_0x4b3566,_0x3b96a7,!![]);}catch(_0x1e383e){return BPromise[_0xff98('0x25')](_0x1e383e);}return _0x4b3566['save']();};}function saveUpdates(_0x4aa434,_0x30bb06){return function(_0x8ba91c){if(_0x8ba91c){return _0x8ba91c[_0xff98('0x26')](_0x4aa434)[_0xff98('0x27')](function(_0x569439){return _0x569439;});}return null;};}function removeEntity(_0x320fa5,_0x37b96f){return function(_0x4482ea){if(_0x4482ea){return _0x4482ea[_0xff98('0x28')]()[_0xff98('0x27')](function(){var _0xd1760f=_0x4482ea[_0xff98('0x29')]({'plain':!![]});var _0x179242=_0xff98('0x2a');return db[_0xff98('0x2b')][_0xff98('0x28')]({'where':{'type':_0x179242,'resourceId':_0xd1760f['id']}})[_0xff98('0x27')](function(){return _0x4482ea;});})[_0xff98('0x27')](function(){_0x320fa5[_0xff98('0x1c')](0xcc)[_0xff98('0x2c')]();});}};}function handleEntityNotFound(_0x678529,_0x36433c){return function(_0x409d4a){if(!_0x409d4a){_0x678529[_0xff98('0x1b')](0x194);}return _0x409d4a;};}function handleError(_0x4b27b7,_0xef4955){_0xef4955=_0xef4955||0x1f4;return function(_0x55ed24){logger[_0xff98('0x2d')](_0x55ed24[_0xff98('0x2e')]);if(_0x55ed24[_0xff98('0x2f')]){delete _0x55ed24[_0xff98('0x2f')];}_0x4b27b7['status'](_0xef4955)[_0xff98('0x30')](_0x55ed24);};}exports['index']=function(_0x3763eb,_0x3408fa){var _0x24b681={},_0x41679a={},_0x1f30d={'count':0x0,'rows':[]};var _0x2ce8a7=_[_0xff98('0x31')](db['Sound'][_0xff98('0x32')],function(_0x3e6b9d){return{'name':_0x3e6b9d[_0xff98('0x33')],'type':_0x3e6b9d[_0xff98('0x34')]['key']};});_0x41679a[_0xff98('0x35')]=_[_0xff98('0x31')](_0x2ce8a7,_0xff98('0x2f'));_0x41679a[_0xff98('0x36')]=_['keys'](_0x3763eb['query']);_0x41679a[_0xff98('0x37')]=_['intersection'](_0x41679a[_0xff98('0x35')],_0x41679a[_0xff98('0x36')]);_0x24b681['attributes']=_[_0xff98('0x38')](_0x41679a[_0xff98('0x35')],qs['fields'](_0x3763eb['query'][_0xff98('0x39')]));_0x24b681[_0xff98('0x3a')]=_0x24b681[_0xff98('0x3a')]['length']?_0x24b681[_0xff98('0x3a')]:_0x41679a[_0xff98('0x35')];if(!_0x3763eb['query'][_0xff98('0x3b')](_0xff98('0x3c'))){_0x24b681[_0xff98('0x20')]=qs['limit'](_0x3763eb['query'][_0xff98('0x20')]);_0x24b681[_0xff98('0x1e')]=qs[_0xff98('0x1e')](_0x3763eb[_0xff98('0x36')][_0xff98('0x1e')]);}_0x24b681[_0xff98('0x3d')]=qs[_0xff98('0x3e')](_0x3763eb[_0xff98('0x36')][_0xff98('0x3e')]);_0x24b681[_0xff98('0x3f')]=qs[_0xff98('0x37')](_[_0xff98('0x40')](_0x3763eb[_0xff98('0x36')],_0x41679a[_0xff98('0x37')]),_0x2ce8a7);if(_0x3763eb[_0xff98('0x36')][_0xff98('0x41')]){_0x24b681[_0xff98('0x3f')]=_[_0xff98('0x42')](_0x24b681['where'],{'$or':_[_0xff98('0x31')](_0x2ce8a7,function(_0x43faf0){if(_0x43faf0['type']!=='VIRTUAL'){var _0x430b5c={};_0x430b5c[_0x43faf0[_0xff98('0x2f')]]={'$like':'%'+_0x3763eb[_0xff98('0x36')][_0xff98('0x41')]+'%'};return _0x430b5c;}})});}_0x24b681=_[_0xff98('0x42')]({},_0x24b681,_0x3763eb[_0xff98('0x43')]);var _0x29af20={'where':_0x24b681[_0xff98('0x3f')]};return db[_0xff98('0x44')][_0xff98('0x21')](_0x29af20)[_0xff98('0x27')](function(_0x37723b){_0x1f30d[_0xff98('0x21')]=_0x37723b;if(_0x3763eb[_0xff98('0x36')][_0xff98('0x45')]){_0x24b681[_0xff98('0x46')]=[{'all':!![]}];}return db[_0xff98('0x44')][_0xff98('0x47')](_0x24b681);})[_0xff98('0x27')](function(_0x2888d9){_0x1f30d['rows']=_0x2888d9;return _0x1f30d;})['then'](respondWithFilteredResult(_0x3408fa,_0x24b681))[_0xff98('0x48')](handleError(_0x3408fa,null));};exports[_0xff98('0x49')]=function(_0x3258b2,_0x11f7aa){var _0x162786={'raw':!![],'where':{'id':_0x3258b2['params']['id']}},_0x4e4542={};_0x4e4542[_0xff98('0x35')]=_[_0xff98('0x4a')](db['Sound'][_0xff98('0x32')]);_0x4e4542['query']=_[_0xff98('0x4a')](_0x3258b2[_0xff98('0x36')]);_0x4e4542[_0xff98('0x37')]=_[_0xff98('0x38')](_0x4e4542[_0xff98('0x35')],_0x4e4542[_0xff98('0x36')]);_0x162786['attributes']=_[_0xff98('0x38')](_0x4e4542[_0xff98('0x35')],qs['fields'](_0x3258b2[_0xff98('0x36')][_0xff98('0x39')]));_0x162786[_0xff98('0x3a')]=_0x162786[_0xff98('0x3a')][_0xff98('0x4b')]?_0x162786[_0xff98('0x3a')]:_0x4e4542[_0xff98('0x35')];if(_0x3258b2['query'][_0xff98('0x45')]){_0x162786[_0xff98('0x46')]=[{'all':!![]}];}_0x162786=_[_0xff98('0x42')]({},_0x162786,_0x3258b2['options']);return db[_0xff98('0x44')][_0xff98('0x4c')](_0x162786)['then'](handleEntityNotFound(_0x11f7aa,null))[_0xff98('0x27')](respondWithResult(_0x11f7aa,null))['catch'](handleError(_0x11f7aa,null));};exports[_0xff98('0x4d')]=function(_0x459502,_0x5c76ce,_0x288a23){return db[_0xff98('0x44')][_0xff98('0x4c')]({'where':{'id':_0x459502[_0xff98('0x4e')]['id']},'attributes':[_0xff98('0x2f'),_0xff98('0x4f'),_0xff98('0x50')],'raw':!![]})[_0xff98('0x27')](handleEntityNotFound(_0x5c76ce,null))[_0xff98('0x27')](function(_0x109c1a){if(_0x109c1a){if(!fs[_0xff98('0x51')](path[_0xff98('0x52')](config[_0xff98('0x53')],_0xff98('0x54'),_0x109c1a[_0xff98('0x4f')]+'.'+_0x109c1a['converted_format']))){throw new db[(_0xff98('0x55'))][(_0xff98('0x56'))](_0xff98('0x57'));}return _0x5c76ce[_0xff98('0x4d')](path['join'](config[_0xff98('0x53')],_0xff98('0x54'),util[_0xff98('0x58')](_0xff98('0x59'),_0x109c1a[_0xff98('0x4f')],_0x109c1a['converted_format'])),util['format'](_0xff98('0x59'),_0x109c1a[_0xff98('0x2f')],_0x109c1a[_0xff98('0x50')]));}return null;})[_0xff98('0x48')](handleError(_0x5c76ce,null));};exports[_0xff98('0x5a')]=function(_0x348219,_0x3a62bb,_0x1c5b20){var _0x1805d5={'name':path[_0xff98('0x5b')](_0x348219[_0xff98('0x5c')][_0xff98('0x5d')],path[_0xff98('0x5e')](_0x348219['file']['originalname'])),'save_name':path['basename'](_0x348219['file'][_0xff98('0x5f')],path['extname'](_0x348219[_0xff98('0x5c')][_0xff98('0x5f')])),'original_format':path[_0xff98('0x5e')](_0x348219[_0xff98('0x5c')][_0xff98('0x5d')])[_0xff98('0x60')](0x1),'converted_format':_0xff98('0x61')};var _0x5888a1=path['join'](config[_0xff98('0x53')],'server/files/sounds/original',_0x348219['file']['filename']);var _0x10462b=path[_0xff98('0x52')](config[_0xff98('0x53')],'server/files/sounds/converted',util[_0xff98('0x58')](_0xff98('0x59'),path['basename'](_0x348219['file'][_0xff98('0x5f')],path[_0xff98('0x5e')](_0x348219[_0xff98('0x5c')][_0xff98('0x5f')])),_0x1805d5[_0xff98('0x50')]));logger[_0xff98('0x62')]('Sound\x20upload\x20filepath:\x20%s',_0x5888a1);logger[_0xff98('0x62')](_0xff98('0x63'),_0x10462b);try{fs[_0xff98('0x64')](_0x5888a1,parseInt('0777',0x8));var _0x2c8463=sox[_0xff98('0x65')](_0x5888a1,_0x10462b,{'sampleRate':0x1f40,'format':_0xff98('0x61'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x2c8463['on'](_0xff98('0x2d'),function(_0x1b2993){fs[_0xff98('0x66')](_0x5888a1);_0x3a62bb[_0xff98('0x1c')](0x1f4)[_0xff98('0x30')](_0x1b2993);});_0x2c8463['on']('src',function(_0x102c7){_0x1805d5[_0xff98('0x67')]=isNaN(_0x102c7[_0xff98('0x68')])?null:_0x102c7['duration']*0x3e8;_0x1805d5[_0xff98('0x69')]=isNaN(_0x102c7[_0xff98('0x6a')])?null:_0x102c7['sampleCount'];_0x1805d5[_0xff98('0x6b')]=isNaN(_0x102c7[_0xff98('0x6c')])?null:_0x102c7[_0xff98('0x6c')];_0x1805d5['original_bitRate']=isNaN(_0x102c7['bitRate'])?null:_0x102c7[_0xff98('0x6d')];_0x1805d5['original_sampleRate']=isNaN(_0x102c7['sampleRate'])?null:_0x102c7[_0xff98('0x6e')];});_0x2c8463['on'](_0xff98('0x6f'),function(_0x3dd8da){_0x1805d5[_0xff98('0x70')]=isNaN(_0x3dd8da['duration'])?null:_0x3dd8da[_0xff98('0x68')]*0x3e8;_0x1805d5[_0xff98('0x71')]=isNaN(_0x3dd8da[_0xff98('0x6a')])?null:_0x3dd8da[_0xff98('0x6a')];_0x1805d5[_0xff98('0x72')]=isNaN(_0x3dd8da[_0xff98('0x6c')])?null:_0x3dd8da['channelCount'];_0x1805d5[_0xff98('0x73')]=isNaN(_0x3dd8da[_0xff98('0x6d')])?null:_0x3dd8da[_0xff98('0x6d')];_0x1805d5[_0xff98('0x74')]=isNaN(_0x3dd8da[_0xff98('0x6e')])?null:_0x3dd8da['sampleRate'];});_0x2c8463['on'](_0xff98('0x2c'),function(){fs[_0xff98('0x64')](_0x10462b,parseInt(_0xff98('0x75'),0x8));return db[_0xff98('0x44')][_0xff98('0x5a')](_0x1805d5)[_0xff98('0x27')](function(_0x56ba06){var _0x475af1={'role':_0x348219[_0xff98('0x76')][_0xff98('0x77')],'userProfileId':_0x348219[_0xff98('0x76')][_0xff98('0x78')]};if(!_0x475af1)throw new Error(_0xff98('0x79'));if(_0x475af1[_0xff98('0x77')]===_0xff98('0x7a')){var _0x411e0c=_0x56ba06[_0xff98('0x29')]({'plain':!![]});return db[_0xff98('0x7b')][_0xff98('0x4c')]({'where':{'name':_0xff98('0x2a'),'userProfileId':_0x475af1[_0xff98('0x78')]},'raw':!![]})['then'](function(_0x45df75){if(_0x45df75&&_0x45df75[_0xff98('0x7c')]===0x0){return db[_0xff98('0x2b')][_0xff98('0x5a')]({'name':_0x411e0c['name'],'resourceId':_0x411e0c['id'],'type':_0x45df75['name'],'sectionId':_0x45df75['id']},{})[_0xff98('0x27')](function(){return _0x56ba06;});}else{return _0x56ba06;}})[_0xff98('0x48')](function(_0x4cbbfd){logger[_0xff98('0x2d')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x4cbbfd);throw _0x4cbbfd;});}return _0x56ba06;})[_0xff98('0x27')](respondWithResult(_0x3a62bb,null))[_0xff98('0x48')](handleError(_0x3a62bb,null));});_0x2c8463[_0xff98('0x7d')]();}catch(_0xdba300){_0x3a62bb[_0xff98('0x1c')](0x1f4)[_0xff98('0x30')](_0xdba300);}};exports[_0xff98('0x26')]=function(_0x37484b,_0x295cbe,_0x259e44){return db[_0xff98('0x44')]['find']({'where':{'id':_0x37484b['params']['id']}})[_0xff98('0x27')](handleEntityNotFound(_0x295cbe,null))[_0xff98('0x27')](saveUpdates(_['pick'](_0x37484b[_0xff98('0x76')],[_0xff98('0x2f'),_0xff98('0x7e')]),null))[_0xff98('0x27')](respondWithResult(_0x295cbe,null))[_0xff98('0x48')](handleError(_0x295cbe,null));};exports['delete']=function(_0x5d7e5f,_0x3c74b2,_0x299f9a){return db['Sound'][_0xff98('0x4c')]({'where':{'id':_0x5d7e5f[_0xff98('0x4e')]['id']}})[_0xff98('0x27')](function(_0x2cee9f){if(_0x2cee9f){return _0x2cee9f[_0xff98('0x28')]();}})['then'](function(_0x48d6bb){var _0x2da039=path[_0xff98('0x52')](config['root'],_0xff98('0x7f'),util[_0xff98('0x58')]('%s.%s',_0x48d6bb['save_name'],_0x48d6bb[_0xff98('0x80')]));var _0x46e706=path[_0xff98('0x52')](config[_0xff98('0x53')],_0xff98('0x54'),util[_0xff98('0x58')]('%s.%s',_0x48d6bb[_0xff98('0x4f')],_0x48d6bb['converted_format']));fs[_0xff98('0x66')](_0x2da039);fs[_0xff98('0x66')](_0x46e706);return _0x48d6bb;})[_0xff98('0x27')](function(_0x11cca2){if(_0x11cca2){return _0x3c74b2[_0xff98('0x1c')](0xcc)[_0xff98('0x2c')]();}})[_0xff98('0x27')](handleEntityNotFound(_0x3c74b2,null))[_0xff98('0x48')](handleError(_0x3c74b2,null));};