Built motion from commit (unavailable).|2.2.5
[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 _0x37b4=['Unable\x20to\x20retrieve\x20the\x20current\x20user','user','get','Sounds','autoAssociation','start','delete','original_format','html-pdf','moment','bluebird','mustache','util','path','to-csv','ejs','lodash','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../mysqldb','sendStatus','status','json','offset','limit','set','Content-Range','apply','reject','update','then','destroy','UserProfileResource','end','error','stack','name','Sound','rawAttributes','model','keys','query','filters','intersection','attributes','length','nolimit','order','sort','where','pick','merge','map','type','key','field','filter','options','count','includeAll','include','findAll','catch','show','fields','find','download','params','save_name','existsSync','root','server/files/sounds/converted','converted_format','ValidationError','Sound\x20not\x20found','join','format','%s.%s','create','file','originalname','extname','basename','filename','chmodSync','0777','transcode','wav','unlinkSync','send','src','original_duration','original_sampleCount','sampleCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','converted_duration','duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','body','role'];(function(_0x4a6a33,_0x2a7001){var _0x368e76=function(_0x14dde3){while(--_0x14dde3){_0x4a6a33['push'](_0x4a6a33['shift']());}};_0x368e76(++_0x2a7001);}(_0x37b4,0xee));var _0x437b=function(_0x4cd14a,_0x3c08b4){_0x4cd14a=_0x4cd14a-0x0;var _0x59ac6a=_0x37b4[_0x4cd14a];return _0x59ac6a;};'use strict';var pdf=require(_0x437b('0x0'));var emlformat=require('eml-format');var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require('fast-json-patch');var rp=require('request-promise');var moment=require(_0x437b('0x1'));var BPromise=require(_0x437b('0x2'));var Mustache=require(_0x437b('0x3'));var util=require(_0x437b('0x4'));var path=require(_0x437b('0x5'));var sox=require('sox');var csv=require(_0x437b('0x6'));var ejs=require(_0x437b('0x7'));var fs=require('fs');var _=require(_0x437b('0x8'));var squel=require('squel');var crypto=require(_0x437b('0x9'));var jsforce=require(_0x437b('0xa'));var deskjs=require(_0x437b('0xb'));var toCsv=require('to-csv');var querystring=require(_0x437b('0xc'));var Papa=require(_0x437b('0xd'));var Redis=require(_0x437b('0xe'));var qs=require(_0x437b('0xf'));var logger=require(_0x437b('0x10'))(_0x437b('0x11'));var utils=require(_0x437b('0x12'));var config=require('../../config/environment');var db=require(_0x437b('0x13'))['db'];function respondWithStatusCode(_0x598275,_0x229f89){_0x229f89=_0x229f89||0xcc;return function(_0x2634da){if(_0x2634da){return _0x598275[_0x437b('0x14')](_0x229f89);}return _0x598275[_0x437b('0x15')](_0x229f89)['end']();};}function respondWithResult(_0x2dcaeb,_0x3579e5){_0x3579e5=_0x3579e5||0xc8;return function(_0x4c66d8){if(_0x4c66d8){return _0x2dcaeb[_0x437b('0x15')](_0x3579e5)[_0x437b('0x16')](_0x4c66d8);}};}function respondWithFilteredResult(_0x2e046b,_0x2ca5a7){return function(_0xfc4878){if(_0xfc4878){var _0x554de8=_0xfc4878['count'],_0x244988=_0x2ca5a7[_0x437b('0x17')],_0x4b4e63=_0x2ca5a7['offset']+_0x2ca5a7[_0x437b('0x18')],_0x24a65a;if(_0x4b4e63>=_0x554de8){_0x4b4e63=_0x554de8;_0x24a65a=0xc8;}else{_0x24a65a=0xce;}_0x2e046b[_0x437b('0x15')](_0x24a65a);return _0x2e046b[_0x437b('0x19')](_0x437b('0x1a'),_0x244988+'-'+_0x4b4e63+'/'+_0x554de8)['json'](_0xfc4878);}return null;};}function patchUpdates(_0x31571b){return function(_0x1e6f3d){try{jsonpatch[_0x437b('0x1b')](_0x1e6f3d,_0x31571b,!![]);}catch(_0x44cbe6){return BPromise[_0x437b('0x1c')](_0x44cbe6);}return _0x1e6f3d['save']();};}function saveUpdates(_0x151b06,_0x4c4d05){return function(_0x2e7315){if(_0x2e7315){return _0x2e7315[_0x437b('0x1d')](_0x151b06)[_0x437b('0x1e')](function(_0xeabb8c){return _0xeabb8c;});}return null;};}function removeEntity(_0x2680a5,_0x74c220){return function(_0x6e2944){if(_0x6e2944){return _0x6e2944[_0x437b('0x1f')]()[_0x437b('0x1e')](function(){var _0x11f1b3=_0x6e2944['get']({'plain':!![]});var _0x4be776='Sounds';return db[_0x437b('0x20')][_0x437b('0x1f')]({'where':{'type':_0x4be776,'resourceId':_0x11f1b3['id']}})['then'](function(){return _0x6e2944;});})[_0x437b('0x1e')](function(){_0x2680a5[_0x437b('0x15')](0xcc)[_0x437b('0x21')]();});}};}function handleEntityNotFound(_0x542b01,_0x19c22a){return function(_0x739474){if(!_0x739474){_0x542b01['sendStatus'](0x194);}return _0x739474;};}function handleError(_0x3ac557,_0xa7e455){_0xa7e455=_0xa7e455||0x1f4;return function(_0x32ca12){logger[_0x437b('0x22')](_0x32ca12[_0x437b('0x23')]);if(_0x32ca12[_0x437b('0x24')]){delete _0x32ca12[_0x437b('0x24')];}_0x3ac557[_0x437b('0x15')](_0xa7e455)['send'](_0x32ca12);};}exports['index']=function(_0x32e8d5,_0x1916c6){var _0x14fdb5={},_0x3cb32c={},_0x50b8e9={'count':0x0,'rows':[]};var _0x35a9d7=db[_0x437b('0x25')][_0x437b('0x26')];_0x3cb32c[_0x437b('0x27')]=_[_0x437b('0x28')](_0x35a9d7);_0x3cb32c[_0x437b('0x29')]=_['keys'](_0x32e8d5[_0x437b('0x29')]);_0x3cb32c[_0x437b('0x2a')]=_[_0x437b('0x2b')](_0x3cb32c[_0x437b('0x27')],_0x3cb32c[_0x437b('0x29')]);_0x14fdb5[_0x437b('0x2c')]=_['intersection'](_0x3cb32c[_0x437b('0x27')],qs['fields'](_0x32e8d5[_0x437b('0x29')]['fields']));_0x14fdb5[_0x437b('0x2c')]=_0x14fdb5[_0x437b('0x2c')][_0x437b('0x2d')]?_0x14fdb5['attributes']:_0x3cb32c[_0x437b('0x27')];if(!_0x32e8d5['query']['hasOwnProperty'](_0x437b('0x2e'))){_0x14fdb5[_0x437b('0x18')]=qs[_0x437b('0x18')](_0x32e8d5[_0x437b('0x29')]['limit']);_0x14fdb5[_0x437b('0x17')]=qs[_0x437b('0x17')](_0x32e8d5['query'][_0x437b('0x17')]);}_0x14fdb5[_0x437b('0x2f')]=qs[_0x437b('0x30')](_0x32e8d5[_0x437b('0x29')]['sort']);_0x14fdb5[_0x437b('0x31')]=qs['filters'](_[_0x437b('0x32')](_0x32e8d5[_0x437b('0x29')],_0x3cb32c[_0x437b('0x2a')]));if(_0x32e8d5[_0x437b('0x29')]['filter']){_0x14fdb5[_0x437b('0x31')]=_[_0x437b('0x33')](_0x14fdb5[_0x437b('0x31')],{'$or':_[_0x437b('0x34')](_0x35a9d7,function(_0x281380){if(_0x281380[_0x437b('0x35')][_0x437b('0x36')]!=='VIRTUAL'){var _0x2018a3={};_0x2018a3[_0x281380[_0x437b('0x37')]]={'$like':'%'+_0x32e8d5[_0x437b('0x29')][_0x437b('0x38')]+'%'};return _0x2018a3;}})});}_0x14fdb5=_[_0x437b('0x33')]({},_0x14fdb5,_0x32e8d5[_0x437b('0x39')]);var _0x40f3c7={'where':_0x14fdb5[_0x437b('0x31')]};return db[_0x437b('0x25')][_0x437b('0x3a')](_0x40f3c7)[_0x437b('0x1e')](function(_0x51ab45){_0x50b8e9[_0x437b('0x3a')]=_0x51ab45;if(_0x32e8d5[_0x437b('0x29')][_0x437b('0x3b')]){_0x14fdb5[_0x437b('0x3c')]=[{'all':!![]}];}return db[_0x437b('0x25')][_0x437b('0x3d')](_0x14fdb5);})[_0x437b('0x1e')](function(_0xbfc547){_0x50b8e9['rows']=_0xbfc547;return _0x50b8e9;})[_0x437b('0x1e')](respondWithFilteredResult(_0x1916c6,_0x14fdb5))[_0x437b('0x3e')](handleError(_0x1916c6,null));};exports[_0x437b('0x3f')]=function(_0x3ed3a3,_0x53dac4){var _0x2841ac={'raw':!![],'where':{'id':_0x3ed3a3['params']['id']}},_0x2ececa={};_0x2ececa[_0x437b('0x27')]=_[_0x437b('0x28')](db[_0x437b('0x25')][_0x437b('0x26')]);_0x2ececa['query']=_[_0x437b('0x28')](_0x3ed3a3['query']);_0x2ececa[_0x437b('0x2a')]=_[_0x437b('0x2b')](_0x2ececa[_0x437b('0x27')],_0x2ececa[_0x437b('0x29')]);_0x2841ac[_0x437b('0x2c')]=_[_0x437b('0x2b')](_0x2ececa[_0x437b('0x27')],qs[_0x437b('0x40')](_0x3ed3a3[_0x437b('0x29')][_0x437b('0x40')]));_0x2841ac['attributes']=_0x2841ac['attributes'][_0x437b('0x2d')]?_0x2841ac[_0x437b('0x2c')]:_0x2ececa[_0x437b('0x27')];if(_0x3ed3a3[_0x437b('0x29')]['includeAll']){_0x2841ac[_0x437b('0x3c')]=[{'all':!![]}];}_0x2841ac=_[_0x437b('0x33')]({},_0x2841ac,_0x3ed3a3['options']);return db[_0x437b('0x25')][_0x437b('0x41')](_0x2841ac)[_0x437b('0x1e')](handleEntityNotFound(_0x53dac4,null))[_0x437b('0x1e')](respondWithResult(_0x53dac4,null))[_0x437b('0x3e')](handleError(_0x53dac4,null));};exports[_0x437b('0x42')]=function(_0x3bfdd2,_0x4108c5,_0x267ce2){return db[_0x437b('0x25')]['find']({'where':{'id':_0x3bfdd2[_0x437b('0x43')]['id']},'attributes':[_0x437b('0x24'),_0x437b('0x44'),'converted_format'],'raw':!![]})[_0x437b('0x1e')](handleEntityNotFound(_0x4108c5,null))[_0x437b('0x1e')](function(_0x37dbdd){if(_0x37dbdd){if(!fs[_0x437b('0x45')](path['join'](config[_0x437b('0x46')],_0x437b('0x47'),_0x37dbdd[_0x437b('0x44')]+'.'+_0x37dbdd[_0x437b('0x48')]))){throw new db['Sequelize'][(_0x437b('0x49'))](_0x437b('0x4a'));}return _0x4108c5[_0x437b('0x42')](path[_0x437b('0x4b')](config[_0x437b('0x46')],_0x437b('0x47'),util[_0x437b('0x4c')](_0x437b('0x4d'),_0x37dbdd['save_name'],_0x37dbdd[_0x437b('0x48')])),util[_0x437b('0x4c')](_0x437b('0x4d'),_0x37dbdd[_0x437b('0x24')],_0x37dbdd[_0x437b('0x48')]));}return null;})[_0x437b('0x3e')](handleError(_0x4108c5,null));};exports[_0x437b('0x4e')]=function(_0x5968cc,_0x5d8564,_0xba6737){var _0x1e3afc={'name':path['basename'](_0x5968cc[_0x437b('0x4f')][_0x437b('0x50')],path[_0x437b('0x51')](_0x5968cc[_0x437b('0x4f')][_0x437b('0x50')])),'save_name':path[_0x437b('0x52')](_0x5968cc[_0x437b('0x4f')][_0x437b('0x53')],path[_0x437b('0x51')](_0x5968cc[_0x437b('0x4f')][_0x437b('0x53')])),'original_format':path[_0x437b('0x51')](_0x5968cc[_0x437b('0x4f')][_0x437b('0x50')])['substring'](0x1),'converted_format':'wav'};var _0x3707b0=path['join'](config[_0x437b('0x46')],'server/files/sounds/original',_0x5968cc[_0x437b('0x4f')]['filename']);var _0x4a42d4=path[_0x437b('0x4b')](config[_0x437b('0x46')],'server/files/sounds/converted',util[_0x437b('0x4c')]('%s.%s',path[_0x437b('0x52')](_0x5968cc[_0x437b('0x4f')][_0x437b('0x53')],path[_0x437b('0x51')](_0x5968cc['file'][_0x437b('0x53')])),_0x1e3afc[_0x437b('0x48')]));logger['info']('Sound\x20upload\x20filepath:\x20%s',_0x3707b0);logger['info']('Sound\x20upload\x20destination:\x20%s',_0x4a42d4);try{fs[_0x437b('0x54')](_0x3707b0,parseInt(_0x437b('0x55'),0x8));var _0x12c1d1=sox[_0x437b('0x56')](_0x3707b0,_0x4a42d4,{'sampleRate':0x1f40,'format':_0x437b('0x57'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x12c1d1['on'](_0x437b('0x22'),function(_0x5e2fde){fs[_0x437b('0x58')](_0x3707b0);_0x5d8564[_0x437b('0x15')](0x1f4)[_0x437b('0x59')](_0x5e2fde);});_0x12c1d1['on'](_0x437b('0x5a'),function(_0x383281){_0x1e3afc[_0x437b('0x5b')]=isNaN(_0x383281['duration'])?null:_0x383281['duration']*0x3e8;_0x1e3afc[_0x437b('0x5c')]=isNaN(_0x383281[_0x437b('0x5d')])?null:_0x383281[_0x437b('0x5d')];_0x1e3afc['original_channelCount']=isNaN(_0x383281['channelCount'])?null:_0x383281[_0x437b('0x5e')];_0x1e3afc[_0x437b('0x5f')]=isNaN(_0x383281[_0x437b('0x60')])?null:_0x383281[_0x437b('0x60')];_0x1e3afc[_0x437b('0x61')]=isNaN(_0x383281[_0x437b('0x62')])?null:_0x383281[_0x437b('0x62')];});_0x12c1d1['on']('dest',function(_0x144046){_0x1e3afc[_0x437b('0x63')]=isNaN(_0x144046[_0x437b('0x64')])?null:_0x144046['duration']*0x3e8;_0x1e3afc[_0x437b('0x65')]=isNaN(_0x144046[_0x437b('0x5d')])?null:_0x144046[_0x437b('0x5d')];_0x1e3afc[_0x437b('0x66')]=isNaN(_0x144046[_0x437b('0x5e')])?null:_0x144046[_0x437b('0x5e')];_0x1e3afc[_0x437b('0x67')]=isNaN(_0x144046[_0x437b('0x60')])?null:_0x144046[_0x437b('0x60')];_0x1e3afc[_0x437b('0x68')]=isNaN(_0x144046[_0x437b('0x62')])?null:_0x144046[_0x437b('0x62')];});_0x12c1d1['on']('end',function(){fs[_0x437b('0x54')](_0x4a42d4,parseInt(_0x437b('0x55'),0x8));return db[_0x437b('0x25')][_0x437b('0x4e')](_0x1e3afc)[_0x437b('0x1e')](function(_0x1ae082){var _0x2481cb={'role':_0x5968cc[_0x437b('0x69')][_0x437b('0x6a')],'userProfileId':_0x5968cc[_0x437b('0x69')]['userProfileId']};if(!_0x2481cb)throw new Error(_0x437b('0x6b'));if(_0x2481cb['role']===_0x437b('0x6c')){var _0x4028d9=_0x1ae082[_0x437b('0x6d')]({'plain':!![]});return db['UserProfileSection']['find']({'where':{'name':_0x437b('0x6e'),'userProfileId':_0x2481cb['userProfileId']},'raw':!![]})[_0x437b('0x1e')](function(_0x39b81a){if(_0x39b81a&&_0x39b81a[_0x437b('0x6f')]===0x0){return db['UserProfileResource'][_0x437b('0x4e')]({'name':_0x4028d9[_0x437b('0x24')],'resourceId':_0x4028d9['id'],'type':_0x39b81a[_0x437b('0x24')],'sectionId':_0x39b81a['id']},{})[_0x437b('0x1e')](function(){return _0x1ae082;});}else{return _0x1ae082;}})['catch'](function(_0x99cf83){logger[_0x437b('0x22')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x99cf83);throw _0x99cf83;});}return _0x1ae082;})[_0x437b('0x1e')](respondWithResult(_0x5d8564,null))[_0x437b('0x3e')](handleError(_0x5d8564,null));});_0x12c1d1[_0x437b('0x70')]();}catch(_0x44d6d2){_0x5d8564[_0x437b('0x15')](0x1f4)['send'](_0x44d6d2);}};exports[_0x437b('0x1d')]=function(_0x51d7af,_0x85e3f,_0xe1e3a7){return db[_0x437b('0x25')][_0x437b('0x41')]({'where':{'id':_0x51d7af[_0x437b('0x43')]['id']}})['then'](handleEntityNotFound(_0x85e3f,null))[_0x437b('0x1e')](saveUpdates(_[_0x437b('0x32')](_0x51d7af[_0x437b('0x69')],[_0x437b('0x24'),'description']),null))[_0x437b('0x1e')](respondWithResult(_0x85e3f,null))[_0x437b('0x3e')](handleError(_0x85e3f,null));};exports[_0x437b('0x71')]=function(_0xf728c8,_0x210b3a,_0x17e3e7){return db[_0x437b('0x25')][_0x437b('0x41')]({'where':{'id':_0xf728c8[_0x437b('0x43')]['id']}})['then'](function(_0x39c5ff){if(_0x39c5ff){return _0x39c5ff[_0x437b('0x1f')]();}})[_0x437b('0x1e')](function(_0x1edb6f){var _0x1bd18a=path[_0x437b('0x4b')](config[_0x437b('0x46')],'server/files/sounds/original',util['format']('%s.%s',_0x1edb6f[_0x437b('0x44')],_0x1edb6f[_0x437b('0x72')]));var _0x11ecd2=path[_0x437b('0x4b')](config['root'],'server/files/sounds/converted',util[_0x437b('0x4c')](_0x437b('0x4d'),_0x1edb6f[_0x437b('0x44')],_0x1edb6f['converted_format']));fs[_0x437b('0x58')](_0x1bd18a);fs[_0x437b('0x58')](_0x11ecd2);return _0x1edb6f;})[_0x437b('0x1e')](function(_0x9d681a){if(_0x9d681a){return _0x210b3a[_0x437b('0x15')](0xcc)[_0x437b('0x21')]();}})[_0x437b('0x1e')](handleEntityNotFound(_0x210b3a,null))[_0x437b('0x3e')](handleError(_0x210b3a,null));};