35b46c9d10289fc0cde29ba2f2b0bb92f82d2daf
[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 _0x7107=['../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','end','status','json','offset','undefined','limit','count','set','Content-Range','apply','reject','save','update','then','destroy','get','UserProfileResource','error','stack','name','index','map','type','key','model','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','sort','where','filters','filter','VIRTUAL','merge','options','Sound','includeAll','include','catch','show','keys','rawAttributes','find','download','save_name','converted_format','existsSync','join','root','Sequelize','ValidationError','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','extname','substring','wav','filename','server/files/sounds/converted','info','transcode','unlinkSync','send','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','0777','body','role','userProfileId','user','UserProfileSection','Sounds','autoAssociation','params','pick','delete','server/files/sounds/original','rimraf','zip-dir','fast-json-patch','moment','mustache','util','path','sox','to-csv','ejs','fs-extra','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs'];(function(_0x4b5325,_0x5bc67f){var _0x3d8fe5=function(_0x5c0b5e){while(--_0x5c0b5e){_0x4b5325['push'](_0x4b5325['shift']());}};_0x3d8fe5(++_0x5bc67f);}(_0x7107,0xe9));var _0x7710=function(_0x1ff7f5,_0xf0e2e7){_0x1ff7f5=_0x1ff7f5-0x0;var _0x31cbc5=_0x7107[_0x1ff7f5];return _0x31cbc5;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x7710('0x0'));var zipdir=require(_0x7710('0x1'));var jsonpatch=require(_0x7710('0x2'));var rp=require('request-promise');var moment=require(_0x7710('0x3'));var BPromise=require('bluebird');var Mustache=require(_0x7710('0x4'));var util=require(_0x7710('0x5'));var path=require(_0x7710('0x6'));var sox=require(_0x7710('0x7'));var csv=require(_0x7710('0x8'));var ejs=require(_0x7710('0x9'));var fs=require('fs');var fs_extra=require(_0x7710('0xa'));var _=require('lodash');var squel=require(_0x7710('0xb'));var crypto=require(_0x7710('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x7710('0xd'));var toCsv=require(_0x7710('0x8'));var querystring=require(_0x7710('0xe'));var Papa=require(_0x7710('0xf'));var Redis=require(_0x7710('0x10'));var authService=require(_0x7710('0x11'));var qs=require(_0x7710('0x12'));var as=require(_0x7710('0x13'));var hardwareService=require(_0x7710('0x14'));var logger=require(_0x7710('0x15'))(_0x7710('0x16'));var utils=require(_0x7710('0x17'));var config=require(_0x7710('0x18'));var licenseUtil=require(_0x7710('0x19'));var db=require(_0x7710('0x1a'))['db'];function respondWithStatusCode(_0x3b4557,_0x5f167e){_0x5f167e=_0x5f167e||0xcc;return function(_0xee0f42){if(_0xee0f42){return _0x3b4557[_0x7710('0x1b')](_0x5f167e);}return _0x3b4557['status'](_0x5f167e)[_0x7710('0x1c')]();};}function respondWithResult(_0x5269a8,_0xb3566e){_0xb3566e=_0xb3566e||0xc8;return function(_0x129ce0){if(_0x129ce0){return _0x5269a8[_0x7710('0x1d')](_0xb3566e)[_0x7710('0x1e')](_0x129ce0);}};}function respondWithFilteredResult(_0x217446,_0x2101bb){return function(_0x10eee5){if(_0x10eee5){var _0x311267=typeof _0x2101bb[_0x7710('0x1f')]===_0x7710('0x20')&&typeof _0x2101bb[_0x7710('0x21')]===_0x7710('0x20');var _0x4762dd=_0x10eee5[_0x7710('0x22')];var _0x139765=_0x311267?0x0:_0x2101bb[_0x7710('0x1f')];var _0x49f030=_0x311267?_0x10eee5[_0x7710('0x22')]:_0x2101bb[_0x7710('0x1f')]+_0x2101bb['limit'];var _0x31b4c3;if(_0x49f030>=_0x4762dd){_0x49f030=_0x4762dd;_0x31b4c3=0xc8;}else{_0x31b4c3=0xce;}_0x217446[_0x7710('0x1d')](_0x31b4c3);return _0x217446[_0x7710('0x23')](_0x7710('0x24'),_0x139765+'-'+_0x49f030+'/'+_0x4762dd)[_0x7710('0x1e')](_0x10eee5);}return null;};}function patchUpdates(_0x3f740b){return function(_0x3b29f5){try{jsonpatch[_0x7710('0x25')](_0x3b29f5,_0x3f740b,!![]);}catch(_0x4f7d22){return BPromise[_0x7710('0x26')](_0x4f7d22);}return _0x3b29f5[_0x7710('0x27')]();};}function saveUpdates(_0xfe61af,_0x236ce1){return function(_0x136205){if(_0x136205){return _0x136205[_0x7710('0x28')](_0xfe61af)[_0x7710('0x29')](function(_0x47b687){return _0x47b687;});}return null;};}function removeEntity(_0xff2349,_0x15928e){return function(_0xab5d84){if(_0xab5d84){return _0xab5d84[_0x7710('0x2a')]()['then'](function(){var _0x39be92=_0xab5d84[_0x7710('0x2b')]({'plain':!![]});var _0x28a2f1='Sounds';return db[_0x7710('0x2c')]['destroy']({'where':{'type':_0x28a2f1,'resourceId':_0x39be92['id']}})[_0x7710('0x29')](function(){return _0xab5d84;});})[_0x7710('0x29')](function(){_0xff2349[_0x7710('0x1d')](0xcc)[_0x7710('0x1c')]();});}};}function handleEntityNotFound(_0x4f7dcb,_0x46b642){return function(_0x4386bb){if(!_0x4386bb){_0x4f7dcb[_0x7710('0x1b')](0x194);}return _0x4386bb;};}function handleError(_0x5ab698,_0x25de1c){_0x25de1c=_0x25de1c||0x1f4;return function(_0x373937){logger[_0x7710('0x2d')](_0x373937[_0x7710('0x2e')]);if(_0x373937[_0x7710('0x2f')]){delete _0x373937[_0x7710('0x2f')];}_0x5ab698['status'](_0x25de1c)['send'](_0x373937);};}exports[_0x7710('0x30')]=function(_0x4a0f83,_0x28a97d){var _0x3fa1ab={},_0x1fdb33={},_0x21a2e6={'count':0x0,'rows':[]};var _0xb21ae2=_[_0x7710('0x31')](db['Sound']['rawAttributes'],function(_0x1a2784){return{'name':_0x1a2784['fieldName'],'type':_0x1a2784[_0x7710('0x32')][_0x7710('0x33')]};});_0x1fdb33[_0x7710('0x34')]=_[_0x7710('0x31')](_0xb21ae2,_0x7710('0x2f'));_0x1fdb33['query']=_['keys'](_0x4a0f83[_0x7710('0x35')]);_0x1fdb33['filters']=_[_0x7710('0x36')](_0x1fdb33[_0x7710('0x34')],_0x1fdb33[_0x7710('0x35')]);_0x3fa1ab[_0x7710('0x37')]=_[_0x7710('0x36')](_0x1fdb33[_0x7710('0x34')],qs['fields'](_0x4a0f83[_0x7710('0x35')][_0x7710('0x38')]));_0x3fa1ab[_0x7710('0x37')]=_0x3fa1ab[_0x7710('0x37')][_0x7710('0x39')]?_0x3fa1ab['attributes']:_0x1fdb33[_0x7710('0x34')];if(!_0x4a0f83['query'][_0x7710('0x3a')](_0x7710('0x3b'))){_0x3fa1ab[_0x7710('0x21')]=qs[_0x7710('0x21')](_0x4a0f83[_0x7710('0x35')]['limit']);_0x3fa1ab[_0x7710('0x1f')]=qs['offset'](_0x4a0f83['query'][_0x7710('0x1f')]);}_0x3fa1ab['order']=qs[_0x7710('0x3c')](_0x4a0f83[_0x7710('0x35')][_0x7710('0x3c')]);_0x3fa1ab[_0x7710('0x3d')]=qs[_0x7710('0x3e')](_['pick'](_0x4a0f83[_0x7710('0x35')],_0x1fdb33[_0x7710('0x3e')]),_0xb21ae2);if(_0x4a0f83[_0x7710('0x35')][_0x7710('0x3f')]){_0x3fa1ab['where']=_['merge'](_0x3fa1ab[_0x7710('0x3d')],{'$or':_[_0x7710('0x31')](_0xb21ae2,function(_0x38e222){if(_0x38e222[_0x7710('0x32')]!==_0x7710('0x40')){var _0x3045c5={};_0x3045c5[_0x38e222[_0x7710('0x2f')]]={'$like':'%'+_0x4a0f83[_0x7710('0x35')][_0x7710('0x3f')]+'%'};return _0x3045c5;}})});}_0x3fa1ab=_[_0x7710('0x41')]({},_0x3fa1ab,_0x4a0f83[_0x7710('0x42')]);var _0xb26cef={'where':_0x3fa1ab[_0x7710('0x3d')]};return db[_0x7710('0x43')][_0x7710('0x22')](_0xb26cef)[_0x7710('0x29')](function(_0x3324d2){_0x21a2e6[_0x7710('0x22')]=_0x3324d2;if(_0x4a0f83[_0x7710('0x35')][_0x7710('0x44')]){_0x3fa1ab[_0x7710('0x45')]=[{'all':!![]}];}return db[_0x7710('0x43')]['findAll'](_0x3fa1ab);})[_0x7710('0x29')](function(_0x49b557){_0x21a2e6['rows']=_0x49b557;return _0x21a2e6;})[_0x7710('0x29')](respondWithFilteredResult(_0x28a97d,_0x3fa1ab))[_0x7710('0x46')](handleError(_0x28a97d,null));};exports[_0x7710('0x47')]=function(_0x576959,_0x342b46){var _0x56ea68={'raw':!![],'where':{'id':_0x576959['params']['id']}},_0x110311={};_0x110311[_0x7710('0x34')]=_[_0x7710('0x48')](db['Sound'][_0x7710('0x49')]);_0x110311[_0x7710('0x35')]=_['keys'](_0x576959['query']);_0x110311[_0x7710('0x3e')]=_[_0x7710('0x36')](_0x110311['model'],_0x110311[_0x7710('0x35')]);_0x56ea68[_0x7710('0x37')]=_[_0x7710('0x36')](_0x110311[_0x7710('0x34')],qs[_0x7710('0x38')](_0x576959['query'][_0x7710('0x38')]));_0x56ea68[_0x7710('0x37')]=_0x56ea68['attributes'][_0x7710('0x39')]?_0x56ea68[_0x7710('0x37')]:_0x110311['model'];if(_0x576959[_0x7710('0x35')][_0x7710('0x44')]){_0x56ea68[_0x7710('0x45')]=[{'all':!![]}];}_0x56ea68=_[_0x7710('0x41')]({},_0x56ea68,_0x576959[_0x7710('0x42')]);return db[_0x7710('0x43')][_0x7710('0x4a')](_0x56ea68)['then'](handleEntityNotFound(_0x342b46,null))[_0x7710('0x29')](respondWithResult(_0x342b46,null))[_0x7710('0x46')](handleError(_0x342b46,null));};exports[_0x7710('0x4b')]=function(_0x12e01b,_0x8d2153,_0x29d58c){return db[_0x7710('0x43')]['find']({'where':{'id':_0x12e01b['params']['id']},'attributes':[_0x7710('0x2f'),_0x7710('0x4c'),_0x7710('0x4d')],'raw':!![]})[_0x7710('0x29')](handleEntityNotFound(_0x8d2153,null))[_0x7710('0x29')](function(_0x4e0452){if(_0x4e0452){if(!fs[_0x7710('0x4e')](path[_0x7710('0x4f')](config[_0x7710('0x50')],'server/files/sounds/converted',_0x4e0452[_0x7710('0x4c')]+'.'+_0x4e0452[_0x7710('0x4d')]))){throw new db[(_0x7710('0x51'))][(_0x7710('0x52'))](_0x7710('0x53'));}return _0x8d2153['download'](path[_0x7710('0x4f')](config['root'],'server/files/sounds/converted',util[_0x7710('0x54')](_0x7710('0x55'),_0x4e0452[_0x7710('0x4c')],_0x4e0452['converted_format'])),util['format'](_0x7710('0x55'),_0x4e0452[_0x7710('0x2f')],_0x4e0452[_0x7710('0x4d')]));}return null;})[_0x7710('0x46')](handleError(_0x8d2153,null));};exports[_0x7710('0x56')]=function(_0x210e10,_0x370964,_0x2a3f20){var _0x245740={'name':path[_0x7710('0x57')](_0x210e10[_0x7710('0x58')][_0x7710('0x59')],path['extname'](_0x210e10[_0x7710('0x58')][_0x7710('0x59')])),'save_name':path[_0x7710('0x57')](_0x210e10[_0x7710('0x58')]['filename'],path[_0x7710('0x5a')](_0x210e10[_0x7710('0x58')]['filename'])),'original_format':path[_0x7710('0x5a')](_0x210e10[_0x7710('0x58')][_0x7710('0x59')])[_0x7710('0x5b')](0x1),'converted_format':_0x7710('0x5c')};var _0x229c2e=path['join'](config[_0x7710('0x50')],'server/files/sounds/original',_0x210e10['file'][_0x7710('0x5d')]);var _0x200a0c=path[_0x7710('0x4f')](config[_0x7710('0x50')],_0x7710('0x5e'),util[_0x7710('0x54')](_0x7710('0x55'),path[_0x7710('0x57')](_0x210e10['file'][_0x7710('0x5d')],path[_0x7710('0x5a')](_0x210e10['file'][_0x7710('0x5d')])),_0x245740[_0x7710('0x4d')]));logger['info']('Sound\x20upload\x20filepath:\x20%s',_0x229c2e);logger[_0x7710('0x5f')]('Sound\x20upload\x20destination:\x20%s',_0x200a0c);try{fs['chmodSync'](_0x229c2e,parseInt('0777',0x8));var _0x42123d=sox[_0x7710('0x60')](_0x229c2e,_0x200a0c,{'sampleRate':0x1f40,'format':_0x7710('0x5c'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x42123d['on'](_0x7710('0x2d'),function(_0x5c2ae9){fs[_0x7710('0x61')](_0x229c2e);_0x370964[_0x7710('0x1d')](0x1f4)[_0x7710('0x62')](_0x5c2ae9);});_0x42123d['on'](_0x7710('0x63'),function(_0x10472d){_0x245740['original_duration']=isNaN(_0x10472d[_0x7710('0x64')])?null:_0x10472d[_0x7710('0x64')]*0x3e8;_0x245740[_0x7710('0x65')]=isNaN(_0x10472d[_0x7710('0x66')])?null:_0x10472d['sampleCount'];_0x245740[_0x7710('0x67')]=isNaN(_0x10472d[_0x7710('0x68')])?null:_0x10472d[_0x7710('0x68')];_0x245740[_0x7710('0x69')]=isNaN(_0x10472d[_0x7710('0x6a')])?null:_0x10472d[_0x7710('0x6a')];_0x245740['original_sampleRate']=isNaN(_0x10472d[_0x7710('0x6b')])?null:_0x10472d['sampleRate'];});_0x42123d['on'](_0x7710('0x6c'),function(_0x60a8fd){_0x245740[_0x7710('0x6d')]=isNaN(_0x60a8fd[_0x7710('0x64')])?null:_0x60a8fd[_0x7710('0x64')]*0x3e8;_0x245740[_0x7710('0x6e')]=isNaN(_0x60a8fd[_0x7710('0x66')])?null:_0x60a8fd['sampleCount'];_0x245740[_0x7710('0x6f')]=isNaN(_0x60a8fd[_0x7710('0x68')])?null:_0x60a8fd[_0x7710('0x68')];_0x245740[_0x7710('0x70')]=isNaN(_0x60a8fd[_0x7710('0x6a')])?null:_0x60a8fd[_0x7710('0x6a')];_0x245740[_0x7710('0x71')]=isNaN(_0x60a8fd[_0x7710('0x6b')])?null:_0x60a8fd[_0x7710('0x6b')];});_0x42123d['on'](_0x7710('0x1c'),function(){fs['chmodSync'](_0x200a0c,parseInt(_0x7710('0x72'),0x8));return db[_0x7710('0x43')][_0x7710('0x56')](_0x245740)[_0x7710('0x29')](function(_0x18f084){var _0x3a346a={'role':_0x210e10[_0x7710('0x73')][_0x7710('0x74')],'userProfileId':_0x210e10['body'][_0x7710('0x75')]};if(!_0x3a346a)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x3a346a[_0x7710('0x74')]===_0x7710('0x76')){var _0x56d6da=_0x18f084[_0x7710('0x2b')]({'plain':!![]});return db[_0x7710('0x77')][_0x7710('0x4a')]({'where':{'name':_0x7710('0x78'),'userProfileId':_0x3a346a[_0x7710('0x75')]},'raw':!![]})['then'](function(_0x134208){if(_0x134208&&_0x134208[_0x7710('0x79')]===0x0){return db[_0x7710('0x2c')]['create']({'name':_0x56d6da[_0x7710('0x2f')],'resourceId':_0x56d6da['id'],'type':_0x134208[_0x7710('0x2f')],'sectionId':_0x134208['id']},{})[_0x7710('0x29')](function(){return _0x18f084;});}else{return _0x18f084;}})['catch'](function(_0x41a561){logger[_0x7710('0x2d')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x41a561);throw _0x41a561;});}return _0x18f084;})[_0x7710('0x29')](respondWithResult(_0x370964,null))[_0x7710('0x46')](handleError(_0x370964,null));});_0x42123d['start']();}catch(_0x44423f){_0x370964[_0x7710('0x1d')](0x1f4)[_0x7710('0x62')](_0x44423f);}};exports[_0x7710('0x28')]=function(_0x4dd76b,_0xabe1d5,_0xd35c4a){return db['Sound']['find']({'where':{'id':_0x4dd76b[_0x7710('0x7a')]['id']}})['then'](handleEntityNotFound(_0xabe1d5,null))[_0x7710('0x29')](saveUpdates(_[_0x7710('0x7b')](_0x4dd76b[_0x7710('0x73')],[_0x7710('0x2f'),'description']),null))[_0x7710('0x29')](respondWithResult(_0xabe1d5,null))[_0x7710('0x46')](handleError(_0xabe1d5,null));};exports[_0x7710('0x7c')]=function(_0x495272,_0x2108a1,_0x412c58){return db[_0x7710('0x43')][_0x7710('0x4a')]({'where':{'id':_0x495272[_0x7710('0x7a')]['id']}})[_0x7710('0x29')](function(_0xe16a8f){if(_0xe16a8f){return _0xe16a8f[_0x7710('0x2a')]();}})[_0x7710('0x29')](function(_0x3852f5){var _0x279229=path[_0x7710('0x4f')](config[_0x7710('0x50')],_0x7710('0x7d'),util['format'](_0x7710('0x55'),_0x3852f5[_0x7710('0x4c')],_0x3852f5['original_format']));var _0x2c97b5=path[_0x7710('0x4f')](config[_0x7710('0x50')],'server/files/sounds/converted',util[_0x7710('0x54')]('%s.%s',_0x3852f5[_0x7710('0x4c')],_0x3852f5['converted_format']));fs[_0x7710('0x61')](_0x279229);fs[_0x7710('0x61')](_0x2c97b5);return _0x3852f5;})[_0x7710('0x29')](function(_0x52cb32){if(_0x52cb32){return _0x2108a1[_0x7710('0x1d')](0xcc)[_0x7710('0x1c')]();}})[_0x7710('0x29')](handleEntityNotFound(_0x2108a1,null))[_0x7710('0x46')](handleError(_0x2108a1,null));};