80d41cf388bbcfe42f703a5053138a0b477a102f
[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 _0xbfea=['update','server/files/sounds/original','original_format','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','lodash','squel','crypto','jsforce','desk.js','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','sendStatus','end','status','json','count','offset','limit','set','Content-Range','apply','reject','save','then','destroy','get','UserProfileResource','error','stack','name','send','index','Sound','rawAttributes','model','keys','query','intersection','attributes','fields','hasOwnProperty','order','sort','where','filters','pick','filter','merge','map','VIRTUAL','findAll','rows','params','length','include','options','find','catch','download','save_name','converted_format','existsSync','join','root','server/files/sounds/converted','Sound\x20not\x20found','format','%s.%s','create','basename','file','extname','originalname','filename','substring','wav','info','Sound\x20upload\x20filepath:\x20%s','transcode','unlinkSync','src','original_duration','duration','original_sampleCount','sampleCount','channelCount','original_bitRate','sampleRate','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','bitRate','chmodSync','body','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','role','UserProfileSection','Sounds','autoAssociation','start'];(function(_0x14add3,_0x13ce8d){var _0x4a174f=function(_0x2b3ed7){while(--_0x2b3ed7){_0x14add3['push'](_0x14add3['shift']());}};_0x4a174f(++_0x13ce8d);}(_0xbfea,0x168));var _0xabfe=function(_0x3cd6da,_0x29e669){_0x3cd6da=_0x3cd6da-0x0;var _0x27d9d5=_0xbfea[_0x3cd6da];return _0x27d9d5;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0xabfe('0x0'));var rimraf=require(_0xabfe('0x1'));var zipdir=require(_0xabfe('0x2'));var jsonpatch=require(_0xabfe('0x3'));var rp=require(_0xabfe('0x4'));var moment=require(_0xabfe('0x5'));var BPromise=require(_0xabfe('0x6'));var Mustache=require(_0xabfe('0x7'));var util=require(_0xabfe('0x8'));var path=require(_0xabfe('0x9'));var sox=require(_0xabfe('0xa'));var csv=require(_0xabfe('0xb'));var ejs=require('ejs');var fs=require('fs');var _=require(_0xabfe('0xc'));var squel=require(_0xabfe('0xd'));var crypto=require(_0xabfe('0xe'));var jsforce=require(_0xabfe('0xf'));var deskjs=require(_0xabfe('0x10'));var toCsv=require(_0xabfe('0xb'));var querystring=require('querystring');var Papa=require(_0xabfe('0x11'));var Redis=require(_0xabfe('0x12'));var authService=require(_0xabfe('0x13'));var qs=require(_0xabfe('0x14'));var hardwareService=require(_0xabfe('0x15'));var logger=require(_0xabfe('0x16'))('api');var utils=require(_0xabfe('0x17'));var config=require(_0xabfe('0x18'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4d257d,_0x3b8582){_0x3b8582=_0x3b8582||0xcc;return function(_0x42467b){if(_0x42467b){return _0x4d257d[_0xabfe('0x19')](_0x3b8582);}return _0x4d257d['status'](_0x3b8582)[_0xabfe('0x1a')]();};}function respondWithResult(_0x34f002,_0x5778c5){_0x5778c5=_0x5778c5||0xc8;return function(_0x3315bc){if(_0x3315bc){return _0x34f002[_0xabfe('0x1b')](_0x5778c5)[_0xabfe('0x1c')](_0x3315bc);}};}function respondWithFilteredResult(_0x199fab,_0xef09ca){return function(_0x252c94){if(_0x252c94){var _0x1e5ff0=_0x252c94[_0xabfe('0x1d')],_0x31d75c=_0xef09ca[_0xabfe('0x1e')],_0x1c6f34=_0xef09ca[_0xabfe('0x1e')]+_0xef09ca[_0xabfe('0x1f')],_0x22054f;if(_0x1c6f34>=_0x1e5ff0){_0x1c6f34=_0x1e5ff0;_0x22054f=0xc8;}else{_0x22054f=0xce;}_0x199fab[_0xabfe('0x1b')](_0x22054f);return _0x199fab[_0xabfe('0x20')](_0xabfe('0x21'),_0x31d75c+'-'+_0x1c6f34+'/'+_0x1e5ff0)['json'](_0x252c94);}return null;};}function patchUpdates(_0x16295b){return function(_0x11ca9e){try{jsonpatch[_0xabfe('0x22')](_0x11ca9e,_0x16295b,!![]);}catch(_0x35f3b8){return BPromise[_0xabfe('0x23')](_0x35f3b8);}return _0x11ca9e[_0xabfe('0x24')]();};}function saveUpdates(_0x3cef30,_0x5a75b0){return function(_0x1f27b5){if(_0x1f27b5){return _0x1f27b5['update'](_0x3cef30)[_0xabfe('0x25')](function(_0x18512b){return _0x18512b;});}return null;};}function removeEntity(_0x42de98,_0x1a740e){return function(_0x177979){if(_0x177979){return _0x177979[_0xabfe('0x26')]()['then'](function(){var _0x534318=_0x177979[_0xabfe('0x27')]({'plain':!![]});var _0x6875fe='Sounds';return db[_0xabfe('0x28')][_0xabfe('0x26')]({'where':{'type':_0x6875fe,'resourceId':_0x534318['id']}})[_0xabfe('0x25')](function(){return _0x177979;});})[_0xabfe('0x25')](function(){_0x42de98[_0xabfe('0x1b')](0xcc)[_0xabfe('0x1a')]();});}};}function handleEntityNotFound(_0x47542c,_0x243ea7){return function(_0x41fe93){if(!_0x41fe93){_0x47542c[_0xabfe('0x19')](0x194);}return _0x41fe93;};}function handleError(_0x146eef,_0x5c208b){_0x5c208b=_0x5c208b||0x1f4;return function(_0x1f5d00){logger[_0xabfe('0x29')](_0x1f5d00[_0xabfe('0x2a')]);if(_0x1f5d00['name']){delete _0x1f5d00[_0xabfe('0x2b')];}_0x146eef[_0xabfe('0x1b')](_0x5c208b)[_0xabfe('0x2c')](_0x1f5d00);};}exports[_0xabfe('0x2d')]=function(_0x46fcf1,_0xef0df){var _0x3c25de={},_0x2f5192={},_0x1e5585={'count':0x0,'rows':[]};var _0x53ebe6=db[_0xabfe('0x2e')][_0xabfe('0x2f')];_0x2f5192[_0xabfe('0x30')]=_[_0xabfe('0x31')](_0x53ebe6);_0x2f5192['query']=_[_0xabfe('0x31')](_0x46fcf1[_0xabfe('0x32')]);_0x2f5192['filters']=_[_0xabfe('0x33')](_0x2f5192[_0xabfe('0x30')],_0x2f5192[_0xabfe('0x32')]);_0x3c25de[_0xabfe('0x34')]=_[_0xabfe('0x33')](_0x2f5192[_0xabfe('0x30')],qs[_0xabfe('0x35')](_0x46fcf1['query'][_0xabfe('0x35')]));_0x3c25de['attributes']=_0x3c25de['attributes']['length']?_0x3c25de[_0xabfe('0x34')]:_0x2f5192[_0xabfe('0x30')];if(!_0x46fcf1[_0xabfe('0x32')][_0xabfe('0x36')]('nolimit')){_0x3c25de[_0xabfe('0x1f')]=qs['limit'](_0x46fcf1['query']['limit']);_0x3c25de['offset']=qs[_0xabfe('0x1e')](_0x46fcf1[_0xabfe('0x32')]['offset']);}_0x3c25de[_0xabfe('0x37')]=qs[_0xabfe('0x38')](_0x46fcf1[_0xabfe('0x32')]['sort']);_0x3c25de[_0xabfe('0x39')]=qs[_0xabfe('0x3a')](_[_0xabfe('0x3b')](_0x46fcf1[_0xabfe('0x32')],_0x2f5192[_0xabfe('0x3a')]));if(_0x46fcf1[_0xabfe('0x32')][_0xabfe('0x3c')]){_0x3c25de['where']=_[_0xabfe('0x3d')](_0x3c25de[_0xabfe('0x39')],{'$or':_[_0xabfe('0x3e')](_0x53ebe6,function(_0xb738f7){if(_0xb738f7['type']['key']!==_0xabfe('0x3f')){var _0x42932c={};_0x42932c[_0xb738f7['field']]={'$like':'%'+_0x46fcf1[_0xabfe('0x32')][_0xabfe('0x3c')]+'%'};return _0x42932c;}})});}_0x3c25de=_['merge']({},_0x3c25de,_0x46fcf1['options']);var _0x1d1b66={'where':_0x3c25de[_0xabfe('0x39')]};return db[_0xabfe('0x2e')][_0xabfe('0x1d')](_0x1d1b66)[_0xabfe('0x25')](function(_0x310806){_0x1e5585[_0xabfe('0x1d')]=_0x310806;if(_0x46fcf1[_0xabfe('0x32')]['includeAll']){_0x3c25de['include']=[{'all':!![]}];}return db[_0xabfe('0x2e')][_0xabfe('0x40')](_0x3c25de);})['then'](function(_0x304c71){_0x1e5585[_0xabfe('0x41')]=_0x304c71;return _0x1e5585;})[_0xabfe('0x25')](respondWithFilteredResult(_0xef0df,_0x3c25de))['catch'](handleError(_0xef0df,null));};exports['show']=function(_0x130554,_0x5c8c7f){var _0x27a0da={'raw':!![],'where':{'id':_0x130554[_0xabfe('0x42')]['id']}},_0x34aefb={};_0x34aefb[_0xabfe('0x30')]=_[_0xabfe('0x31')](db['Sound'][_0xabfe('0x2f')]);_0x34aefb[_0xabfe('0x32')]=_['keys'](_0x130554[_0xabfe('0x32')]);_0x34aefb[_0xabfe('0x3a')]=_[_0xabfe('0x33')](_0x34aefb[_0xabfe('0x30')],_0x34aefb[_0xabfe('0x32')]);_0x27a0da[_0xabfe('0x34')]=_[_0xabfe('0x33')](_0x34aefb['model'],qs[_0xabfe('0x35')](_0x130554[_0xabfe('0x32')][_0xabfe('0x35')]));_0x27a0da[_0xabfe('0x34')]=_0x27a0da[_0xabfe('0x34')][_0xabfe('0x43')]?_0x27a0da['attributes']:_0x34aefb[_0xabfe('0x30')];if(_0x130554['query']['includeAll']){_0x27a0da[_0xabfe('0x44')]=[{'all':!![]}];}_0x27a0da=_[_0xabfe('0x3d')]({},_0x27a0da,_0x130554[_0xabfe('0x45')]);return db[_0xabfe('0x2e')][_0xabfe('0x46')](_0x27a0da)[_0xabfe('0x25')](handleEntityNotFound(_0x5c8c7f,null))[_0xabfe('0x25')](respondWithResult(_0x5c8c7f,null))[_0xabfe('0x47')](handleError(_0x5c8c7f,null));};exports[_0xabfe('0x48')]=function(_0x1456c8,_0x1ba4f0,_0x27dc3e){return db['Sound'][_0xabfe('0x46')]({'where':{'id':_0x1456c8[_0xabfe('0x42')]['id']},'attributes':[_0xabfe('0x2b'),_0xabfe('0x49'),_0xabfe('0x4a')],'raw':!![]})[_0xabfe('0x25')](handleEntityNotFound(_0x1ba4f0,null))[_0xabfe('0x25')](function(_0x5b1a84){if(_0x5b1a84){if(!fs[_0xabfe('0x4b')](path[_0xabfe('0x4c')](config[_0xabfe('0x4d')],_0xabfe('0x4e'),_0x5b1a84['save_name']+'.'+_0x5b1a84[_0xabfe('0x4a')]))){throw new db['Sequelize']['ValidationError'](_0xabfe('0x4f'));}return _0x1ba4f0[_0xabfe('0x48')](path[_0xabfe('0x4c')](config[_0xabfe('0x4d')],'server/files/sounds/converted',util[_0xabfe('0x50')]('%s.%s',_0x5b1a84['save_name'],_0x5b1a84['converted_format'])),util[_0xabfe('0x50')](_0xabfe('0x51'),_0x5b1a84[_0xabfe('0x2b')],_0x5b1a84[_0xabfe('0x4a')]));}return null;})['catch'](handleError(_0x1ba4f0,null));};exports[_0xabfe('0x52')]=function(_0x4739e9,_0x44cef9,_0x4b5d27){var _0x56fffc={'name':path[_0xabfe('0x53')](_0x4739e9[_0xabfe('0x54')]['originalname'],path[_0xabfe('0x55')](_0x4739e9[_0xabfe('0x54')][_0xabfe('0x56')])),'save_name':path[_0xabfe('0x53')](_0x4739e9[_0xabfe('0x54')]['filename'],path['extname'](_0x4739e9['file'][_0xabfe('0x57')])),'original_format':path[_0xabfe('0x55')](_0x4739e9['file'][_0xabfe('0x56')])[_0xabfe('0x58')](0x1),'converted_format':_0xabfe('0x59')};var _0x9ab846=path['join'](config[_0xabfe('0x4d')],'server/files/sounds/original',_0x4739e9['file']['filename']);var _0x409114=path[_0xabfe('0x4c')](config[_0xabfe('0x4d')],_0xabfe('0x4e'),util[_0xabfe('0x50')]('%s.%s',path[_0xabfe('0x53')](_0x4739e9[_0xabfe('0x54')][_0xabfe('0x57')],path[_0xabfe('0x55')](_0x4739e9[_0xabfe('0x54')]['filename'])),_0x56fffc['converted_format']));logger[_0xabfe('0x5a')](_0xabfe('0x5b'),_0x9ab846);logger['info']('Sound\x20upload\x20destination:\x20%s',_0x409114);try{fs['chmodSync'](_0x9ab846,parseInt('0777',0x8));var _0x1d181c=sox[_0xabfe('0x5c')](_0x9ab846,_0x409114,{'sampleRate':0x1f40,'format':_0xabfe('0x59'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x1d181c['on'](_0xabfe('0x29'),function(_0x25206e){fs[_0xabfe('0x5d')](_0x9ab846);_0x44cef9[_0xabfe('0x1b')](0x1f4)[_0xabfe('0x2c')](_0x25206e);});_0x1d181c['on'](_0xabfe('0x5e'),function(_0x35a5d1){_0x56fffc[_0xabfe('0x5f')]=isNaN(_0x35a5d1['duration'])?null:_0x35a5d1[_0xabfe('0x60')]*0x3e8;_0x56fffc[_0xabfe('0x61')]=isNaN(_0x35a5d1['sampleCount'])?null:_0x35a5d1[_0xabfe('0x62')];_0x56fffc['original_channelCount']=isNaN(_0x35a5d1[_0xabfe('0x63')])?null:_0x35a5d1['channelCount'];_0x56fffc[_0xabfe('0x64')]=isNaN(_0x35a5d1['bitRate'])?null:_0x35a5d1['bitRate'];_0x56fffc['original_sampleRate']=isNaN(_0x35a5d1[_0xabfe('0x65')])?null:_0x35a5d1[_0xabfe('0x65')];});_0x1d181c['on']('dest',function(_0x3b0a58){_0x56fffc[_0xabfe('0x66')]=isNaN(_0x3b0a58['duration'])?null:_0x3b0a58[_0xabfe('0x60')]*0x3e8;_0x56fffc[_0xabfe('0x67')]=isNaN(_0x3b0a58[_0xabfe('0x62')])?null:_0x3b0a58[_0xabfe('0x62')];_0x56fffc[_0xabfe('0x68')]=isNaN(_0x3b0a58['channelCount'])?null:_0x3b0a58['channelCount'];_0x56fffc[_0xabfe('0x69')]=isNaN(_0x3b0a58[_0xabfe('0x6a')])?null:_0x3b0a58[_0xabfe('0x6a')];_0x56fffc['converted_sampleRate']=isNaN(_0x3b0a58['sampleRate'])?null:_0x3b0a58['sampleRate'];});_0x1d181c['on'](_0xabfe('0x1a'),function(){fs[_0xabfe('0x6b')](_0x409114,parseInt('0777',0x8));return db[_0xabfe('0x2e')]['create'](_0x56fffc)[_0xabfe('0x25')](function(_0x57e203){var _0x324c58={'role':_0x4739e9['body']['role'],'userProfileId':_0x4739e9[_0xabfe('0x6c')][_0xabfe('0x6d')]};if(!_0x324c58)throw new Error(_0xabfe('0x6e'));if(_0x324c58[_0xabfe('0x6f')]==='user'){var _0x1abcc5=_0x57e203['get']({'plain':!![]});return db[_0xabfe('0x70')][_0xabfe('0x46')]({'where':{'name':_0xabfe('0x71'),'userProfileId':_0x324c58[_0xabfe('0x6d')]},'raw':!![]})[_0xabfe('0x25')](function(_0x14fc9e){if(_0x14fc9e&&_0x14fc9e[_0xabfe('0x72')]===0x0){return db[_0xabfe('0x28')][_0xabfe('0x52')]({'name':_0x1abcc5['name'],'resourceId':_0x1abcc5['id'],'type':_0x14fc9e[_0xabfe('0x2b')],'sectionId':_0x14fc9e['id']},{})['then'](function(){return _0x57e203;});}else{return _0x57e203;}})[_0xabfe('0x47')](function(_0x2826ff){logger[_0xabfe('0x29')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0x2826ff);throw _0x2826ff;});}return _0x57e203;})[_0xabfe('0x25')](respondWithResult(_0x44cef9,null))[_0xabfe('0x47')](handleError(_0x44cef9,null));});_0x1d181c[_0xabfe('0x73')]();}catch(_0x380bf2){_0x44cef9['status'](0x1f4)[_0xabfe('0x2c')](_0x380bf2);}};exports[_0xabfe('0x74')]=function(_0x240dc4,_0x2c944c,_0x1cb5f9){return db[_0xabfe('0x2e')]['find']({'where':{'id':_0x240dc4[_0xabfe('0x42')]['id']}})['then'](handleEntityNotFound(_0x2c944c,null))[_0xabfe('0x25')](saveUpdates(_[_0xabfe('0x3b')](_0x240dc4[_0xabfe('0x6c')],[_0xabfe('0x2b'),'description']),null))[_0xabfe('0x25')](respondWithResult(_0x2c944c,null))[_0xabfe('0x47')](handleError(_0x2c944c,null));};exports['delete']=function(_0x13db87,_0x3f9a31,_0x93253a){return db[_0xabfe('0x2e')][_0xabfe('0x46')]({'where':{'id':_0x13db87[_0xabfe('0x42')]['id']}})['then'](function(_0x21759b){if(_0x21759b){return _0x21759b[_0xabfe('0x26')]();}})[_0xabfe('0x25')](function(_0x2ac509){var _0x4d8277=path[_0xabfe('0x4c')](config[_0xabfe('0x4d')],_0xabfe('0x75'),util['format'](_0xabfe('0x51'),_0x2ac509['save_name'],_0x2ac509[_0xabfe('0x76')]));var _0xd97768=path[_0xabfe('0x4c')](config[_0xabfe('0x4d')],_0xabfe('0x4e'),util[_0xabfe('0x50')]('%s.%s',_0x2ac509[_0xabfe('0x49')],_0x2ac509['converted_format']));fs[_0xabfe('0x5d')](_0x4d8277);fs[_0xabfe('0x5d')](_0xd97768);return _0x2ac509;})[_0xabfe('0x25')](function(_0x16573f){if(_0x16573f){return _0x3f9a31[_0xabfe('0x1b')](0xcc)[_0xabfe('0x1a')]();}})[_0xabfe('0x25')](handleEntityNotFound(_0x3f9a31,null))[_0xabfe('0x47')](handleError(_0x3f9a31,null));};