bdd520c0465964aa47a4b81b820fef7a593ee4d8
[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 _0x9b84=['download','format','%s.%s','create','basename','file','originalname','extname','filename','wav','join','server/files/sounds/original','info','Sound\x20upload\x20filepath:\x20%s','chmodSync','0777','transcode','src','original_duration','duration','sampleCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','autoAssociation','UserProfileResource','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','update','delete','original_format','unlinkSync','rimraf','zip-dir','fast-json-patch','request-promise','bluebird','mustache','util','path','sox','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','to-csv','querystring','ioredis','../../components/auth/service','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','end','json','offset','undefined','count','limit','Content-Range','apply','reject','then','get','Sounds','destroy','error','stack','name','send','index','map','Sound','rawAttributes','fieldName','type','key','model','query','keys','filters','attributes','intersection','fields','length','nolimit','order','sort','where','pick','filter','merge','options','includeAll','include','rows','catch','show','params','find','save_name','existsSync','root','server/files/sounds/converted','converted_format','Sequelize','ValidationError'];(function(_0x106c39,_0x4bf3be){var _0x27a1c0=function(_0x2ae354){while(--_0x2ae354){_0x106c39['push'](_0x106c39['shift']());}};_0x27a1c0(++_0x4bf3be);}(_0x9b84,0x1a8));var _0x49b8=function(_0x2e6c19,_0x131233){_0x2e6c19=_0x2e6c19-0x0;var _0x52885d=_0x9b84[_0x2e6c19];return _0x52885d;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0x49b8('0x0'));var zipdir=require(_0x49b8('0x1'));var jsonpatch=require(_0x49b8('0x2'));var rp=require(_0x49b8('0x3'));var moment=require('moment');var BPromise=require(_0x49b8('0x4'));var Mustache=require(_0x49b8('0x5'));var util=require(_0x49b8('0x6'));var path=require(_0x49b8('0x7'));var sox=require(_0x49b8('0x8'));var csv=require('to-csv');var ejs=require(_0x49b8('0x9'));var fs=require('fs');var fs_extra=require(_0x49b8('0xa'));var _=require(_0x49b8('0xb'));var squel=require(_0x49b8('0xc'));var crypto=require(_0x49b8('0xd'));var jsforce=require(_0x49b8('0xe'));var deskjs=require(_0x49b8('0xf'));var toCsv=require(_0x49b8('0x10'));var querystring=require(_0x49b8('0x11'));var Papa=require('papaparse');var Redis=require(_0x49b8('0x12'));var authService=require(_0x49b8('0x13'));var qs=require('../../components/parsers/qs');var as=require(_0x49b8('0x14'));var hardwareService=require(_0x49b8('0x15'));var logger=require(_0x49b8('0x16'))(_0x49b8('0x17'));var utils=require(_0x49b8('0x18'));var config=require(_0x49b8('0x19'));var licenseUtil=require(_0x49b8('0x1a'));var db=require(_0x49b8('0x1b'))['db'];function respondWithStatusCode(_0x20ea15,_0x4c82c9){_0x4c82c9=_0x4c82c9||0xcc;return function(_0x2b31b0){if(_0x2b31b0){return _0x20ea15[_0x49b8('0x1c')](_0x4c82c9);}return _0x20ea15[_0x49b8('0x1d')](_0x4c82c9)[_0x49b8('0x1e')]();};}function respondWithResult(_0x13832b,_0x3a22be){_0x3a22be=_0x3a22be||0xc8;return function(_0x1cf279){if(_0x1cf279){return _0x13832b['status'](_0x3a22be)[_0x49b8('0x1f')](_0x1cf279);}};}function respondWithFilteredResult(_0x35445b,_0x4a1a50){return function(_0xe04b15){if(_0xe04b15){var _0x1f42bd=typeof _0x4a1a50[_0x49b8('0x20')]===_0x49b8('0x21')&&typeof _0x4a1a50['limit']===_0x49b8('0x21');var _0x28b3be=_0xe04b15[_0x49b8('0x22')];var _0x512ff1=_0x1f42bd?0x0:_0x4a1a50['offset'];var _0x3ab4e5=_0x1f42bd?_0xe04b15[_0x49b8('0x22')]:_0x4a1a50['offset']+_0x4a1a50[_0x49b8('0x23')];var _0x36fec6;if(_0x3ab4e5>=_0x28b3be){_0x3ab4e5=_0x28b3be;_0x36fec6=0xc8;}else{_0x36fec6=0xce;}_0x35445b[_0x49b8('0x1d')](_0x36fec6);return _0x35445b['set'](_0x49b8('0x24'),_0x512ff1+'-'+_0x3ab4e5+'/'+_0x28b3be)['json'](_0xe04b15);}return null;};}function patchUpdates(_0x9f93d0){return function(_0x11f9b1){try{jsonpatch[_0x49b8('0x25')](_0x11f9b1,_0x9f93d0,!![]);}catch(_0x1f8c91){return BPromise[_0x49b8('0x26')](_0x1f8c91);}return _0x11f9b1['save']();};}function saveUpdates(_0x536a05,_0x365102){return function(_0x57ef8d){if(_0x57ef8d){return _0x57ef8d['update'](_0x536a05)['then'](function(_0x2ab328){return _0x2ab328;});}return null;};}function removeEntity(_0x55436a,_0x408fc5){return function(_0x53702e){if(_0x53702e){return _0x53702e['destroy']()[_0x49b8('0x27')](function(){var _0x36f834=_0x53702e[_0x49b8('0x28')]({'plain':!![]});var _0x56afee=_0x49b8('0x29');return db['UserProfileResource'][_0x49b8('0x2a')]({'where':{'type':_0x56afee,'resourceId':_0x36f834['id']}})[_0x49b8('0x27')](function(){return _0x53702e;});})[_0x49b8('0x27')](function(){_0x55436a[_0x49b8('0x1d')](0xcc)[_0x49b8('0x1e')]();});}};}function handleEntityNotFound(_0x470c3e,_0x55e0d9){return function(_0x3be824){if(!_0x3be824){_0x470c3e[_0x49b8('0x1c')](0x194);}return _0x3be824;};}function handleError(_0x53bcf4,_0x35a2aa){_0x35a2aa=_0x35a2aa||0x1f4;return function(_0x4b53fa){logger[_0x49b8('0x2b')](_0x4b53fa[_0x49b8('0x2c')]);if(_0x4b53fa['name']){delete _0x4b53fa[_0x49b8('0x2d')];}_0x53bcf4[_0x49b8('0x1d')](_0x35a2aa)[_0x49b8('0x2e')](_0x4b53fa);};}exports[_0x49b8('0x2f')]=function(_0x28d1ec,_0x105bc5){var _0xb50c9f={},_0x5d9851={},_0x464b60={'count':0x0,'rows':[]};var _0x58d098=_[_0x49b8('0x30')](db[_0x49b8('0x31')][_0x49b8('0x32')],function(_0x1d5e22){return{'name':_0x1d5e22[_0x49b8('0x33')],'type':_0x1d5e22[_0x49b8('0x34')][_0x49b8('0x35')]};});_0x5d9851[_0x49b8('0x36')]=_[_0x49b8('0x30')](_0x58d098,_0x49b8('0x2d'));_0x5d9851[_0x49b8('0x37')]=_[_0x49b8('0x38')](_0x28d1ec[_0x49b8('0x37')]);_0x5d9851[_0x49b8('0x39')]=_['intersection'](_0x5d9851[_0x49b8('0x36')],_0x5d9851[_0x49b8('0x37')]);_0xb50c9f[_0x49b8('0x3a')]=_[_0x49b8('0x3b')](_0x5d9851[_0x49b8('0x36')],qs[_0x49b8('0x3c')](_0x28d1ec[_0x49b8('0x37')][_0x49b8('0x3c')]));_0xb50c9f[_0x49b8('0x3a')]=_0xb50c9f[_0x49b8('0x3a')][_0x49b8('0x3d')]?_0xb50c9f[_0x49b8('0x3a')]:_0x5d9851[_0x49b8('0x36')];if(!_0x28d1ec['query']['hasOwnProperty'](_0x49b8('0x3e'))){_0xb50c9f[_0x49b8('0x23')]=qs[_0x49b8('0x23')](_0x28d1ec[_0x49b8('0x37')][_0x49b8('0x23')]);_0xb50c9f['offset']=qs[_0x49b8('0x20')](_0x28d1ec['query'][_0x49b8('0x20')]);}_0xb50c9f[_0x49b8('0x3f')]=qs[_0x49b8('0x40')](_0x28d1ec[_0x49b8('0x37')]['sort']);_0xb50c9f[_0x49b8('0x41')]=qs[_0x49b8('0x39')](_[_0x49b8('0x42')](_0x28d1ec['query'],_0x5d9851[_0x49b8('0x39')]),_0x58d098);if(_0x28d1ec[_0x49b8('0x37')][_0x49b8('0x43')]){_0xb50c9f[_0x49b8('0x41')]=_[_0x49b8('0x44')](_0xb50c9f['where'],{'$or':_['map'](_0x58d098,function(_0x2e5324){if(_0x2e5324[_0x49b8('0x34')]!=='VIRTUAL'){var _0x21d582={};_0x21d582[_0x2e5324[_0x49b8('0x2d')]]={'$like':'%'+_0x28d1ec[_0x49b8('0x37')]['filter']+'%'};return _0x21d582;}})});}_0xb50c9f=_[_0x49b8('0x44')]({},_0xb50c9f,_0x28d1ec[_0x49b8('0x45')]);var _0x51b9a4={'where':_0xb50c9f['where']};return db[_0x49b8('0x31')][_0x49b8('0x22')](_0x51b9a4)['then'](function(_0x50030a){_0x464b60[_0x49b8('0x22')]=_0x50030a;if(_0x28d1ec[_0x49b8('0x37')][_0x49b8('0x46')]){_0xb50c9f[_0x49b8('0x47')]=[{'all':!![]}];}return db[_0x49b8('0x31')]['findAll'](_0xb50c9f);})[_0x49b8('0x27')](function(_0x20c0fd){_0x464b60[_0x49b8('0x48')]=_0x20c0fd;return _0x464b60;})['then'](respondWithFilteredResult(_0x105bc5,_0xb50c9f))[_0x49b8('0x49')](handleError(_0x105bc5,null));};exports[_0x49b8('0x4a')]=function(_0x4c13ec,_0x3d5578){var _0x3e07a9={'raw':!![],'where':{'id':_0x4c13ec[_0x49b8('0x4b')]['id']}},_0x55ce96={};_0x55ce96[_0x49b8('0x36')]=_[_0x49b8('0x38')](db[_0x49b8('0x31')][_0x49b8('0x32')]);_0x55ce96['query']=_[_0x49b8('0x38')](_0x4c13ec['query']);_0x55ce96['filters']=_[_0x49b8('0x3b')](_0x55ce96[_0x49b8('0x36')],_0x55ce96[_0x49b8('0x37')]);_0x3e07a9[_0x49b8('0x3a')]=_[_0x49b8('0x3b')](_0x55ce96[_0x49b8('0x36')],qs['fields'](_0x4c13ec[_0x49b8('0x37')][_0x49b8('0x3c')]));_0x3e07a9[_0x49b8('0x3a')]=_0x3e07a9[_0x49b8('0x3a')][_0x49b8('0x3d')]?_0x3e07a9[_0x49b8('0x3a')]:_0x55ce96['model'];if(_0x4c13ec[_0x49b8('0x37')][_0x49b8('0x46')]){_0x3e07a9[_0x49b8('0x47')]=[{'all':!![]}];}_0x3e07a9=_[_0x49b8('0x44')]({},_0x3e07a9,_0x4c13ec[_0x49b8('0x45')]);return db['Sound'][_0x49b8('0x4c')](_0x3e07a9)[_0x49b8('0x27')](handleEntityNotFound(_0x3d5578,null))[_0x49b8('0x27')](respondWithResult(_0x3d5578,null))[_0x49b8('0x49')](handleError(_0x3d5578,null));};exports['download']=function(_0x4bd25d,_0x814f17,_0x1d3bb9){return db['Sound'][_0x49b8('0x4c')]({'where':{'id':_0x4bd25d[_0x49b8('0x4b')]['id']},'attributes':['name',_0x49b8('0x4d'),'converted_format'],'raw':!![]})[_0x49b8('0x27')](handleEntityNotFound(_0x814f17,null))[_0x49b8('0x27')](function(_0x192429){if(_0x192429){if(!fs[_0x49b8('0x4e')](path['join'](config[_0x49b8('0x4f')],_0x49b8('0x50'),_0x192429['save_name']+'.'+_0x192429[_0x49b8('0x51')]))){throw new db[(_0x49b8('0x52'))][(_0x49b8('0x53'))]('Sound\x20not\x20found');}return _0x814f17[_0x49b8('0x54')](path['join'](config[_0x49b8('0x4f')],'server/files/sounds/converted',util[_0x49b8('0x55')](_0x49b8('0x56'),_0x192429[_0x49b8('0x4d')],_0x192429[_0x49b8('0x51')])),util[_0x49b8('0x55')](_0x49b8('0x56'),_0x192429[_0x49b8('0x2d')],_0x192429['converted_format']));}return null;})[_0x49b8('0x49')](handleError(_0x814f17,null));};exports[_0x49b8('0x57')]=function(_0x23ecc2,_0x5acd90,_0x80f6f0){var _0x44c725={'name':path[_0x49b8('0x58')](_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5a')],path[_0x49b8('0x5b')](_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5a')])),'save_name':path[_0x49b8('0x58')](_0x23ecc2[_0x49b8('0x59')]['filename'],path[_0x49b8('0x5b')](_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5c')])),'original_format':path[_0x49b8('0x5b')](_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5a')])['substring'](0x1),'converted_format':_0x49b8('0x5d')};var _0x2f5b8b=path[_0x49b8('0x5e')](config[_0x49b8('0x4f')],_0x49b8('0x5f'),_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5c')]);var _0x66dc15=path[_0x49b8('0x5e')](config['root'],'server/files/sounds/converted',util[_0x49b8('0x55')]('%s.%s',path[_0x49b8('0x58')](_0x23ecc2['file'][_0x49b8('0x5c')],path[_0x49b8('0x5b')](_0x23ecc2[_0x49b8('0x59')][_0x49b8('0x5c')])),_0x44c725[_0x49b8('0x51')]));logger[_0x49b8('0x60')](_0x49b8('0x61'),_0x2f5b8b);logger[_0x49b8('0x60')]('Sound\x20upload\x20destination:\x20%s',_0x66dc15);try{fs[_0x49b8('0x62')](_0x2f5b8b,parseInt(_0x49b8('0x63'),0x8));var _0x58e3b6=sox[_0x49b8('0x64')](_0x2f5b8b,_0x66dc15,{'sampleRate':0x1f40,'format':_0x49b8('0x5d'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x58e3b6['on'](_0x49b8('0x2b'),function(_0x15f543){fs['unlinkSync'](_0x2f5b8b);_0x5acd90[_0x49b8('0x1d')](0x1f4)[_0x49b8('0x2e')](_0x15f543);});_0x58e3b6['on'](_0x49b8('0x65'),function(_0x4628aa){_0x44c725[_0x49b8('0x66')]=isNaN(_0x4628aa[_0x49b8('0x67')])?null:_0x4628aa['duration']*0x3e8;_0x44c725['original_sampleCount']=isNaN(_0x4628aa['sampleCount'])?null:_0x4628aa[_0x49b8('0x68')];_0x44c725['original_channelCount']=isNaN(_0x4628aa[_0x49b8('0x69')])?null:_0x4628aa[_0x49b8('0x69')];_0x44c725[_0x49b8('0x6a')]=isNaN(_0x4628aa[_0x49b8('0x6b')])?null:_0x4628aa['bitRate'];_0x44c725[_0x49b8('0x6c')]=isNaN(_0x4628aa[_0x49b8('0x6d')])?null:_0x4628aa['sampleRate'];});_0x58e3b6['on'](_0x49b8('0x6e'),function(_0x2c5db2){_0x44c725[_0x49b8('0x6f')]=isNaN(_0x2c5db2[_0x49b8('0x67')])?null:_0x2c5db2[_0x49b8('0x67')]*0x3e8;_0x44c725['converted_sampleCount']=isNaN(_0x2c5db2[_0x49b8('0x68')])?null:_0x2c5db2['sampleCount'];_0x44c725['converted_channelCount']=isNaN(_0x2c5db2[_0x49b8('0x69')])?null:_0x2c5db2[_0x49b8('0x69')];_0x44c725[_0x49b8('0x70')]=isNaN(_0x2c5db2[_0x49b8('0x6b')])?null:_0x2c5db2[_0x49b8('0x6b')];_0x44c725[_0x49b8('0x71')]=isNaN(_0x2c5db2[_0x49b8('0x6d')])?null:_0x2c5db2[_0x49b8('0x6d')];});_0x58e3b6['on'](_0x49b8('0x1e'),function(){fs['chmodSync'](_0x66dc15,parseInt('0777',0x8));return db['Sound']['create'](_0x44c725)[_0x49b8('0x27')](function(_0x49da97){var _0x2a6629={'role':_0x23ecc2[_0x49b8('0x72')][_0x49b8('0x73')],'userProfileId':_0x23ecc2[_0x49b8('0x72')][_0x49b8('0x74')]};if(!_0x2a6629)throw new Error(_0x49b8('0x75'));if(_0x2a6629['role']===_0x49b8('0x76')){var _0x55ef48=_0x49da97[_0x49b8('0x28')]({'plain':!![]});return db['UserProfileSection'][_0x49b8('0x4c')]({'where':{'name':_0x49b8('0x29'),'userProfileId':_0x2a6629[_0x49b8('0x74')]},'raw':!![]})['then'](function(_0x56e6b8){if(_0x56e6b8&&_0x56e6b8[_0x49b8('0x77')]===0x0){return db[_0x49b8('0x78')][_0x49b8('0x57')]({'name':_0x55ef48[_0x49b8('0x2d')],'resourceId':_0x55ef48['id'],'type':_0x56e6b8['name'],'sectionId':_0x56e6b8['id']},{})['then'](function(){return _0x49da97;});}else{return _0x49da97;}})[_0x49b8('0x49')](function(_0x419059){logger[_0x49b8('0x2b')](_0x49b8('0x79'),_0x419059);throw _0x419059;});}return _0x49da97;})[_0x49b8('0x27')](respondWithResult(_0x5acd90,null))[_0x49b8('0x49')](handleError(_0x5acd90,null));});_0x58e3b6[_0x49b8('0x7a')]();}catch(_0xdeea2f){_0x5acd90[_0x49b8('0x1d')](0x1f4)[_0x49b8('0x2e')](_0xdeea2f);}};exports[_0x49b8('0x7b')]=function(_0x19e1bf,_0x47cf72,_0x586f26){return db[_0x49b8('0x31')]['find']({'where':{'id':_0x19e1bf['params']['id']}})[_0x49b8('0x27')](handleEntityNotFound(_0x47cf72,null))[_0x49b8('0x27')](saveUpdates(_[_0x49b8('0x42')](_0x19e1bf[_0x49b8('0x72')],['name','description']),null))[_0x49b8('0x27')](respondWithResult(_0x47cf72,null))[_0x49b8('0x49')](handleError(_0x47cf72,null));};exports[_0x49b8('0x7c')]=function(_0x11ea2b,_0x1c3cb7,_0x475ad1){return db[_0x49b8('0x31')][_0x49b8('0x4c')]({'where':{'id':_0x11ea2b[_0x49b8('0x4b')]['id']}})[_0x49b8('0x27')](function(_0x59b82e){if(_0x59b82e){return _0x59b82e[_0x49b8('0x2a')]();}})[_0x49b8('0x27')](function(_0x3af6eb){var _0x5e721e=path['join'](config['root'],_0x49b8('0x5f'),util['format'](_0x49b8('0x56'),_0x3af6eb[_0x49b8('0x4d')],_0x3af6eb[_0x49b8('0x7d')]));var _0x120c41=path[_0x49b8('0x5e')](config[_0x49b8('0x4f')],_0x49b8('0x50'),util['format'](_0x49b8('0x56'),_0x3af6eb[_0x49b8('0x4d')],_0x3af6eb[_0x49b8('0x51')]));fs[_0x49b8('0x7e')](_0x5e721e);fs[_0x49b8('0x7e')](_0x120c41);return _0x3af6eb;})[_0x49b8('0x27')](function(_0x2f9173){if(_0x2f9173){return _0x1c3cb7[_0x49b8('0x1d')](0xcc)[_0x49b8('0x1e')]();}})[_0x49b8('0x27')](handleEntityNotFound(_0x1c3cb7,null))['catch'](handleError(_0x1c3cb7,null));};