Built motion from commit 85328c08.|2.5.50
[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 _0x2ff8=['role','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','get','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','description','delete','original_format','eml-format','zip-dir','fast-json-patch','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','squel','crypto','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../mysqldb','sendStatus','status','end','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','Sounds','UserProfileResource','destroy','error','stack','name','send','index','map','Sound','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','include','findAll','catch','params','length','options','find','download','converted_format','existsSync','join','root','ValidationError','server/files/sounds/converted','format','%s.%s','save_name','basename','file','originalname','extname','filename','substring','server/files/sounds/original','info','Sound\x20upload\x20destination:\x20%s','wav','unlinkSync','src','original_duration','duration','original_sampleCount','sampleCount','channelCount','bitRate','original_sampleRate','sampleRate','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','chmodSync','create','body'];(function(_0x5b6d34,_0x2a32a0){var _0x65eb46=function(_0x2fed53){while(--_0x2fed53){_0x5b6d34['push'](_0x5b6d34['shift']());}};_0x65eb46(++_0x2a32a0);}(_0x2ff8,0x88));var _0x82ff=function(_0x1cfa0d,_0x4391f2){_0x1cfa0d=_0x1cfa0d-0x0;var _0xe33279=_0x2ff8[_0x1cfa0d];return _0xe33279;};'use strict';var emlformat=require(_0x82ff('0x0'));var rimraf=require('rimraf');var zipdir=require(_0x82ff('0x1'));var jsonpatch=require(_0x82ff('0x2'));var rp=require('request-promise');var moment=require(_0x82ff('0x3'));var BPromise=require(_0x82ff('0x4'));var Mustache=require(_0x82ff('0x5'));var util=require(_0x82ff('0x6'));var path=require(_0x82ff('0x7'));var sox=require(_0x82ff('0x8'));var csv=require(_0x82ff('0x9'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x82ff('0xa'));var _=require(_0x82ff('0xb'));var squel=require(_0x82ff('0xc'));var crypto=require(_0x82ff('0xd'));var jsforce=require('jsforce');var deskjs=require('desk.js');var toCsv=require(_0x82ff('0x9'));var querystring=require(_0x82ff('0xe'));var Papa=require(_0x82ff('0xf'));var Redis=require(_0x82ff('0x10'));var authService=require(_0x82ff('0x11'));var qs=require(_0x82ff('0x12'));var as=require(_0x82ff('0x13'));var hardwareService=require(_0x82ff('0x14'));var logger=require(_0x82ff('0x15'))('api');var utils=require(_0x82ff('0x16'));var config=require('../../config/environment');var licenseUtil=require('../../config/license/util');var db=require(_0x82ff('0x17'))['db'];function respondWithStatusCode(_0x5d5966,_0x37f1dc){_0x37f1dc=_0x37f1dc||0xcc;return function(_0x93c1c6){if(_0x93c1c6){return _0x5d5966[_0x82ff('0x18')](_0x37f1dc);}return _0x5d5966[_0x82ff('0x19')](_0x37f1dc)[_0x82ff('0x1a')]();};}function respondWithResult(_0x46fb1a,_0x37f9cf){_0x37f9cf=_0x37f9cf||0xc8;return function(_0x2c1c4e){if(_0x2c1c4e){return _0x46fb1a['status'](_0x37f9cf)[_0x82ff('0x1b')](_0x2c1c4e);}};}function respondWithFilteredResult(_0x59135e,_0x121c5a){return function(_0x42afa3){if(_0x42afa3){var _0x27af01=typeof _0x121c5a[_0x82ff('0x1c')]===_0x82ff('0x1d')&&typeof _0x121c5a[_0x82ff('0x1e')]===_0x82ff('0x1d');var _0x5659a0=_0x42afa3[_0x82ff('0x1f')];var _0x97c74d=_0x27af01?0x0:_0x121c5a[_0x82ff('0x1c')];var _0x2790bf=_0x27af01?_0x42afa3[_0x82ff('0x1f')]:_0x121c5a[_0x82ff('0x1c')]+_0x121c5a[_0x82ff('0x1e')];var _0x46e443;if(_0x2790bf>=_0x5659a0){_0x2790bf=_0x5659a0;_0x46e443=0xc8;}else{_0x46e443=0xce;}_0x59135e[_0x82ff('0x19')](_0x46e443);return _0x59135e[_0x82ff('0x20')](_0x82ff('0x21'),_0x97c74d+'-'+_0x2790bf+'/'+_0x5659a0)['json'](_0x42afa3);}return null;};}function patchUpdates(_0x24b798){return function(_0xe03e08){try{jsonpatch[_0x82ff('0x22')](_0xe03e08,_0x24b798,!![]);}catch(_0x2cad6b){return BPromise[_0x82ff('0x23')](_0x2cad6b);}return _0xe03e08[_0x82ff('0x24')]();};}function saveUpdates(_0x3f5e7a,_0x271b55){return function(_0x50fa82){if(_0x50fa82){return _0x50fa82[_0x82ff('0x25')](_0x3f5e7a)[_0x82ff('0x26')](function(_0x52f6f1){return _0x52f6f1;});}return null;};}function removeEntity(_0x5894cd,_0x213a07){return function(_0x182065){if(_0x182065){return _0x182065['destroy']()[_0x82ff('0x26')](function(){var _0x39b46d=_0x182065['get']({'plain':!![]});var _0x3de96c=_0x82ff('0x27');return db[_0x82ff('0x28')][_0x82ff('0x29')]({'where':{'type':_0x3de96c,'resourceId':_0x39b46d['id']}})['then'](function(){return _0x182065;});})[_0x82ff('0x26')](function(){_0x5894cd[_0x82ff('0x19')](0xcc)['end']();});}};}function handleEntityNotFound(_0x4f8c15,_0x14428a){return function(_0x3ef00d){if(!_0x3ef00d){_0x4f8c15[_0x82ff('0x18')](0x194);}return _0x3ef00d;};}function handleError(_0x1363d2,_0x1eb512){_0x1eb512=_0x1eb512||0x1f4;return function(_0x9e3c2a){logger[_0x82ff('0x2a')](_0x9e3c2a[_0x82ff('0x2b')]);if(_0x9e3c2a[_0x82ff('0x2c')]){delete _0x9e3c2a[_0x82ff('0x2c')];}_0x1363d2[_0x82ff('0x19')](_0x1eb512)[_0x82ff('0x2d')](_0x9e3c2a);};}exports[_0x82ff('0x2e')]=function(_0x1200c2,_0x4c8e65){var _0x5287bb={},_0xf7e61d={},_0x5e5009={'count':0x0,'rows':[]};var _0x346c39=_[_0x82ff('0x2f')](db[_0x82ff('0x30')][_0x82ff('0x31')],function(_0x2dde13){return{'name':_0x2dde13[_0x82ff('0x32')],'type':_0x2dde13['type'][_0x82ff('0x33')]};});_0xf7e61d[_0x82ff('0x34')]=_[_0x82ff('0x2f')](_0x346c39,_0x82ff('0x2c'));_0xf7e61d[_0x82ff('0x35')]=_[_0x82ff('0x36')](_0x1200c2[_0x82ff('0x35')]);_0xf7e61d[_0x82ff('0x37')]=_[_0x82ff('0x38')](_0xf7e61d[_0x82ff('0x34')],_0xf7e61d[_0x82ff('0x35')]);_0x5287bb[_0x82ff('0x39')]=_[_0x82ff('0x38')](_0xf7e61d['model'],qs[_0x82ff('0x3a')](_0x1200c2[_0x82ff('0x35')][_0x82ff('0x3a')]));_0x5287bb['attributes']=_0x5287bb[_0x82ff('0x39')]['length']?_0x5287bb[_0x82ff('0x39')]:_0xf7e61d['model'];if(!_0x1200c2[_0x82ff('0x35')][_0x82ff('0x3b')](_0x82ff('0x3c'))){_0x5287bb['limit']=qs['limit'](_0x1200c2[_0x82ff('0x35')][_0x82ff('0x1e')]);_0x5287bb['offset']=qs[_0x82ff('0x1c')](_0x1200c2[_0x82ff('0x35')][_0x82ff('0x1c')]);}_0x5287bb[_0x82ff('0x3d')]=qs[_0x82ff('0x3e')](_0x1200c2[_0x82ff('0x35')]['sort']);_0x5287bb[_0x82ff('0x3f')]=qs[_0x82ff('0x37')](_[_0x82ff('0x40')](_0x1200c2[_0x82ff('0x35')],_0xf7e61d[_0x82ff('0x37')]),_0x346c39);if(_0x1200c2['query'][_0x82ff('0x41')]){_0x5287bb['where']=_[_0x82ff('0x42')](_0x5287bb[_0x82ff('0x3f')],{'$or':_['map'](_0x346c39,function(_0xca5a20){if(_0xca5a20['type']!==_0x82ff('0x43')){var _0x4592b4={};_0x4592b4[_0xca5a20['name']]={'$like':'%'+_0x1200c2[_0x82ff('0x35')][_0x82ff('0x41')]+'%'};return _0x4592b4;}})});}_0x5287bb=_['merge']({},_0x5287bb,_0x1200c2['options']);var _0x266379={'where':_0x5287bb['where']};return db[_0x82ff('0x30')][_0x82ff('0x1f')](_0x266379)[_0x82ff('0x26')](function(_0x10003c){_0x5e5009[_0x82ff('0x1f')]=_0x10003c;if(_0x1200c2[_0x82ff('0x35')][_0x82ff('0x44')]){_0x5287bb[_0x82ff('0x45')]=[{'all':!![]}];}return db[_0x82ff('0x30')][_0x82ff('0x46')](_0x5287bb);})[_0x82ff('0x26')](function(_0x21f439){_0x5e5009['rows']=_0x21f439;return _0x5e5009;})[_0x82ff('0x26')](respondWithFilteredResult(_0x4c8e65,_0x5287bb))[_0x82ff('0x47')](handleError(_0x4c8e65,null));};exports['show']=function(_0x352caf,_0x1fbb63){var _0x2e6e35={'raw':!![],'where':{'id':_0x352caf[_0x82ff('0x48')]['id']}},_0x203e61={};_0x203e61[_0x82ff('0x34')]=_[_0x82ff('0x36')](db[_0x82ff('0x30')][_0x82ff('0x31')]);_0x203e61[_0x82ff('0x35')]=_[_0x82ff('0x36')](_0x352caf[_0x82ff('0x35')]);_0x203e61[_0x82ff('0x37')]=_[_0x82ff('0x38')](_0x203e61['model'],_0x203e61[_0x82ff('0x35')]);_0x2e6e35[_0x82ff('0x39')]=_[_0x82ff('0x38')](_0x203e61[_0x82ff('0x34')],qs[_0x82ff('0x3a')](_0x352caf[_0x82ff('0x35')][_0x82ff('0x3a')]));_0x2e6e35['attributes']=_0x2e6e35['attributes'][_0x82ff('0x49')]?_0x2e6e35['attributes']:_0x203e61['model'];if(_0x352caf[_0x82ff('0x35')][_0x82ff('0x44')]){_0x2e6e35[_0x82ff('0x45')]=[{'all':!![]}];}_0x2e6e35=_[_0x82ff('0x42')]({},_0x2e6e35,_0x352caf[_0x82ff('0x4a')]);return db['Sound'][_0x82ff('0x4b')](_0x2e6e35)['then'](handleEntityNotFound(_0x1fbb63,null))[_0x82ff('0x26')](respondWithResult(_0x1fbb63,null))[_0x82ff('0x47')](handleError(_0x1fbb63,null));};exports[_0x82ff('0x4c')]=function(_0x382b9c,_0x9f1421,_0x4b7ab6){return db['Sound'][_0x82ff('0x4b')]({'where':{'id':_0x382b9c[_0x82ff('0x48')]['id']},'attributes':[_0x82ff('0x2c'),'save_name',_0x82ff('0x4d')],'raw':!![]})[_0x82ff('0x26')](handleEntityNotFound(_0x9f1421,null))[_0x82ff('0x26')](function(_0x28673d){if(_0x28673d){if(!fs[_0x82ff('0x4e')](path[_0x82ff('0x4f')](config[_0x82ff('0x50')],'server/files/sounds/converted',_0x28673d['save_name']+'.'+_0x28673d[_0x82ff('0x4d')]))){throw new db['Sequelize'][(_0x82ff('0x51'))]('Sound\x20not\x20found');}return _0x9f1421[_0x82ff('0x4c')](path['join'](config[_0x82ff('0x50')],_0x82ff('0x52'),util[_0x82ff('0x53')](_0x82ff('0x54'),_0x28673d[_0x82ff('0x55')],_0x28673d['converted_format'])),util[_0x82ff('0x53')](_0x82ff('0x54'),_0x28673d['name'],_0x28673d[_0x82ff('0x4d')]));}return null;})[_0x82ff('0x47')](handleError(_0x9f1421,null));};exports['create']=function(_0x2c4ba1,_0x3251b9,_0x19ed9b){var _0x222465={'name':path[_0x82ff('0x56')](_0x2c4ba1[_0x82ff('0x57')][_0x82ff('0x58')],path[_0x82ff('0x59')](_0x2c4ba1['file']['originalname'])),'save_name':path[_0x82ff('0x56')](_0x2c4ba1[_0x82ff('0x57')]['filename'],path['extname'](_0x2c4ba1['file'][_0x82ff('0x5a')])),'original_format':path[_0x82ff('0x59')](_0x2c4ba1['file'][_0x82ff('0x58')])[_0x82ff('0x5b')](0x1),'converted_format':'wav'};var _0x1541f8=path[_0x82ff('0x4f')](config['root'],_0x82ff('0x5c'),_0x2c4ba1[_0x82ff('0x57')]['filename']);var _0x54476b=path[_0x82ff('0x4f')](config[_0x82ff('0x50')],'server/files/sounds/converted',util[_0x82ff('0x53')]('%s.%s',path[_0x82ff('0x56')](_0x2c4ba1[_0x82ff('0x57')]['filename'],path[_0x82ff('0x59')](_0x2c4ba1[_0x82ff('0x57')]['filename'])),_0x222465['converted_format']));logger[_0x82ff('0x5d')]('Sound\x20upload\x20filepath:\x20%s',_0x1541f8);logger['info'](_0x82ff('0x5e'),_0x54476b);try{fs['chmodSync'](_0x1541f8,parseInt('0777',0x8));var _0x2bc1b1=sox['transcode'](_0x1541f8,_0x54476b,{'sampleRate':0x1f40,'format':_0x82ff('0x5f'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x2bc1b1['on']('error',function(_0x2e6bb1){fs[_0x82ff('0x60')](_0x1541f8);_0x3251b9['status'](0x1f4)[_0x82ff('0x2d')](_0x2e6bb1);});_0x2bc1b1['on'](_0x82ff('0x61'),function(_0x6ca6d7){_0x222465[_0x82ff('0x62')]=isNaN(_0x6ca6d7[_0x82ff('0x63')])?null:_0x6ca6d7['duration']*0x3e8;_0x222465[_0x82ff('0x64')]=isNaN(_0x6ca6d7[_0x82ff('0x65')])?null:_0x6ca6d7[_0x82ff('0x65')];_0x222465['original_channelCount']=isNaN(_0x6ca6d7[_0x82ff('0x66')])?null:_0x6ca6d7['channelCount'];_0x222465['original_bitRate']=isNaN(_0x6ca6d7[_0x82ff('0x67')])?null:_0x6ca6d7[_0x82ff('0x67')];_0x222465[_0x82ff('0x68')]=isNaN(_0x6ca6d7[_0x82ff('0x69')])?null:_0x6ca6d7[_0x82ff('0x69')];});_0x2bc1b1['on']('dest',function(_0x4cc73b){_0x222465[_0x82ff('0x6a')]=isNaN(_0x4cc73b[_0x82ff('0x63')])?null:_0x4cc73b[_0x82ff('0x63')]*0x3e8;_0x222465[_0x82ff('0x6b')]=isNaN(_0x4cc73b[_0x82ff('0x65')])?null:_0x4cc73b['sampleCount'];_0x222465[_0x82ff('0x6c')]=isNaN(_0x4cc73b[_0x82ff('0x66')])?null:_0x4cc73b[_0x82ff('0x66')];_0x222465[_0x82ff('0x6d')]=isNaN(_0x4cc73b[_0x82ff('0x67')])?null:_0x4cc73b[_0x82ff('0x67')];_0x222465[_0x82ff('0x6e')]=isNaN(_0x4cc73b['sampleRate'])?null:_0x4cc73b[_0x82ff('0x69')];});_0x2bc1b1['on']('end',function(){fs[_0x82ff('0x6f')](_0x54476b,parseInt('0777',0x8));return db['Sound'][_0x82ff('0x70')](_0x222465)[_0x82ff('0x26')](function(_0x502d42){var _0x38268c={'role':_0x2c4ba1[_0x82ff('0x71')][_0x82ff('0x72')],'userProfileId':_0x2c4ba1[_0x82ff('0x71')]['userProfileId']};if(!_0x38268c)throw new Error(_0x82ff('0x73'));if(_0x38268c[_0x82ff('0x72')]===_0x82ff('0x74')){var _0x1524e3=_0x502d42[_0x82ff('0x75')]({'plain':!![]});return db[_0x82ff('0x76')][_0x82ff('0x4b')]({'where':{'name':_0x82ff('0x27'),'userProfileId':_0x38268c['userProfileId']},'raw':!![]})[_0x82ff('0x26')](function(_0x595c23){if(_0x595c23&&_0x595c23[_0x82ff('0x77')]===0x0){return db['UserProfileResource']['create']({'name':_0x1524e3[_0x82ff('0x2c')],'resourceId':_0x1524e3['id'],'type':_0x595c23[_0x82ff('0x2c')],'sectionId':_0x595c23['id']},{})[_0x82ff('0x26')](function(){return _0x502d42;});}else{return _0x502d42;}})[_0x82ff('0x47')](function(_0x341136){logger[_0x82ff('0x2a')](_0x82ff('0x78'),_0x341136);throw _0x341136;});}return _0x502d42;})[_0x82ff('0x26')](respondWithResult(_0x3251b9,null))[_0x82ff('0x47')](handleError(_0x3251b9,null));});_0x2bc1b1[_0x82ff('0x79')]();}catch(_0x47a0d6){_0x3251b9[_0x82ff('0x19')](0x1f4)[_0x82ff('0x2d')](_0x47a0d6);}};exports[_0x82ff('0x25')]=function(_0x2f59b4,_0x1d51ef,_0x568c5f){return db[_0x82ff('0x30')][_0x82ff('0x4b')]({'where':{'id':_0x2f59b4[_0x82ff('0x48')]['id']}})['then'](handleEntityNotFound(_0x1d51ef,null))[_0x82ff('0x26')](saveUpdates(_[_0x82ff('0x40')](_0x2f59b4[_0x82ff('0x71')],[_0x82ff('0x2c'),_0x82ff('0x7a')]),null))[_0x82ff('0x26')](respondWithResult(_0x1d51ef,null))['catch'](handleError(_0x1d51ef,null));};exports[_0x82ff('0x7b')]=function(_0x3b355e,_0x269116,_0x51e202){return db[_0x82ff('0x30')][_0x82ff('0x4b')]({'where':{'id':_0x3b355e['params']['id']}})[_0x82ff('0x26')](function(_0x1570fc){if(_0x1570fc){return _0x1570fc[_0x82ff('0x29')]();}})[_0x82ff('0x26')](function(_0x2cff19){var _0x4b4704=path[_0x82ff('0x4f')](config[_0x82ff('0x50')],_0x82ff('0x5c'),util[_0x82ff('0x53')]('%s.%s',_0x2cff19[_0x82ff('0x55')],_0x2cff19[_0x82ff('0x7c')]));var _0xa25835=path['join'](config[_0x82ff('0x50')],_0x82ff('0x52'),util[_0x82ff('0x53')](_0x82ff('0x54'),_0x2cff19[_0x82ff('0x55')],_0x2cff19['converted_format']));fs[_0x82ff('0x60')](_0x4b4704);fs[_0x82ff('0x60')](_0xa25835);return _0x2cff19;})[_0x82ff('0x26')](function(_0x1b49b5){if(_0x1b49b5){return _0x269116[_0x82ff('0x19')](0xcc)[_0x82ff('0x1a')]();}})[_0x82ff('0x26')](handleEntityNotFound(_0x269116,null))[_0x82ff('0x47')](handleError(_0x269116,null));};