1a156cf19b01b38685a14887a16c60d20c5194cf
[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 _0x667f=['stack','name','send','index','map','rawAttributes','fieldName','key','model','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','pick','merge','type','filter','options','where','Sound','includeAll','include','rows','catch','show','find','params','save_name','converted_format','existsSync','root','server/files/sounds/converted','Sequelize','Sound\x20not\x20found','join','%s.%s','format','create','basename','originalname','file','filename','extname','substring','wav','server/files/sounds/original','info','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','unlinkSync','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','original_sampleRate','sampleRate','dest','converted_sampleCount','converted_channelCount','converted_bitRate','bitRate','converted_sampleRate','body','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','userProfileId','error','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','update','eml-format','rimraf','zip-dir','fast-json-patch','moment','mustache','path','sox','to-csv','ejs','fs-extra','squel','crypto','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','offset','undefined','limit','count','set','Content-Range','json','apply','reject','save','then','get','Sounds','UserProfileResource','destroy'];(function(_0x234671,_0x533748){var _0x2bf8a4=function(_0x1eaac4){while(--_0x1eaac4){_0x234671['push'](_0x234671['shift']());}};_0x2bf8a4(++_0x533748);}(_0x667f,0x14d));var _0xf667=function(_0x18bcf8,_0x38d6c3){_0x18bcf8=_0x18bcf8-0x0;var _0x1d8542=_0x667f[_0x18bcf8];return _0x1d8542;};'use strict';var emlformat=require(_0xf667('0x0'));var rimraf=require(_0xf667('0x1'));var zipdir=require(_0xf667('0x2'));var jsonpatch=require(_0xf667('0x3'));var rp=require('request-promise');var moment=require(_0xf667('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xf667('0x5'));var util=require('util');var path=require(_0xf667('0x6'));var sox=require(_0xf667('0x7'));var csv=require(_0xf667('0x8'));var ejs=require(_0xf667('0x9'));var fs=require('fs');var fs_extra=require(_0xf667('0xa'));var _=require('lodash');var squel=require(_0xf667('0xb'));var crypto=require(_0xf667('0xc'));var jsforce=require('jsforce');var deskjs=require(_0xf667('0xd'));var toCsv=require(_0xf667('0x8'));var querystring=require(_0xf667('0xe'));var Papa=require(_0xf667('0xf'));var Redis=require('ioredis');var authService=require(_0xf667('0x10'));var qs=require(_0xf667('0x11'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require('../../config/license/hardware');var logger=require(_0xf667('0x12'))(_0xf667('0x13'));var utils=require(_0xf667('0x14'));var config=require(_0xf667('0x15'));var licenseUtil=require(_0xf667('0x16'));var db=require(_0xf667('0x17'))['db'];function respondWithStatusCode(_0x46c9ab,_0x3d518e){_0x3d518e=_0x3d518e||0xcc;return function(_0xe3c596){if(_0xe3c596){return _0x46c9ab[_0xf667('0x18')](_0x3d518e);}return _0x46c9ab['status'](_0x3d518e)[_0xf667('0x19')]();};}function respondWithResult(_0x201644,_0x22973d){_0x22973d=_0x22973d||0xc8;return function(_0x237082){if(_0x237082){return _0x201644[_0xf667('0x1a')](_0x22973d)['json'](_0x237082);}};}function respondWithFilteredResult(_0x374407,_0x1e46e2){return function(_0x5cf0b9){if(_0x5cf0b9){var _0x4b625a=typeof _0x1e46e2[_0xf667('0x1b')]===_0xf667('0x1c')&&typeof _0x1e46e2[_0xf667('0x1d')]===_0xf667('0x1c');var _0x36fee5=_0x5cf0b9[_0xf667('0x1e')];var _0x47f695=_0x4b625a?0x0:_0x1e46e2['offset'];var _0x5a6c03=_0x4b625a?_0x5cf0b9[_0xf667('0x1e')]:_0x1e46e2[_0xf667('0x1b')]+_0x1e46e2[_0xf667('0x1d')];var _0x4087a0;if(_0x5a6c03>=_0x36fee5){_0x5a6c03=_0x36fee5;_0x4087a0=0xc8;}else{_0x4087a0=0xce;}_0x374407['status'](_0x4087a0);return _0x374407[_0xf667('0x1f')](_0xf667('0x20'),_0x47f695+'-'+_0x5a6c03+'/'+_0x36fee5)[_0xf667('0x21')](_0x5cf0b9);}return null;};}function patchUpdates(_0x1e119e){return function(_0xb39b09){try{jsonpatch[_0xf667('0x22')](_0xb39b09,_0x1e119e,!![]);}catch(_0x1657f8){return BPromise[_0xf667('0x23')](_0x1657f8);}return _0xb39b09[_0xf667('0x24')]();};}function saveUpdates(_0x5648a7,_0xbbec62){return function(_0x1134fa){if(_0x1134fa){return _0x1134fa['update'](_0x5648a7)[_0xf667('0x25')](function(_0x2c4b25){return _0x2c4b25;});}return null;};}function removeEntity(_0xbce432,_0x5a2b97){return function(_0x35fb0c){if(_0x35fb0c){return _0x35fb0c['destroy']()[_0xf667('0x25')](function(){var _0x1141bb=_0x35fb0c[_0xf667('0x26')]({'plain':!![]});var _0x10af5b=_0xf667('0x27');return db[_0xf667('0x28')][_0xf667('0x29')]({'where':{'type':_0x10af5b,'resourceId':_0x1141bb['id']}})[_0xf667('0x25')](function(){return _0x35fb0c;});})['then'](function(){_0xbce432['status'](0xcc)['end']();});}};}function handleEntityNotFound(_0x3a7e5a,_0x3bb3bf){return function(_0x29729d){if(!_0x29729d){_0x3a7e5a[_0xf667('0x18')](0x194);}return _0x29729d;};}function handleError(_0x493693,_0x13e1c8){_0x13e1c8=_0x13e1c8||0x1f4;return function(_0x1034aa){logger['error'](_0x1034aa[_0xf667('0x2a')]);if(_0x1034aa[_0xf667('0x2b')]){delete _0x1034aa[_0xf667('0x2b')];}_0x493693[_0xf667('0x1a')](_0x13e1c8)[_0xf667('0x2c')](_0x1034aa);};}exports[_0xf667('0x2d')]=function(_0x16836c,_0x18366a){var _0x534765={},_0x3e7bf2={},_0x56aeed={'count':0x0,'rows':[]};var _0x2fc9b1=_[_0xf667('0x2e')](db['Sound'][_0xf667('0x2f')],function(_0x15427e){return{'name':_0x15427e[_0xf667('0x30')],'type':_0x15427e['type'][_0xf667('0x31')]};});_0x3e7bf2[_0xf667('0x32')]=_[_0xf667('0x2e')](_0x2fc9b1,_0xf667('0x2b'));_0x3e7bf2[_0xf667('0x33')]=_[_0xf667('0x34')](_0x16836c['query']);_0x3e7bf2[_0xf667('0x35')]=_[_0xf667('0x36')](_0x3e7bf2['model'],_0x3e7bf2[_0xf667('0x33')]);_0x534765[_0xf667('0x37')]=_[_0xf667('0x36')](_0x3e7bf2[_0xf667('0x32')],qs['fields'](_0x16836c[_0xf667('0x33')][_0xf667('0x38')]));_0x534765['attributes']=_0x534765[_0xf667('0x37')][_0xf667('0x39')]?_0x534765[_0xf667('0x37')]:_0x3e7bf2['model'];if(!_0x16836c['query'][_0xf667('0x3a')](_0xf667('0x3b'))){_0x534765['limit']=qs['limit'](_0x16836c[_0xf667('0x33')][_0xf667('0x1d')]);_0x534765[_0xf667('0x1b')]=qs[_0xf667('0x1b')](_0x16836c[_0xf667('0x33')][_0xf667('0x1b')]);}_0x534765[_0xf667('0x3c')]=qs[_0xf667('0x3d')](_0x16836c[_0xf667('0x33')][_0xf667('0x3d')]);_0x534765['where']=qs[_0xf667('0x35')](_[_0xf667('0x3e')](_0x16836c[_0xf667('0x33')],_0x3e7bf2[_0xf667('0x35')]),_0x2fc9b1);if(_0x16836c[_0xf667('0x33')]['filter']){_0x534765['where']=_[_0xf667('0x3f')](_0x534765['where'],{'$or':_[_0xf667('0x2e')](_0x2fc9b1,function(_0x32550a){if(_0x32550a[_0xf667('0x40')]!=='VIRTUAL'){var _0x12e4a8={};_0x12e4a8[_0x32550a['name']]={'$like':'%'+_0x16836c[_0xf667('0x33')][_0xf667('0x41')]+'%'};return _0x12e4a8;}})});}_0x534765=_[_0xf667('0x3f')]({},_0x534765,_0x16836c[_0xf667('0x42')]);var _0x1698be={'where':_0x534765[_0xf667('0x43')]};return db[_0xf667('0x44')][_0xf667('0x1e')](_0x1698be)[_0xf667('0x25')](function(_0x3bf329){_0x56aeed[_0xf667('0x1e')]=_0x3bf329;if(_0x16836c['query'][_0xf667('0x45')]){_0x534765[_0xf667('0x46')]=[{'all':!![]}];}return db[_0xf667('0x44')]['findAll'](_0x534765);})[_0xf667('0x25')](function(_0x336f99){_0x56aeed[_0xf667('0x47')]=_0x336f99;return _0x56aeed;})[_0xf667('0x25')](respondWithFilteredResult(_0x18366a,_0x534765))[_0xf667('0x48')](handleError(_0x18366a,null));};exports[_0xf667('0x49')]=function(_0x279ffb,_0x3d7861){var _0x48a20b={'raw':!![],'where':{'id':_0x279ffb['params']['id']}},_0x5c3ef7={};_0x5c3ef7[_0xf667('0x32')]=_['keys'](db[_0xf667('0x44')]['rawAttributes']);_0x5c3ef7['query']=_[_0xf667('0x34')](_0x279ffb[_0xf667('0x33')]);_0x5c3ef7[_0xf667('0x35')]=_[_0xf667('0x36')](_0x5c3ef7['model'],_0x5c3ef7[_0xf667('0x33')]);_0x48a20b[_0xf667('0x37')]=_[_0xf667('0x36')](_0x5c3ef7['model'],qs[_0xf667('0x38')](_0x279ffb[_0xf667('0x33')][_0xf667('0x38')]));_0x48a20b['attributes']=_0x48a20b[_0xf667('0x37')]['length']?_0x48a20b[_0xf667('0x37')]:_0x5c3ef7['model'];if(_0x279ffb[_0xf667('0x33')]['includeAll']){_0x48a20b[_0xf667('0x46')]=[{'all':!![]}];}_0x48a20b=_[_0xf667('0x3f')]({},_0x48a20b,_0x279ffb[_0xf667('0x42')]);return db[_0xf667('0x44')][_0xf667('0x4a')](_0x48a20b)[_0xf667('0x25')](handleEntityNotFound(_0x3d7861,null))[_0xf667('0x25')](respondWithResult(_0x3d7861,null))['catch'](handleError(_0x3d7861,null));};exports['download']=function(_0xa2b73e,_0x2b4df4,_0x1d4147){return db[_0xf667('0x44')][_0xf667('0x4a')]({'where':{'id':_0xa2b73e[_0xf667('0x4b')]['id']},'attributes':[_0xf667('0x2b'),_0xf667('0x4c'),_0xf667('0x4d')],'raw':!![]})[_0xf667('0x25')](handleEntityNotFound(_0x2b4df4,null))[_0xf667('0x25')](function(_0x20836c){if(_0x20836c){if(!fs[_0xf667('0x4e')](path['join'](config[_0xf667('0x4f')],_0xf667('0x50'),_0x20836c[_0xf667('0x4c')]+'.'+_0x20836c[_0xf667('0x4d')]))){throw new db[(_0xf667('0x51'))]['ValidationError'](_0xf667('0x52'));}return _0x2b4df4['download'](path[_0xf667('0x53')](config[_0xf667('0x4f')],'server/files/sounds/converted',util['format'](_0xf667('0x54'),_0x20836c[_0xf667('0x4c')],_0x20836c[_0xf667('0x4d')])),util[_0xf667('0x55')]('%s.%s',_0x20836c[_0xf667('0x2b')],_0x20836c[_0xf667('0x4d')]));}return null;})['catch'](handleError(_0x2b4df4,null));};exports[_0xf667('0x56')]=function(_0x31d1dc,_0x42a1ca,_0x109082){var _0x4f883f={'name':path[_0xf667('0x57')](_0x31d1dc['file'][_0xf667('0x58')],path['extname'](_0x31d1dc[_0xf667('0x59')][_0xf667('0x58')])),'save_name':path[_0xf667('0x57')](_0x31d1dc[_0xf667('0x59')][_0xf667('0x5a')],path[_0xf667('0x5b')](_0x31d1dc[_0xf667('0x59')]['filename'])),'original_format':path['extname'](_0x31d1dc[_0xf667('0x59')][_0xf667('0x58')])[_0xf667('0x5c')](0x1),'converted_format':_0xf667('0x5d')};var _0x5126ed=path['join'](config['root'],_0xf667('0x5e'),_0x31d1dc['file']['filename']);var _0x2f3d60=path[_0xf667('0x53')](config['root'],'server/files/sounds/converted',util[_0xf667('0x55')](_0xf667('0x54'),path[_0xf667('0x57')](_0x31d1dc[_0xf667('0x59')][_0xf667('0x5a')],path[_0xf667('0x5b')](_0x31d1dc['file']['filename'])),_0x4f883f[_0xf667('0x4d')]));logger[_0xf667('0x5f')](_0xf667('0x60'),_0x5126ed);logger[_0xf667('0x5f')](_0xf667('0x61'),_0x2f3d60);try{fs[_0xf667('0x62')](_0x5126ed,parseInt(_0xf667('0x63'),0x8));var _0xb92c65=sox[_0xf667('0x64')](_0x5126ed,_0x2f3d60,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0xb92c65['on']('error',function(_0x21dbb3){fs[_0xf667('0x65')](_0x5126ed);_0x42a1ca[_0xf667('0x1a')](0x1f4)[_0xf667('0x2c')](_0x21dbb3);});_0xb92c65['on']('src',function(_0x409aa9){_0x4f883f['original_duration']=isNaN(_0x409aa9[_0xf667('0x66')])?null:_0x409aa9[_0xf667('0x66')]*0x3e8;_0x4f883f[_0xf667('0x67')]=isNaN(_0x409aa9[_0xf667('0x68')])?null:_0x409aa9[_0xf667('0x68')];_0x4f883f[_0xf667('0x69')]=isNaN(_0x409aa9['channelCount'])?null:_0x409aa9[_0xf667('0x6a')];_0x4f883f[_0xf667('0x6b')]=isNaN(_0x409aa9['bitRate'])?null:_0x409aa9['bitRate'];_0x4f883f[_0xf667('0x6c')]=isNaN(_0x409aa9[_0xf667('0x6d')])?null:_0x409aa9[_0xf667('0x6d')];});_0xb92c65['on'](_0xf667('0x6e'),function(_0x5787be){_0x4f883f['converted_duration']=isNaN(_0x5787be['duration'])?null:_0x5787be['duration']*0x3e8;_0x4f883f[_0xf667('0x6f')]=isNaN(_0x5787be[_0xf667('0x68')])?null:_0x5787be[_0xf667('0x68')];_0x4f883f[_0xf667('0x70')]=isNaN(_0x5787be['channelCount'])?null:_0x5787be[_0xf667('0x6a')];_0x4f883f[_0xf667('0x71')]=isNaN(_0x5787be[_0xf667('0x72')])?null:_0x5787be['bitRate'];_0x4f883f[_0xf667('0x73')]=isNaN(_0x5787be[_0xf667('0x6d')])?null:_0x5787be['sampleRate'];});_0xb92c65['on']('end',function(){fs[_0xf667('0x62')](_0x2f3d60,parseInt(_0xf667('0x63'),0x8));return db[_0xf667('0x44')][_0xf667('0x56')](_0x4f883f)[_0xf667('0x25')](function(_0x14ac5e){var _0x16d9f7={'role':_0x31d1dc[_0xf667('0x74')]['role'],'userProfileId':_0x31d1dc[_0xf667('0x74')]['userProfileId']};if(!_0x16d9f7)throw new Error(_0xf667('0x75'));if(_0x16d9f7['role']===_0xf667('0x76')){var _0x249400=_0x14ac5e[_0xf667('0x26')]({'plain':!![]});return db[_0xf667('0x77')][_0xf667('0x4a')]({'where':{'name':'Sounds','userProfileId':_0x16d9f7[_0xf667('0x78')]},'raw':!![]})[_0xf667('0x25')](function(_0x229e79){if(_0x229e79&&_0x229e79['autoAssociation']===0x0){return db[_0xf667('0x28')][_0xf667('0x56')]({'name':_0x249400['name'],'resourceId':_0x249400['id'],'type':_0x229e79[_0xf667('0x2b')],'sectionId':_0x229e79['id']},{})['then'](function(){return _0x14ac5e;});}else{return _0x14ac5e;}})[_0xf667('0x48')](function(_0x5c04d6){logger[_0xf667('0x79')](_0xf667('0x7a'),_0x5c04d6);throw _0x5c04d6;});}return _0x14ac5e;})[_0xf667('0x25')](respondWithResult(_0x42a1ca,null))['catch'](handleError(_0x42a1ca,null));});_0xb92c65[_0xf667('0x7b')]();}catch(_0x1d7077){_0x42a1ca[_0xf667('0x1a')](0x1f4)['send'](_0x1d7077);}};exports[_0xf667('0x7c')]=function(_0x11cc29,_0x40c544,_0x2ca39f){return db['Sound'][_0xf667('0x4a')]({'where':{'id':_0x11cc29[_0xf667('0x4b')]['id']}})['then'](handleEntityNotFound(_0x40c544,null))[_0xf667('0x25')](saveUpdates(_[_0xf667('0x3e')](_0x11cc29[_0xf667('0x74')],[_0xf667('0x2b'),'description']),null))[_0xf667('0x25')](respondWithResult(_0x40c544,null))['catch'](handleError(_0x40c544,null));};exports['delete']=function(_0x33beea,_0x2f19c9,_0x1b0ee7){return db[_0xf667('0x44')][_0xf667('0x4a')]({'where':{'id':_0x33beea[_0xf667('0x4b')]['id']}})[_0xf667('0x25')](function(_0x50f666){if(_0x50f666){return _0x50f666[_0xf667('0x29')]();}})['then'](function(_0x548abe){var _0x9a47e2=path[_0xf667('0x53')](config[_0xf667('0x4f')],_0xf667('0x5e'),util[_0xf667('0x55')](_0xf667('0x54'),_0x548abe[_0xf667('0x4c')],_0x548abe['original_format']));var _0x5d2b4c=path[_0xf667('0x53')](config[_0xf667('0x4f')],_0xf667('0x50'),util['format']('%s.%s',_0x548abe['save_name'],_0x548abe[_0xf667('0x4d')]));fs[_0xf667('0x65')](_0x9a47e2);fs[_0xf667('0x65')](_0x5d2b4c);return _0x548abe;})[_0xf667('0x25')](function(_0x3ca932){if(_0x3ca932){return _0x2f19c9[_0xf667('0x1a')](0xcc)[_0xf667('0x19')]();}})[_0xf667('0x25')](handleEntityNotFound(_0x2f19c9,null))[_0xf667('0x48')](handleError(_0x2f19c9,null));};