Built motion from commit d69f4380.|2.6.29
[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 _0xe300=['keys','attributes','intersection','fields','hasOwnProperty','nolimit','sort','where','filters','filter','type','merge','Sound','findAll','rows','catch','length','includeAll','include','find','download','params','save_name','converted_format','existsSync','join','root','ValidationError','Sound\x20not\x20found','format','%s.%s','originalname','file','basename','extname','filename','substring','server/files/sounds/original','Sound\x20upload\x20filepath:\x20%s','transcode','wav','error','src','original_duration','duration','sampleCount','original_channelCount','channelCount','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','chmodSync','create','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','Sounds','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','description','delete','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','fs-extra','lodash','crypto','jsforce','desk.js','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','end','status','json','undefined','limit','count','offset','set','Content-Range','apply','reject','update','destroy','then','get','UserProfileResource','sendStatus','stack','name','send','index','map','rawAttributes','fieldName','key','model','query'];(function(_0x8c43de,_0x90ff31){var _0xa6591b=function(_0x469716){while(--_0x469716){_0x8c43de['push'](_0x8c43de['shift']());}};_0xa6591b(++_0x90ff31);}(_0xe300,0x1b5));var _0x0e30=function(_0xdf7a0d,_0x213f65){_0xdf7a0d=_0xdf7a0d-0x0;var _0x3a5d96=_0xe300[_0xdf7a0d];return _0x3a5d96;};'use strict';var emlformat=require(_0x0e30('0x0'));var rimraf=require(_0x0e30('0x1'));var zipdir=require(_0x0e30('0x2'));var jsonpatch=require(_0x0e30('0x3'));var rp=require(_0x0e30('0x4'));var moment=require(_0x0e30('0x5'));var BPromise=require(_0x0e30('0x6'));var Mustache=require(_0x0e30('0x7'));var util=require(_0x0e30('0x8'));var path=require(_0x0e30('0x9'));var sox=require(_0x0e30('0xa'));var csv=require(_0x0e30('0xb'));var ejs=require('ejs');var fs=require('fs');var fs_extra=require(_0x0e30('0xc'));var _=require(_0x0e30('0xd'));var squel=require('squel');var crypto=require(_0x0e30('0xe'));var jsforce=require(_0x0e30('0xf'));var deskjs=require(_0x0e30('0x10'));var toCsv=require(_0x0e30('0xb'));var querystring=require('querystring');var Papa=require('papaparse');var Redis=require(_0x0e30('0x11'));var authService=require(_0x0e30('0x12'));var qs=require(_0x0e30('0x13'));var as=require(_0x0e30('0x14'));var hardwareService=require(_0x0e30('0x15'));var logger=require(_0x0e30('0x16'))(_0x0e30('0x17'));var utils=require(_0x0e30('0x18'));var config=require(_0x0e30('0x19'));var licenseUtil=require(_0x0e30('0x1a'));var db=require(_0x0e30('0x1b'))['db'];function respondWithStatusCode(_0x531132,_0xa42f48){_0xa42f48=_0xa42f48||0xcc;return function(_0x2c11b4){if(_0x2c11b4){return _0x531132['sendStatus'](_0xa42f48);}return _0x531132['status'](_0xa42f48)[_0x0e30('0x1c')]();};}function respondWithResult(_0x4f649e,_0x3c34c9){_0x3c34c9=_0x3c34c9||0xc8;return function(_0x4f01e8){if(_0x4f01e8){return _0x4f649e[_0x0e30('0x1d')](_0x3c34c9)[_0x0e30('0x1e')](_0x4f01e8);}};}function respondWithFilteredResult(_0x5ffce1,_0x570f6e){return function(_0x220f2e){if(_0x220f2e){var _0x478e53=typeof _0x570f6e['offset']===_0x0e30('0x1f')&&typeof _0x570f6e[_0x0e30('0x20')]===_0x0e30('0x1f');var _0x439a68=_0x220f2e[_0x0e30('0x21')];var _0x52c7be=_0x478e53?0x0:_0x570f6e[_0x0e30('0x22')];var _0x16e732=_0x478e53?_0x220f2e[_0x0e30('0x21')]:_0x570f6e[_0x0e30('0x22')]+_0x570f6e[_0x0e30('0x20')];var _0x5d3306;if(_0x16e732>=_0x439a68){_0x16e732=_0x439a68;_0x5d3306=0xc8;}else{_0x5d3306=0xce;}_0x5ffce1['status'](_0x5d3306);return _0x5ffce1[_0x0e30('0x23')](_0x0e30('0x24'),_0x52c7be+'-'+_0x16e732+'/'+_0x439a68)[_0x0e30('0x1e')](_0x220f2e);}return null;};}function patchUpdates(_0x301a44){return function(_0x1af50f){try{jsonpatch[_0x0e30('0x25')](_0x1af50f,_0x301a44,!![]);}catch(_0x2b161c){return BPromise[_0x0e30('0x26')](_0x2b161c);}return _0x1af50f['save']();};}function saveUpdates(_0x287ab1,_0x4a4a09){return function(_0xb08e02){if(_0xb08e02){return _0xb08e02[_0x0e30('0x27')](_0x287ab1)['then'](function(_0x686a97){return _0x686a97;});}return null;};}function removeEntity(_0x2e308d,_0x2f2b7a){return function(_0x5f52ea){if(_0x5f52ea){return _0x5f52ea[_0x0e30('0x28')]()[_0x0e30('0x29')](function(){var _0x28a815=_0x5f52ea[_0x0e30('0x2a')]({'plain':!![]});var _0x43cf73='Sounds';return db[_0x0e30('0x2b')]['destroy']({'where':{'type':_0x43cf73,'resourceId':_0x28a815['id']}})[_0x0e30('0x29')](function(){return _0x5f52ea;});})[_0x0e30('0x29')](function(){_0x2e308d[_0x0e30('0x1d')](0xcc)['end']();});}};}function handleEntityNotFound(_0xab3b66,_0x1156f5){return function(_0xcbeec6){if(!_0xcbeec6){_0xab3b66[_0x0e30('0x2c')](0x194);}return _0xcbeec6;};}function handleError(_0x50bb4d,_0x55dcab){_0x55dcab=_0x55dcab||0x1f4;return function(_0x5a7c86){logger['error'](_0x5a7c86[_0x0e30('0x2d')]);if(_0x5a7c86[_0x0e30('0x2e')]){delete _0x5a7c86[_0x0e30('0x2e')];}_0x50bb4d['status'](_0x55dcab)[_0x0e30('0x2f')](_0x5a7c86);};}exports[_0x0e30('0x30')]=function(_0x57d29e,_0x2bc454){var _0x471482={},_0x334f60={},_0x3958d9={'count':0x0,'rows':[]};var _0xffe73f=_[_0x0e30('0x31')](db['Sound'][_0x0e30('0x32')],function(_0x353be6){return{'name':_0x353be6[_0x0e30('0x33')],'type':_0x353be6['type'][_0x0e30('0x34')]};});_0x334f60[_0x0e30('0x35')]=_[_0x0e30('0x31')](_0xffe73f,_0x0e30('0x2e'));_0x334f60[_0x0e30('0x36')]=_[_0x0e30('0x37')](_0x57d29e['query']);_0x334f60['filters']=_['intersection'](_0x334f60['model'],_0x334f60[_0x0e30('0x36')]);_0x471482[_0x0e30('0x38')]=_[_0x0e30('0x39')](_0x334f60[_0x0e30('0x35')],qs[_0x0e30('0x3a')](_0x57d29e[_0x0e30('0x36')]['fields']));_0x471482['attributes']=_0x471482[_0x0e30('0x38')]['length']?_0x471482['attributes']:_0x334f60[_0x0e30('0x35')];if(!_0x57d29e['query'][_0x0e30('0x3b')](_0x0e30('0x3c'))){_0x471482[_0x0e30('0x20')]=qs[_0x0e30('0x20')](_0x57d29e[_0x0e30('0x36')]['limit']);_0x471482[_0x0e30('0x22')]=qs[_0x0e30('0x22')](_0x57d29e['query'][_0x0e30('0x22')]);}_0x471482['order']=qs[_0x0e30('0x3d')](_0x57d29e[_0x0e30('0x36')][_0x0e30('0x3d')]);_0x471482[_0x0e30('0x3e')]=qs['filters'](_['pick'](_0x57d29e['query'],_0x334f60[_0x0e30('0x3f')]),_0xffe73f);if(_0x57d29e['query'][_0x0e30('0x40')]){_0x471482['where']=_['merge'](_0x471482[_0x0e30('0x3e')],{'$or':_[_0x0e30('0x31')](_0xffe73f,function(_0x2fa3df){if(_0x2fa3df[_0x0e30('0x41')]!=='VIRTUAL'){var _0x2db074={};_0x2db074[_0x2fa3df[_0x0e30('0x2e')]]={'$like':'%'+_0x57d29e[_0x0e30('0x36')][_0x0e30('0x40')]+'%'};return _0x2db074;}})});}_0x471482=_[_0x0e30('0x42')]({},_0x471482,_0x57d29e['options']);var _0x5f3678={'where':_0x471482[_0x0e30('0x3e')]};return db[_0x0e30('0x43')][_0x0e30('0x21')](_0x5f3678)[_0x0e30('0x29')](function(_0x2eca97){_0x3958d9['count']=_0x2eca97;if(_0x57d29e[_0x0e30('0x36')]['includeAll']){_0x471482['include']=[{'all':!![]}];}return db[_0x0e30('0x43')][_0x0e30('0x44')](_0x471482);})['then'](function(_0x1380e7){_0x3958d9[_0x0e30('0x45')]=_0x1380e7;return _0x3958d9;})['then'](respondWithFilteredResult(_0x2bc454,_0x471482))[_0x0e30('0x46')](handleError(_0x2bc454,null));};exports['show']=function(_0x3dfeeb,_0x409194){var _0x4d4cd0={'raw':!![],'where':{'id':_0x3dfeeb['params']['id']}},_0x346a46={};_0x346a46[_0x0e30('0x35')]=_[_0x0e30('0x37')](db[_0x0e30('0x43')][_0x0e30('0x32')]);_0x346a46[_0x0e30('0x36')]=_[_0x0e30('0x37')](_0x3dfeeb['query']);_0x346a46[_0x0e30('0x3f')]=_[_0x0e30('0x39')](_0x346a46[_0x0e30('0x35')],_0x346a46[_0x0e30('0x36')]);_0x4d4cd0[_0x0e30('0x38')]=_[_0x0e30('0x39')](_0x346a46['model'],qs[_0x0e30('0x3a')](_0x3dfeeb[_0x0e30('0x36')][_0x0e30('0x3a')]));_0x4d4cd0[_0x0e30('0x38')]=_0x4d4cd0['attributes'][_0x0e30('0x47')]?_0x4d4cd0[_0x0e30('0x38')]:_0x346a46[_0x0e30('0x35')];if(_0x3dfeeb['query'][_0x0e30('0x48')]){_0x4d4cd0[_0x0e30('0x49')]=[{'all':!![]}];}_0x4d4cd0=_[_0x0e30('0x42')]({},_0x4d4cd0,_0x3dfeeb['options']);return db[_0x0e30('0x43')][_0x0e30('0x4a')](_0x4d4cd0)[_0x0e30('0x29')](handleEntityNotFound(_0x409194,null))[_0x0e30('0x29')](respondWithResult(_0x409194,null))[_0x0e30('0x46')](handleError(_0x409194,null));};exports[_0x0e30('0x4b')]=function(_0x417aca,_0x2b3b45,_0x73b407){return db['Sound']['find']({'where':{'id':_0x417aca[_0x0e30('0x4c')]['id']},'attributes':[_0x0e30('0x2e'),_0x0e30('0x4d'),_0x0e30('0x4e')],'raw':!![]})[_0x0e30('0x29')](handleEntityNotFound(_0x2b3b45,null))[_0x0e30('0x29')](function(_0x4f9277){if(_0x4f9277){if(!fs[_0x0e30('0x4f')](path[_0x0e30('0x50')](config[_0x0e30('0x51')],'server/files/sounds/converted',_0x4f9277[_0x0e30('0x4d')]+'.'+_0x4f9277['converted_format']))){throw new db['Sequelize'][(_0x0e30('0x52'))](_0x0e30('0x53'));}return _0x2b3b45[_0x0e30('0x4b')](path['join'](config[_0x0e30('0x51')],'server/files/sounds/converted',util[_0x0e30('0x54')](_0x0e30('0x55'),_0x4f9277[_0x0e30('0x4d')],_0x4f9277[_0x0e30('0x4e')])),util[_0x0e30('0x54')](_0x0e30('0x55'),_0x4f9277[_0x0e30('0x2e')],_0x4f9277['converted_format']));}return null;})[_0x0e30('0x46')](handleError(_0x2b3b45,null));};exports['create']=function(_0x1636ec,_0x39596c,_0x5e57e1){var _0x53de4c={'name':path['basename'](_0x1636ec['file'][_0x0e30('0x56')],path['extname'](_0x1636ec[_0x0e30('0x57')][_0x0e30('0x56')])),'save_name':path[_0x0e30('0x58')](_0x1636ec['file']['filename'],path[_0x0e30('0x59')](_0x1636ec['file'][_0x0e30('0x5a')])),'original_format':path[_0x0e30('0x59')](_0x1636ec[_0x0e30('0x57')][_0x0e30('0x56')])[_0x0e30('0x5b')](0x1),'converted_format':'wav'};var _0x2a5bc1=path[_0x0e30('0x50')](config[_0x0e30('0x51')],_0x0e30('0x5c'),_0x1636ec['file'][_0x0e30('0x5a')]);var _0x5caa42=path[_0x0e30('0x50')](config[_0x0e30('0x51')],'server/files/sounds/converted',util[_0x0e30('0x54')]('%s.%s',path[_0x0e30('0x58')](_0x1636ec[_0x0e30('0x57')]['filename'],path['extname'](_0x1636ec[_0x0e30('0x57')][_0x0e30('0x5a')])),_0x53de4c[_0x0e30('0x4e')]));logger['info'](_0x0e30('0x5d'),_0x2a5bc1);logger['info']('Sound\x20upload\x20destination:\x20%s',_0x5caa42);try{fs['chmodSync'](_0x2a5bc1,parseInt('0777',0x8));var _0x413cfe=sox[_0x0e30('0x5e')](_0x2a5bc1,_0x5caa42,{'sampleRate':0x1f40,'format':_0x0e30('0x5f'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x413cfe['on'](_0x0e30('0x60'),function(_0x447ae0){fs['unlinkSync'](_0x2a5bc1);_0x39596c[_0x0e30('0x1d')](0x1f4)[_0x0e30('0x2f')](_0x447ae0);});_0x413cfe['on'](_0x0e30('0x61'),function(_0x421b6b){_0x53de4c[_0x0e30('0x62')]=isNaN(_0x421b6b['duration'])?null:_0x421b6b[_0x0e30('0x63')]*0x3e8;_0x53de4c['original_sampleCount']=isNaN(_0x421b6b[_0x0e30('0x64')])?null:_0x421b6b['sampleCount'];_0x53de4c[_0x0e30('0x65')]=isNaN(_0x421b6b[_0x0e30('0x66')])?null:_0x421b6b[_0x0e30('0x66')];_0x53de4c['original_bitRate']=isNaN(_0x421b6b[_0x0e30('0x67')])?null:_0x421b6b[_0x0e30('0x67')];_0x53de4c[_0x0e30('0x68')]=isNaN(_0x421b6b[_0x0e30('0x69')])?null:_0x421b6b[_0x0e30('0x69')];});_0x413cfe['on'](_0x0e30('0x6a'),function(_0x53c95c){_0x53de4c[_0x0e30('0x6b')]=isNaN(_0x53c95c['duration'])?null:_0x53c95c[_0x0e30('0x63')]*0x3e8;_0x53de4c['converted_sampleCount']=isNaN(_0x53c95c['sampleCount'])?null:_0x53c95c[_0x0e30('0x64')];_0x53de4c[_0x0e30('0x6c')]=isNaN(_0x53c95c[_0x0e30('0x66')])?null:_0x53c95c['channelCount'];_0x53de4c[_0x0e30('0x6d')]=isNaN(_0x53c95c[_0x0e30('0x67')])?null:_0x53c95c[_0x0e30('0x67')];_0x53de4c[_0x0e30('0x6e')]=isNaN(_0x53c95c[_0x0e30('0x69')])?null:_0x53c95c[_0x0e30('0x69')];});_0x413cfe['on'](_0x0e30('0x1c'),function(){fs[_0x0e30('0x6f')](_0x5caa42,parseInt('0777',0x8));return db[_0x0e30('0x43')][_0x0e30('0x70')](_0x53de4c)[_0x0e30('0x29')](function(_0xe8aca5){var _0x45ead6={'role':_0x1636ec[_0x0e30('0x71')][_0x0e30('0x72')],'userProfileId':_0x1636ec[_0x0e30('0x71')][_0x0e30('0x73')]};if(!_0x45ead6)throw new Error(_0x0e30('0x74'));if(_0x45ead6[_0x0e30('0x72')]===_0x0e30('0x75')){var _0x1158a7=_0xe8aca5[_0x0e30('0x2a')]({'plain':!![]});return db[_0x0e30('0x76')][_0x0e30('0x4a')]({'where':{'name':_0x0e30('0x77'),'userProfileId':_0x45ead6[_0x0e30('0x73')]},'raw':!![]})[_0x0e30('0x29')](function(_0x2733e5){if(_0x2733e5&&_0x2733e5['autoAssociation']===0x0){return db[_0x0e30('0x2b')]['create']({'name':_0x1158a7[_0x0e30('0x2e')],'resourceId':_0x1158a7['id'],'type':_0x2733e5[_0x0e30('0x2e')],'sectionId':_0x2733e5['id']},{})['then'](function(){return _0xe8aca5;});}else{return _0xe8aca5;}})['catch'](function(_0x28adcd){logger[_0x0e30('0x60')](_0x0e30('0x78'),_0x28adcd);throw _0x28adcd;});}return _0xe8aca5;})[_0x0e30('0x29')](respondWithResult(_0x39596c,null))[_0x0e30('0x46')](handleError(_0x39596c,null));});_0x413cfe['start']();}catch(_0x3af178){_0x39596c[_0x0e30('0x1d')](0x1f4)[_0x0e30('0x2f')](_0x3af178);}};exports['update']=function(_0x4f8124,_0x5b531f,_0x1f3b26){return db[_0x0e30('0x43')]['find']({'where':{'id':_0x4f8124[_0x0e30('0x4c')]['id']}})[_0x0e30('0x29')](handleEntityNotFound(_0x5b531f,null))['then'](saveUpdates(_['pick'](_0x4f8124[_0x0e30('0x71')],[_0x0e30('0x2e'),_0x0e30('0x79')]),null))[_0x0e30('0x29')](respondWithResult(_0x5b531f,null))[_0x0e30('0x46')](handleError(_0x5b531f,null));};exports[_0x0e30('0x7a')]=function(_0x49f285,_0x20a274,_0x1db2f5){return db[_0x0e30('0x43')]['find']({'where':{'id':_0x49f285[_0x0e30('0x4c')]['id']}})[_0x0e30('0x29')](function(_0x591db3){if(_0x591db3){return _0x591db3['destroy']();}})[_0x0e30('0x29')](function(_0x3eae61){var _0x15e4c6=path[_0x0e30('0x50')](config['root'],_0x0e30('0x5c'),util[_0x0e30('0x54')](_0x0e30('0x55'),_0x3eae61['save_name'],_0x3eae61['original_format']));var _0x238daa=path[_0x0e30('0x50')](config[_0x0e30('0x51')],'server/files/sounds/converted',util[_0x0e30('0x54')]('%s.%s',_0x3eae61[_0x0e30('0x4d')],_0x3eae61[_0x0e30('0x4e')]));fs['unlinkSync'](_0x15e4c6);fs['unlinkSync'](_0x238daa);return _0x3eae61;})[_0x0e30('0x29')](function(_0x2ff9c3){if(_0x2ff9c3){return _0x20a274[_0x0e30('0x1d')](0xcc)[_0x0e30('0x1c')]();}})[_0x0e30('0x29')](handleEntityNotFound(_0x20a274,null))['catch'](handleError(_0x20a274,null));};