af1bddabbecfbad22002d34470d632a04b2bf4b6
[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 _0x923f=['../../config/license/util','../../mysqldb','end','json','count','offset','status','set','reject','save','update','then','get','Sounds','UserProfileResource','destroy','sendStatus','stack','name','send','index','map','Sound','rawAttributes','fieldName','type','key','model','keys','query','filters','intersection','fields','attributes','length','hasOwnProperty','nolimit','limit','sort','where','filter','VIRTUAL','merge','options','include','findAll','rows','show','params','includeAll','find','catch','existsSync','join','server/files/sounds/converted','save_name','converted_format','Sequelize','ValidationError','Sound\x20not\x20found','download','create','basename','originalname','file','filename','extname','substring','wav','root','server/files/sounds/original','format','%s.%s','Sound\x20upload\x20destination:\x20%s','0777','error','unlinkSync','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_sampleCount','converted_channelCount','converted_bitRate','converted_sampleRate','chmodSync','body','role','userProfileId','user','UserProfileSection','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','pick','description','delete','original_format','html-pdf','eml-format','zip-dir','fast-json-patch','moment','bluebird','mustache','util','sox','to-csv','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment'];(function(_0x2f3d29,_0x150e7b){var _0x53d69d=function(_0x45f6ec){while(--_0x45f6ec){_0x2f3d29['push'](_0x2f3d29['shift']());}};_0x53d69d(++_0x150e7b);}(_0x923f,0x1e2));var _0xf923=function(_0x3cabf5,_0x2c7b17){_0x3cabf5=_0x3cabf5-0x0;var _0x57bf69=_0x923f[_0x3cabf5];return _0x57bf69;};'use strict';var pdf=require(_0xf923('0x0'));var emlformat=require(_0xf923('0x1'));var rimraf=require('rimraf');var zipdir=require(_0xf923('0x2'));var jsonpatch=require(_0xf923('0x3'));var rp=require('request-promise');var moment=require(_0xf923('0x4'));var BPromise=require(_0xf923('0x5'));var Mustache=require(_0xf923('0x6'));var util=require(_0xf923('0x7'));var path=require('path');var sox=require(_0xf923('0x8'));var csv=require(_0xf923('0x9'));var ejs=require('ejs');var fs=require('fs');var _=require('lodash');var squel=require(_0xf923('0xa'));var crypto=require(_0xf923('0xb'));var jsforce=require(_0xf923('0xc'));var deskjs=require(_0xf923('0xd'));var toCsv=require(_0xf923('0x9'));var querystring=require(_0xf923('0xe'));var Papa=require('papaparse');var Redis=require(_0xf923('0xf'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0xf923('0x10'));var hardwareService=require(_0xf923('0x11'));var logger=require(_0xf923('0x12'))(_0xf923('0x13'));var utils=require(_0xf923('0x14'));var config=require(_0xf923('0x15'));var licenseUtil=require(_0xf923('0x16'));var db=require(_0xf923('0x17'))['db'];function respondWithStatusCode(_0x1bf81d,_0x35d43c){_0x35d43c=_0x35d43c||0xcc;return function(_0x2c9209){if(_0x2c9209){return _0x1bf81d['sendStatus'](_0x35d43c);}return _0x1bf81d['status'](_0x35d43c)[_0xf923('0x18')]();};}function respondWithResult(_0x18455e,_0x3d89fc){_0x3d89fc=_0x3d89fc||0xc8;return function(_0x5eaadd){if(_0x5eaadd){return _0x18455e['status'](_0x3d89fc)[_0xf923('0x19')](_0x5eaadd);}};}function respondWithFilteredResult(_0x439042,_0x4921ae){return function(_0x35a99c){if(_0x35a99c){var _0x3e6e70=_0x35a99c[_0xf923('0x1a')],_0x31e085=_0x4921ae['offset'],_0x418705=_0x4921ae[_0xf923('0x1b')]+_0x4921ae['limit'],_0x40ab71;if(_0x418705>=_0x3e6e70){_0x418705=_0x3e6e70;_0x40ab71=0xc8;}else{_0x40ab71=0xce;}_0x439042[_0xf923('0x1c')](_0x40ab71);return _0x439042[_0xf923('0x1d')]('Content-Range',_0x31e085+'-'+_0x418705+'/'+_0x3e6e70)[_0xf923('0x19')](_0x35a99c);}return null;};}function patchUpdates(_0x1dc3d5){return function(_0x4b6eb6){try{jsonpatch['apply'](_0x4b6eb6,_0x1dc3d5,!![]);}catch(_0x4e3fbd){return BPromise[_0xf923('0x1e')](_0x4e3fbd);}return _0x4b6eb6[_0xf923('0x1f')]();};}function saveUpdates(_0x1ca440,_0x448545){return function(_0x572284){if(_0x572284){return _0x572284[_0xf923('0x20')](_0x1ca440)[_0xf923('0x21')](function(_0x10d3f5){return _0x10d3f5;});}return null;};}function removeEntity(_0x4092ee,_0x5609cf){return function(_0x329d12){if(_0x329d12){return _0x329d12['destroy']()[_0xf923('0x21')](function(){var _0x164641=_0x329d12[_0xf923('0x22')]({'plain':!![]});var _0x4f536b=_0xf923('0x23');return db[_0xf923('0x24')][_0xf923('0x25')]({'where':{'type':_0x4f536b,'resourceId':_0x164641['id']}})['then'](function(){return _0x329d12;});})[_0xf923('0x21')](function(){_0x4092ee[_0xf923('0x1c')](0xcc)[_0xf923('0x18')]();});}};}function handleEntityNotFound(_0xbf9f8f,_0x421bf6){return function(_0x1841d6){if(!_0x1841d6){_0xbf9f8f[_0xf923('0x26')](0x194);}return _0x1841d6;};}function handleError(_0x3a55de,_0x3a6084){_0x3a6084=_0x3a6084||0x1f4;return function(_0x45d1ab){logger['error'](_0x45d1ab[_0xf923('0x27')]);if(_0x45d1ab[_0xf923('0x28')]){delete _0x45d1ab[_0xf923('0x28')];}_0x3a55de['status'](_0x3a6084)[_0xf923('0x29')](_0x45d1ab);};}exports[_0xf923('0x2a')]=function(_0x528092,_0x48f106){var _0xb1af63={},_0xd6f236={},_0xac7320={'count':0x0,'rows':[]};var _0x2889aa=_[_0xf923('0x2b')](db[_0xf923('0x2c')][_0xf923('0x2d')],function(_0x5bf0d5){return{'name':_0x5bf0d5[_0xf923('0x2e')],'type':_0x5bf0d5[_0xf923('0x2f')][_0xf923('0x30')]};});_0xd6f236[_0xf923('0x31')]=_[_0xf923('0x2b')](_0x2889aa,'name');_0xd6f236['query']=_[_0xf923('0x32')](_0x528092[_0xf923('0x33')]);_0xd6f236[_0xf923('0x34')]=_[_0xf923('0x35')](_0xd6f236[_0xf923('0x31')],_0xd6f236[_0xf923('0x33')]);_0xb1af63['attributes']=_['intersection'](_0xd6f236[_0xf923('0x31')],qs['fields'](_0x528092['query'][_0xf923('0x36')]));_0xb1af63[_0xf923('0x37')]=_0xb1af63[_0xf923('0x37')][_0xf923('0x38')]?_0xb1af63[_0xf923('0x37')]:_0xd6f236['model'];if(!_0x528092['query'][_0xf923('0x39')](_0xf923('0x3a'))){_0xb1af63[_0xf923('0x3b')]=qs[_0xf923('0x3b')](_0x528092[_0xf923('0x33')][_0xf923('0x3b')]);_0xb1af63['offset']=qs[_0xf923('0x1b')](_0x528092[_0xf923('0x33')][_0xf923('0x1b')]);}_0xb1af63['order']=qs['sort'](_0x528092[_0xf923('0x33')][_0xf923('0x3c')]);_0xb1af63[_0xf923('0x3d')]=qs[_0xf923('0x34')](_['pick'](_0x528092[_0xf923('0x33')],_0xd6f236[_0xf923('0x34')]),_0x2889aa);if(_0x528092[_0xf923('0x33')][_0xf923('0x3e')]){_0xb1af63[_0xf923('0x3d')]=_['merge'](_0xb1af63[_0xf923('0x3d')],{'$or':_[_0xf923('0x2b')](_0x2889aa,function(_0x1aee02){if(_0x1aee02[_0xf923('0x2f')]!==_0xf923('0x3f')){var _0x5444c3={};_0x5444c3[_0x1aee02['name']]={'$like':'%'+_0x528092['query'][_0xf923('0x3e')]+'%'};return _0x5444c3;}})});}_0xb1af63=_[_0xf923('0x40')]({},_0xb1af63,_0x528092[_0xf923('0x41')]);var _0x3726be={'where':_0xb1af63['where']};return db[_0xf923('0x2c')]['count'](_0x3726be)[_0xf923('0x21')](function(_0x294ce6){_0xac7320['count']=_0x294ce6;if(_0x528092[_0xf923('0x33')]['includeAll']){_0xb1af63[_0xf923('0x42')]=[{'all':!![]}];}return db[_0xf923('0x2c')][_0xf923('0x43')](_0xb1af63);})[_0xf923('0x21')](function(_0x217c6d){_0xac7320[_0xf923('0x44')]=_0x217c6d;return _0xac7320;})[_0xf923('0x21')](respondWithFilteredResult(_0x48f106,_0xb1af63))['catch'](handleError(_0x48f106,null));};exports[_0xf923('0x45')]=function(_0x1145a7,_0x4a459e){var _0x183e69={'raw':!![],'where':{'id':_0x1145a7[_0xf923('0x46')]['id']}},_0x319e8d={};_0x319e8d['model']=_[_0xf923('0x32')](db[_0xf923('0x2c')][_0xf923('0x2d')]);_0x319e8d[_0xf923('0x33')]=_[_0xf923('0x32')](_0x1145a7[_0xf923('0x33')]);_0x319e8d[_0xf923('0x34')]=_[_0xf923('0x35')](_0x319e8d[_0xf923('0x31')],_0x319e8d['query']);_0x183e69['attributes']=_[_0xf923('0x35')](_0x319e8d[_0xf923('0x31')],qs[_0xf923('0x36')](_0x1145a7[_0xf923('0x33')][_0xf923('0x36')]));_0x183e69[_0xf923('0x37')]=_0x183e69['attributes'][_0xf923('0x38')]?_0x183e69['attributes']:_0x319e8d['model'];if(_0x1145a7[_0xf923('0x33')][_0xf923('0x47')]){_0x183e69[_0xf923('0x42')]=[{'all':!![]}];}_0x183e69=_[_0xf923('0x40')]({},_0x183e69,_0x1145a7[_0xf923('0x41')]);return db[_0xf923('0x2c')][_0xf923('0x48')](_0x183e69)[_0xf923('0x21')](handleEntityNotFound(_0x4a459e,null))[_0xf923('0x21')](respondWithResult(_0x4a459e,null))[_0xf923('0x49')](handleError(_0x4a459e,null));};exports['download']=function(_0x455e2b,_0x417bc3,_0x39b5fe){return db[_0xf923('0x2c')][_0xf923('0x48')]({'where':{'id':_0x455e2b[_0xf923('0x46')]['id']},'attributes':[_0xf923('0x28'),'save_name','converted_format'],'raw':!![]})[_0xf923('0x21')](handleEntityNotFound(_0x417bc3,null))[_0xf923('0x21')](function(_0x282193){if(_0x282193){if(!fs[_0xf923('0x4a')](path[_0xf923('0x4b')](config['root'],_0xf923('0x4c'),_0x282193[_0xf923('0x4d')]+'.'+_0x282193[_0xf923('0x4e')]))){throw new db[(_0xf923('0x4f'))][(_0xf923('0x50'))](_0xf923('0x51'));}return _0x417bc3[_0xf923('0x52')](path[_0xf923('0x4b')](config['root'],_0xf923('0x4c'),util['format']('%s.%s',_0x282193[_0xf923('0x4d')],_0x282193['converted_format'])),util['format']('%s.%s',_0x282193['name'],_0x282193['converted_format']));}return null;})[_0xf923('0x49')](handleError(_0x417bc3,null));};exports[_0xf923('0x53')]=function(_0x42db98,_0x5a6493,_0x158413){var _0x283a5e={'name':path[_0xf923('0x54')](_0x42db98['file'][_0xf923('0x55')],path['extname'](_0x42db98[_0xf923('0x56')]['originalname'])),'save_name':path['basename'](_0x42db98[_0xf923('0x56')][_0xf923('0x57')],path['extname'](_0x42db98[_0xf923('0x56')]['filename'])),'original_format':path[_0xf923('0x58')](_0x42db98[_0xf923('0x56')][_0xf923('0x55')])[_0xf923('0x59')](0x1),'converted_format':_0xf923('0x5a')};var _0x214fc7=path[_0xf923('0x4b')](config[_0xf923('0x5b')],_0xf923('0x5c'),_0x42db98[_0xf923('0x56')][_0xf923('0x57')]);var _0x1eb5d8=path[_0xf923('0x4b')](config[_0xf923('0x5b')],'server/files/sounds/converted',util[_0xf923('0x5d')](_0xf923('0x5e'),path[_0xf923('0x54')](_0x42db98[_0xf923('0x56')][_0xf923('0x57')],path[_0xf923('0x58')](_0x42db98[_0xf923('0x56')]['filename'])),_0x283a5e[_0xf923('0x4e')]));logger['info']('Sound\x20upload\x20filepath:\x20%s',_0x214fc7);logger['info'](_0xf923('0x5f'),_0x1eb5d8);try{fs['chmodSync'](_0x214fc7,parseInt(_0xf923('0x60'),0x8));var _0x847488=sox['transcode'](_0x214fc7,_0x1eb5d8,{'sampleRate':0x1f40,'format':_0xf923('0x5a'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x847488['on'](_0xf923('0x61'),function(_0x2f37ed){fs[_0xf923('0x62')](_0x214fc7);_0x5a6493['status'](0x1f4)['send'](_0x2f37ed);});_0x847488['on']('src',function(_0x2bf3fe){_0x283a5e['original_duration']=isNaN(_0x2bf3fe[_0xf923('0x63')])?null:_0x2bf3fe[_0xf923('0x63')]*0x3e8;_0x283a5e[_0xf923('0x64')]=isNaN(_0x2bf3fe[_0xf923('0x65')])?null:_0x2bf3fe[_0xf923('0x65')];_0x283a5e[_0xf923('0x66')]=isNaN(_0x2bf3fe[_0xf923('0x67')])?null:_0x2bf3fe[_0xf923('0x67')];_0x283a5e[_0xf923('0x68')]=isNaN(_0x2bf3fe[_0xf923('0x69')])?null:_0x2bf3fe['bitRate'];_0x283a5e[_0xf923('0x6a')]=isNaN(_0x2bf3fe[_0xf923('0x6b')])?null:_0x2bf3fe[_0xf923('0x6b')];});_0x847488['on'](_0xf923('0x6c'),function(_0x249353){_0x283a5e[_0xf923('0x6d')]=isNaN(_0x249353['duration'])?null:_0x249353[_0xf923('0x63')]*0x3e8;_0x283a5e[_0xf923('0x6e')]=isNaN(_0x249353['sampleCount'])?null:_0x249353[_0xf923('0x65')];_0x283a5e[_0xf923('0x6f')]=isNaN(_0x249353['channelCount'])?null:_0x249353[_0xf923('0x67')];_0x283a5e[_0xf923('0x70')]=isNaN(_0x249353['bitRate'])?null:_0x249353[_0xf923('0x69')];_0x283a5e[_0xf923('0x71')]=isNaN(_0x249353[_0xf923('0x6b')])?null:_0x249353[_0xf923('0x6b')];});_0x847488['on'](_0xf923('0x18'),function(){fs[_0xf923('0x72')](_0x1eb5d8,parseInt(_0xf923('0x60'),0x8));return db[_0xf923('0x2c')][_0xf923('0x53')](_0x283a5e)[_0xf923('0x21')](function(_0x2e1a4d){var _0x26c2ac={'role':_0x42db98[_0xf923('0x73')][_0xf923('0x74')],'userProfileId':_0x42db98[_0xf923('0x73')][_0xf923('0x75')]};if(!_0x26c2ac)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x26c2ac[_0xf923('0x74')]===_0xf923('0x76')){var _0x3791d4=_0x2e1a4d[_0xf923('0x22')]({'plain':!![]});return db[_0xf923('0x77')][_0xf923('0x48')]({'where':{'name':'Sounds','userProfileId':_0x26c2ac[_0xf923('0x75')]},'raw':!![]})[_0xf923('0x21')](function(_0x3abed1){if(_0x3abed1&&_0x3abed1['autoAssociation']===0x0){return db['UserProfileResource']['create']({'name':_0x3791d4[_0xf923('0x28')],'resourceId':_0x3791d4['id'],'type':_0x3abed1['name'],'sectionId':_0x3abed1['id']},{})[_0xf923('0x21')](function(){return _0x2e1a4d;});}else{return _0x2e1a4d;}})[_0xf923('0x49')](function(_0x2361a0){logger[_0xf923('0x61')](_0xf923('0x78'),_0x2361a0);throw _0x2361a0;});}return _0x2e1a4d;})[_0xf923('0x21')](respondWithResult(_0x5a6493,null))[_0xf923('0x49')](handleError(_0x5a6493,null));});_0x847488[_0xf923('0x79')]();}catch(_0x259d3b){_0x5a6493[_0xf923('0x1c')](0x1f4)['send'](_0x259d3b);}};exports[_0xf923('0x20')]=function(_0x1221cf,_0x3d3b51,_0x437724){return db[_0xf923('0x2c')][_0xf923('0x48')]({'where':{'id':_0x1221cf['params']['id']}})[_0xf923('0x21')](handleEntityNotFound(_0x3d3b51,null))[_0xf923('0x21')](saveUpdates(_[_0xf923('0x7a')](_0x1221cf['body'],[_0xf923('0x28'),_0xf923('0x7b')]),null))[_0xf923('0x21')](respondWithResult(_0x3d3b51,null))[_0xf923('0x49')](handleError(_0x3d3b51,null));};exports[_0xf923('0x7c')]=function(_0x147c3d,_0x32baf1,_0x24db86){return db[_0xf923('0x2c')]['find']({'where':{'id':_0x147c3d[_0xf923('0x46')]['id']}})['then'](function(_0x43e889){if(_0x43e889){return _0x43e889[_0xf923('0x25')]();}})[_0xf923('0x21')](function(_0x47a127){var _0x22f9ec=path[_0xf923('0x4b')](config[_0xf923('0x5b')],_0xf923('0x5c'),util['format'](_0xf923('0x5e'),_0x47a127[_0xf923('0x4d')],_0x47a127[_0xf923('0x7d')]));var _0x116a08=path[_0xf923('0x4b')](config[_0xf923('0x5b')],_0xf923('0x4c'),util['format'](_0xf923('0x5e'),_0x47a127[_0xf923('0x4d')],_0x47a127[_0xf923('0x4e')]));fs[_0xf923('0x62')](_0x22f9ec);fs[_0xf923('0x62')](_0x116a08);return _0x47a127;})[_0xf923('0x21')](function(_0x29f03d){if(_0x29f03d){return _0x32baf1[_0xf923('0x1c')](0xcc)[_0xf923('0x18')]();}})['then'](handleEntityNotFound(_0x32baf1,null))[_0xf923('0x49')](handleError(_0x32baf1,null));};