0c249de4e3ba66f133592eb1b3829305807fc573
[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 _0x415f=['server/files/sounds/converted','Sequelize','ValidationError','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','extname','filename','substring','wav','server/files/sounds/original','info','chmodSync','0777','transcode','unlinkSync','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','body','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','user','UserProfileSection','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','send','pick','description','delete','original_format','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','crypto','jsforce','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','api','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','reject','save','update','then','destroy','get','Sounds','UserProfileResource','error','stack','name','index','map','Sound','rawAttributes','fieldName','type','key','model','keys','query','filters','attributes','intersection','fields','length','hasOwnProperty','sort','where','filter','merge','VIRTUAL','options','includeAll','include','catch','show','params','find','download','save_name','converted_format','existsSync','join','root'];(function(_0x27bd2c,_0x24bd98){var _0x3784ea=function(_0x4ca802){while(--_0x4ca802){_0x27bd2c['push'](_0x27bd2c['shift']());}};_0x3784ea(++_0x24bd98);}(_0x415f,0x12f));var _0xf415=function(_0x542459,_0x4a1296){_0x542459=_0x542459-0x0;var _0x3eaf66=_0x415f[_0x542459];return _0x3eaf66;};'use strict';var emlformat=require(_0xf415('0x0'));var rimraf=require(_0xf415('0x1'));var zipdir=require(_0xf415('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xf415('0x3'));var moment=require(_0xf415('0x4'));var BPromise=require(_0xf415('0x5'));var Mustache=require(_0xf415('0x6'));var util=require(_0xf415('0x7'));var path=require(_0xf415('0x8'));var sox=require(_0xf415('0x9'));var csv=require(_0xf415('0xa'));var ejs=require(_0xf415('0xb'));var fs=require('fs');var fs_extra=require(_0xf415('0xc'));var _=require(_0xf415('0xd'));var squel=require('squel');var crypto=require(_0xf415('0xe'));var jsforce=require(_0xf415('0xf'));var deskjs=require('desk.js');var toCsv=require(_0xf415('0xa'));var querystring=require(_0xf415('0x10'));var Papa=require(_0xf415('0x11'));var Redis=require(_0xf415('0x12'));var authService=require(_0xf415('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0xf415('0x14'));var hardwareService=require(_0xf415('0x15'));var logger=require('../../config/logger')(_0xf415('0x16'));var utils=require('../../config/utils');var config=require(_0xf415('0x17'));var licenseUtil=require(_0xf415('0x18'));var db=require(_0xf415('0x19'))['db'];function respondWithStatusCode(_0x44aa16,_0x1349ea){_0x1349ea=_0x1349ea||0xcc;return function(_0xc9f2f4){if(_0xc9f2f4){return _0x44aa16[_0xf415('0x1a')](_0x1349ea);}return _0x44aa16['status'](_0x1349ea)[_0xf415('0x1b')]();};}function respondWithResult(_0x5e8690,_0x55d78d){_0x55d78d=_0x55d78d||0xc8;return function(_0x49c217){if(_0x49c217){return _0x5e8690[_0xf415('0x1c')](_0x55d78d)[_0xf415('0x1d')](_0x49c217);}};}function respondWithFilteredResult(_0x421690,_0x22c154){return function(_0x533309){if(_0x533309){var _0x560f7c=typeof _0x22c154[_0xf415('0x1e')]===_0xf415('0x1f')&&typeof _0x22c154[_0xf415('0x20')]==='undefined';var _0xa33850=_0x533309[_0xf415('0x21')];var _0x265993=_0x560f7c?0x0:_0x22c154[_0xf415('0x1e')];var _0x207d84=_0x560f7c?_0x533309['count']:_0x22c154[_0xf415('0x1e')]+_0x22c154[_0xf415('0x20')];var _0x456b03;if(_0x207d84>=_0xa33850){_0x207d84=_0xa33850;_0x456b03=0xc8;}else{_0x456b03=0xce;}_0x421690[_0xf415('0x1c')](_0x456b03);return _0x421690[_0xf415('0x22')](_0xf415('0x23'),_0x265993+'-'+_0x207d84+'/'+_0xa33850)['json'](_0x533309);}return null;};}function patchUpdates(_0x4bd8e9){return function(_0x1a86d4){try{jsonpatch['apply'](_0x1a86d4,_0x4bd8e9,!![]);}catch(_0x369208){return BPromise[_0xf415('0x24')](_0x369208);}return _0x1a86d4[_0xf415('0x25')]();};}function saveUpdates(_0x178ecb,_0x23ab2b){return function(_0x5ac04f){if(_0x5ac04f){return _0x5ac04f[_0xf415('0x26')](_0x178ecb)[_0xf415('0x27')](function(_0x1e9b00){return _0x1e9b00;});}return null;};}function removeEntity(_0x5610f8,_0x359114){return function(_0x1e82ce){if(_0x1e82ce){return _0x1e82ce[_0xf415('0x28')]()['then'](function(){var _0x1bf35f=_0x1e82ce[_0xf415('0x29')]({'plain':!![]});var _0x1c98a7=_0xf415('0x2a');return db[_0xf415('0x2b')][_0xf415('0x28')]({'where':{'type':_0x1c98a7,'resourceId':_0x1bf35f['id']}})[_0xf415('0x27')](function(){return _0x1e82ce;});})[_0xf415('0x27')](function(){_0x5610f8['status'](0xcc)[_0xf415('0x1b')]();});}};}function handleEntityNotFound(_0x4eec94,_0x43facd){return function(_0x1aa1fa){if(!_0x1aa1fa){_0x4eec94['sendStatus'](0x194);}return _0x1aa1fa;};}function handleError(_0x16153d,_0x974741){_0x974741=_0x974741||0x1f4;return function(_0x2b3b0d){logger[_0xf415('0x2c')](_0x2b3b0d[_0xf415('0x2d')]);if(_0x2b3b0d[_0xf415('0x2e')]){delete _0x2b3b0d['name'];}_0x16153d[_0xf415('0x1c')](_0x974741)['send'](_0x2b3b0d);};}exports[_0xf415('0x2f')]=function(_0x4ee7cd,_0xfc64fa){var _0x1b49a4={},_0x3c9ef3={},_0xe0d37b={'count':0x0,'rows':[]};var _0x1c56a9=_[_0xf415('0x30')](db[_0xf415('0x31')][_0xf415('0x32')],function(_0x452e70){return{'name':_0x452e70[_0xf415('0x33')],'type':_0x452e70[_0xf415('0x34')][_0xf415('0x35')]};});_0x3c9ef3[_0xf415('0x36')]=_[_0xf415('0x30')](_0x1c56a9,_0xf415('0x2e'));_0x3c9ef3['query']=_[_0xf415('0x37')](_0x4ee7cd[_0xf415('0x38')]);_0x3c9ef3[_0xf415('0x39')]=_['intersection'](_0x3c9ef3[_0xf415('0x36')],_0x3c9ef3['query']);_0x1b49a4[_0xf415('0x3a')]=_[_0xf415('0x3b')](_0x3c9ef3[_0xf415('0x36')],qs['fields'](_0x4ee7cd[_0xf415('0x38')][_0xf415('0x3c')]));_0x1b49a4[_0xf415('0x3a')]=_0x1b49a4[_0xf415('0x3a')][_0xf415('0x3d')]?_0x1b49a4['attributes']:_0x3c9ef3[_0xf415('0x36')];if(!_0x4ee7cd[_0xf415('0x38')][_0xf415('0x3e')]('nolimit')){_0x1b49a4[_0xf415('0x20')]=qs[_0xf415('0x20')](_0x4ee7cd[_0xf415('0x38')][_0xf415('0x20')]);_0x1b49a4[_0xf415('0x1e')]=qs['offset'](_0x4ee7cd[_0xf415('0x38')][_0xf415('0x1e')]);}_0x1b49a4['order']=qs[_0xf415('0x3f')](_0x4ee7cd['query'][_0xf415('0x3f')]);_0x1b49a4[_0xf415('0x40')]=qs['filters'](_['pick'](_0x4ee7cd['query'],_0x3c9ef3['filters']),_0x1c56a9);if(_0x4ee7cd['query'][_0xf415('0x41')]){_0x1b49a4[_0xf415('0x40')]=_[_0xf415('0x42')](_0x1b49a4[_0xf415('0x40')],{'$or':_['map'](_0x1c56a9,function(_0x530118){if(_0x530118[_0xf415('0x34')]!==_0xf415('0x43')){var _0x2eb522={};_0x2eb522[_0x530118[_0xf415('0x2e')]]={'$like':'%'+_0x4ee7cd[_0xf415('0x38')][_0xf415('0x41')]+'%'};return _0x2eb522;}})});}_0x1b49a4=_['merge']({},_0x1b49a4,_0x4ee7cd[_0xf415('0x44')]);var _0x448d95={'where':_0x1b49a4[_0xf415('0x40')]};return db[_0xf415('0x31')][_0xf415('0x21')](_0x448d95)['then'](function(_0x538853){_0xe0d37b[_0xf415('0x21')]=_0x538853;if(_0x4ee7cd[_0xf415('0x38')][_0xf415('0x45')]){_0x1b49a4[_0xf415('0x46')]=[{'all':!![]}];}return db['Sound']['findAll'](_0x1b49a4);})[_0xf415('0x27')](function(_0xa2bf6e){_0xe0d37b['rows']=_0xa2bf6e;return _0xe0d37b;})[_0xf415('0x27')](respondWithFilteredResult(_0xfc64fa,_0x1b49a4))[_0xf415('0x47')](handleError(_0xfc64fa,null));};exports[_0xf415('0x48')]=function(_0x1c4bee,_0x7a76e4){var _0x1dcb3a={'raw':!![],'where':{'id':_0x1c4bee[_0xf415('0x49')]['id']}},_0x55d3e3={};_0x55d3e3[_0xf415('0x36')]=_[_0xf415('0x37')](db['Sound'][_0xf415('0x32')]);_0x55d3e3['query']=_['keys'](_0x1c4bee[_0xf415('0x38')]);_0x55d3e3[_0xf415('0x39')]=_[_0xf415('0x3b')](_0x55d3e3[_0xf415('0x36')],_0x55d3e3[_0xf415('0x38')]);_0x1dcb3a[_0xf415('0x3a')]=_[_0xf415('0x3b')](_0x55d3e3[_0xf415('0x36')],qs[_0xf415('0x3c')](_0x1c4bee[_0xf415('0x38')][_0xf415('0x3c')]));_0x1dcb3a[_0xf415('0x3a')]=_0x1dcb3a[_0xf415('0x3a')][_0xf415('0x3d')]?_0x1dcb3a[_0xf415('0x3a')]:_0x55d3e3[_0xf415('0x36')];if(_0x1c4bee[_0xf415('0x38')][_0xf415('0x45')]){_0x1dcb3a[_0xf415('0x46')]=[{'all':!![]}];}_0x1dcb3a=_[_0xf415('0x42')]({},_0x1dcb3a,_0x1c4bee[_0xf415('0x44')]);return db['Sound'][_0xf415('0x4a')](_0x1dcb3a)[_0xf415('0x27')](handleEntityNotFound(_0x7a76e4,null))[_0xf415('0x27')](respondWithResult(_0x7a76e4,null))[_0xf415('0x47')](handleError(_0x7a76e4,null));};exports[_0xf415('0x4b')]=function(_0x2f52fd,_0x27fe44,_0x2524e9){return db['Sound'][_0xf415('0x4a')]({'where':{'id':_0x2f52fd[_0xf415('0x49')]['id']},'attributes':[_0xf415('0x2e'),_0xf415('0x4c'),_0xf415('0x4d')],'raw':!![]})['then'](handleEntityNotFound(_0x27fe44,null))['then'](function(_0x228c95){if(_0x228c95){if(!fs[_0xf415('0x4e')](path[_0xf415('0x4f')](config[_0xf415('0x50')],_0xf415('0x51'),_0x228c95[_0xf415('0x4c')]+'.'+_0x228c95[_0xf415('0x4d')]))){throw new db[(_0xf415('0x52'))][(_0xf415('0x53'))](_0xf415('0x54'));}return _0x27fe44['download'](path[_0xf415('0x4f')](config[_0xf415('0x50')],_0xf415('0x51'),util['format']('%s.%s',_0x228c95[_0xf415('0x4c')],_0x228c95[_0xf415('0x4d')])),util[_0xf415('0x55')](_0xf415('0x56'),_0x228c95[_0xf415('0x2e')],_0x228c95[_0xf415('0x4d')]));}return null;})[_0xf415('0x47')](handleError(_0x27fe44,null));};exports[_0xf415('0x57')]=function(_0x1d231e,_0x44f640,_0x1844ac){var _0x596875={'name':path[_0xf415('0x58')](_0x1d231e[_0xf415('0x59')][_0xf415('0x5a')],path[_0xf415('0x5b')](_0x1d231e[_0xf415('0x59')][_0xf415('0x5a')])),'save_name':path[_0xf415('0x58')](_0x1d231e[_0xf415('0x59')]['filename'],path[_0xf415('0x5b')](_0x1d231e['file'][_0xf415('0x5c')])),'original_format':path[_0xf415('0x5b')](_0x1d231e[_0xf415('0x59')]['originalname'])[_0xf415('0x5d')](0x1),'converted_format':_0xf415('0x5e')};var _0x6994ff=path[_0xf415('0x4f')](config[_0xf415('0x50')],_0xf415('0x5f'),_0x1d231e[_0xf415('0x59')][_0xf415('0x5c')]);var _0x14556c=path['join'](config[_0xf415('0x50')],'server/files/sounds/converted',util[_0xf415('0x55')](_0xf415('0x56'),path['basename'](_0x1d231e['file']['filename'],path[_0xf415('0x5b')](_0x1d231e[_0xf415('0x59')][_0xf415('0x5c')])),_0x596875[_0xf415('0x4d')]));logger[_0xf415('0x60')]('Sound\x20upload\x20filepath:\x20%s',_0x6994ff);logger[_0xf415('0x60')]('Sound\x20upload\x20destination:\x20%s',_0x14556c);try{fs[_0xf415('0x61')](_0x6994ff,parseInt(_0xf415('0x62'),0x8));var _0x4dabdf=sox[_0xf415('0x63')](_0x6994ff,_0x14556c,{'sampleRate':0x1f40,'format':_0xf415('0x5e'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x4dabdf['on'](_0xf415('0x2c'),function(_0xf883e3){fs[_0xf415('0x64')](_0x6994ff);_0x44f640[_0xf415('0x1c')](0x1f4)['send'](_0xf883e3);});_0x4dabdf['on'](_0xf415('0x65'),function(_0xba0cd3){_0x596875['original_duration']=isNaN(_0xba0cd3[_0xf415('0x66')])?null:_0xba0cd3[_0xf415('0x66')]*0x3e8;_0x596875[_0xf415('0x67')]=isNaN(_0xba0cd3[_0xf415('0x68')])?null:_0xba0cd3[_0xf415('0x68')];_0x596875[_0xf415('0x69')]=isNaN(_0xba0cd3[_0xf415('0x6a')])?null:_0xba0cd3[_0xf415('0x6a')];_0x596875[_0xf415('0x6b')]=isNaN(_0xba0cd3[_0xf415('0x6c')])?null:_0xba0cd3[_0xf415('0x6c')];_0x596875[_0xf415('0x6d')]=isNaN(_0xba0cd3[_0xf415('0x6e')])?null:_0xba0cd3['sampleRate'];});_0x4dabdf['on']('dest',function(_0x275401){_0x596875[_0xf415('0x6f')]=isNaN(_0x275401[_0xf415('0x66')])?null:_0x275401[_0xf415('0x66')]*0x3e8;_0x596875['converted_sampleCount']=isNaN(_0x275401[_0xf415('0x68')])?null:_0x275401['sampleCount'];_0x596875[_0xf415('0x70')]=isNaN(_0x275401[_0xf415('0x6a')])?null:_0x275401[_0xf415('0x6a')];_0x596875[_0xf415('0x71')]=isNaN(_0x275401[_0xf415('0x6c')])?null:_0x275401['bitRate'];_0x596875[_0xf415('0x72')]=isNaN(_0x275401[_0xf415('0x6e')])?null:_0x275401[_0xf415('0x6e')];});_0x4dabdf['on'](_0xf415('0x1b'),function(){fs[_0xf415('0x61')](_0x14556c,parseInt(_0xf415('0x62'),0x8));return db[_0xf415('0x31')][_0xf415('0x57')](_0x596875)[_0xf415('0x27')](function(_0x47d947){var _0x43287a={'role':_0x1d231e[_0xf415('0x73')]['role'],'userProfileId':_0x1d231e[_0xf415('0x73')][_0xf415('0x74')]};if(!_0x43287a)throw new Error(_0xf415('0x75'));if(_0x43287a[_0xf415('0x76')]===_0xf415('0x77')){var _0x482e7a=_0x47d947[_0xf415('0x29')]({'plain':!![]});return db[_0xf415('0x78')][_0xf415('0x4a')]({'where':{'name':_0xf415('0x2a'),'userProfileId':_0x43287a[_0xf415('0x74')]},'raw':!![]})[_0xf415('0x27')](function(_0x2ea78f){if(_0x2ea78f&&_0x2ea78f['autoAssociation']===0x0){return db[_0xf415('0x2b')][_0xf415('0x57')]({'name':_0x482e7a[_0xf415('0x2e')],'resourceId':_0x482e7a['id'],'type':_0x2ea78f[_0xf415('0x2e')],'sectionId':_0x2ea78f['id']},{})['then'](function(){return _0x47d947;});}else{return _0x47d947;}})['catch'](function(_0x19c754){logger[_0xf415('0x2c')](_0xf415('0x79'),_0x19c754);throw _0x19c754;});}return _0x47d947;})[_0xf415('0x27')](respondWithResult(_0x44f640,null))[_0xf415('0x47')](handleError(_0x44f640,null));});_0x4dabdf[_0xf415('0x7a')]();}catch(_0x4ade7d){_0x44f640[_0xf415('0x1c')](0x1f4)[_0xf415('0x7b')](_0x4ade7d);}};exports[_0xf415('0x26')]=function(_0x329ec0,_0x2c2d11,_0x31bb4d){return db[_0xf415('0x31')]['find']({'where':{'id':_0x329ec0[_0xf415('0x49')]['id']}})[_0xf415('0x27')](handleEntityNotFound(_0x2c2d11,null))[_0xf415('0x27')](saveUpdates(_[_0xf415('0x7c')](_0x329ec0[_0xf415('0x73')],[_0xf415('0x2e'),_0xf415('0x7d')]),null))[_0xf415('0x27')](respondWithResult(_0x2c2d11,null))[_0xf415('0x47')](handleError(_0x2c2d11,null));};exports[_0xf415('0x7e')]=function(_0x4415f2,_0x5245b4,_0x10a0d0){return db[_0xf415('0x31')][_0xf415('0x4a')]({'where':{'id':_0x4415f2[_0xf415('0x49')]['id']}})[_0xf415('0x27')](function(_0x284914){if(_0x284914){return _0x284914[_0xf415('0x28')]();}})[_0xf415('0x27')](function(_0x179b56){var _0x150377=path[_0xf415('0x4f')](config[_0xf415('0x50')],_0xf415('0x5f'),util[_0xf415('0x55')]('%s.%s',_0x179b56[_0xf415('0x4c')],_0x179b56[_0xf415('0x7f')]));var _0x7f1c6b=path[_0xf415('0x4f')](config[_0xf415('0x50')],_0xf415('0x51'),util[_0xf415('0x55')]('%s.%s',_0x179b56[_0xf415('0x4c')],_0x179b56[_0xf415('0x4d')]));fs[_0xf415('0x64')](_0x150377);fs[_0xf415('0x64')](_0x7f1c6b);return _0x179b56;})[_0xf415('0x27')](function(_0x2ab9f0){if(_0x2ab9f0){return _0x5245b4[_0xf415('0x1c')](0xcc)[_0xf415('0x1b')]();}})['then'](handleEntityNotFound(_0x5245b4,null))[_0xf415('0x47')](handleError(_0x5245b4,null));};