2eea8b330cf9639b9eb173d1b09cc44c186cff8a
[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 _0xc167=['chmodSync','0777','transcode','src','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','find','autoAssociation','UserProfileResource','start','description','delete','server/files/sounds/original','original_format','unlinkSync','eml-format','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','crypto','jsforce','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/utils','../../config/environment','sendStatus','status','end','limit','undefined','count','offset','Content-Range','reject','save','update','then','destroy','get','Sounds','error','stack','name','send','index','Sound','type','key','model','map','query','keys','filters','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','VIRTUAL','includeAll','catch','params','merge','options','download','converted_format','join','server/files/sounds/converted','save_name','Sequelize','ValidationError','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','extname','filename','wav','root','info','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s'];(function(_0x400b43,_0x55da4e){var _0x3c330f=function(_0x5bbd52){while(--_0x5bbd52){_0x400b43['push'](_0x400b43['shift']());}};_0x3c330f(++_0x55da4e);}(_0xc167,0x184));var _0x7c16=function(_0x5187bb,_0x3da6ef){_0x5187bb=_0x5187bb-0x0;var _0x530275=_0xc167[_0x5187bb];return _0x530275;};'use strict';var emlformat=require(_0x7c16('0x0'));var rimraf=require('rimraf');var zipdir=require('zip-dir');var jsonpatch=require(_0x7c16('0x1'));var rp=require(_0x7c16('0x2'));var moment=require(_0x7c16('0x3'));var BPromise=require(_0x7c16('0x4'));var Mustache=require(_0x7c16('0x5'));var util=require(_0x7c16('0x6'));var path=require('path');var sox=require(_0x7c16('0x7'));var csv=require(_0x7c16('0x8'));var ejs=require(_0x7c16('0x9'));var fs=require('fs');var fs_extra=require(_0x7c16('0xa'));var _=require('lodash');var squel=require('squel');var crypto=require(_0x7c16('0xb'));var jsforce=require(_0x7c16('0xc'));var deskjs=require('desk.js');var toCsv=require(_0x7c16('0x8'));var querystring=require(_0x7c16('0xd'));var Papa=require('papaparse');var Redis=require(_0x7c16('0xe'));var authService=require(_0x7c16('0xf'));var qs=require(_0x7c16('0x10'));var as=require(_0x7c16('0x11'));var hardwareService=require(_0x7c16('0x12'));var logger=require(_0x7c16('0x13'))('api');var utils=require(_0x7c16('0x14'));var config=require(_0x7c16('0x15'));var licenseUtil=require('../../config/license/util');var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x5b2276,_0x267eb6){_0x267eb6=_0x267eb6||0xcc;return function(_0x8bc4ac){if(_0x8bc4ac){return _0x5b2276[_0x7c16('0x16')](_0x267eb6);}return _0x5b2276[_0x7c16('0x17')](_0x267eb6)[_0x7c16('0x18')]();};}function respondWithResult(_0x4b1c66,_0x53a216){_0x53a216=_0x53a216||0xc8;return function(_0x2eb0d5){if(_0x2eb0d5){return _0x4b1c66['status'](_0x53a216)['json'](_0x2eb0d5);}};}function respondWithFilteredResult(_0x2c360d,_0x41906b){return function(_0x31f757){if(_0x31f757){var _0x454c1c=typeof _0x41906b['offset']==='undefined'&&typeof _0x41906b[_0x7c16('0x19')]===_0x7c16('0x1a');var _0x43d601=_0x31f757[_0x7c16('0x1b')];var _0x54e232=_0x454c1c?0x0:_0x41906b[_0x7c16('0x1c')];var _0x5e838f=_0x454c1c?_0x31f757[_0x7c16('0x1b')]:_0x41906b[_0x7c16('0x1c')]+_0x41906b[_0x7c16('0x19')];var _0x374c3e;if(_0x5e838f>=_0x43d601){_0x5e838f=_0x43d601;_0x374c3e=0xc8;}else{_0x374c3e=0xce;}_0x2c360d[_0x7c16('0x17')](_0x374c3e);return _0x2c360d['set'](_0x7c16('0x1d'),_0x54e232+'-'+_0x5e838f+'/'+_0x43d601)['json'](_0x31f757);}return null;};}function patchUpdates(_0x39f4e0){return function(_0xbd1fca){try{jsonpatch['apply'](_0xbd1fca,_0x39f4e0,!![]);}catch(_0xfc653a){return BPromise[_0x7c16('0x1e')](_0xfc653a);}return _0xbd1fca[_0x7c16('0x1f')]();};}function saveUpdates(_0x5ce829,_0x1481e0){return function(_0x5226d1){if(_0x5226d1){return _0x5226d1[_0x7c16('0x20')](_0x5ce829)[_0x7c16('0x21')](function(_0x598e32){return _0x598e32;});}return null;};}function removeEntity(_0x465e6e,_0x4da3b7){return function(_0x1bcb9c){if(_0x1bcb9c){return _0x1bcb9c[_0x7c16('0x22')]()[_0x7c16('0x21')](function(){var _0x282b6c=_0x1bcb9c[_0x7c16('0x23')]({'plain':!![]});var _0x3132fa=_0x7c16('0x24');return db['UserProfileResource']['destroy']({'where':{'type':_0x3132fa,'resourceId':_0x282b6c['id']}})[_0x7c16('0x21')](function(){return _0x1bcb9c;});})[_0x7c16('0x21')](function(){_0x465e6e[_0x7c16('0x17')](0xcc)[_0x7c16('0x18')]();});}};}function handleEntityNotFound(_0x544827,_0x2b05de){return function(_0x34bcc0){if(!_0x34bcc0){_0x544827[_0x7c16('0x16')](0x194);}return _0x34bcc0;};}function handleError(_0x39c638,_0x11a17d){_0x11a17d=_0x11a17d||0x1f4;return function(_0x1dbc23){logger[_0x7c16('0x25')](_0x1dbc23[_0x7c16('0x26')]);if(_0x1dbc23[_0x7c16('0x27')]){delete _0x1dbc23['name'];}_0x39c638['status'](_0x11a17d)[_0x7c16('0x28')](_0x1dbc23);};}exports[_0x7c16('0x29')]=function(_0x582135,_0x26515a){var _0x1cb5e9={},_0x1d5cef={},_0x1f38b1={'count':0x0,'rows':[]};var _0x2340f2=_['map'](db[_0x7c16('0x2a')]['rawAttributes'],function(_0x1c20db){return{'name':_0x1c20db['fieldName'],'type':_0x1c20db[_0x7c16('0x2b')][_0x7c16('0x2c')]};});_0x1d5cef[_0x7c16('0x2d')]=_[_0x7c16('0x2e')](_0x2340f2,_0x7c16('0x27'));_0x1d5cef[_0x7c16('0x2f')]=_[_0x7c16('0x30')](_0x582135[_0x7c16('0x2f')]);_0x1d5cef[_0x7c16('0x31')]=_[_0x7c16('0x32')](_0x1d5cef[_0x7c16('0x2d')],_0x1d5cef[_0x7c16('0x2f')]);_0x1cb5e9[_0x7c16('0x33')]=_['intersection'](_0x1d5cef[_0x7c16('0x2d')],qs[_0x7c16('0x34')](_0x582135[_0x7c16('0x2f')][_0x7c16('0x34')]));_0x1cb5e9[_0x7c16('0x33')]=_0x1cb5e9[_0x7c16('0x33')][_0x7c16('0x35')]?_0x1cb5e9[_0x7c16('0x33')]:_0x1d5cef[_0x7c16('0x2d')];if(!_0x582135[_0x7c16('0x2f')][_0x7c16('0x36')](_0x7c16('0x37'))){_0x1cb5e9[_0x7c16('0x19')]=qs[_0x7c16('0x19')](_0x582135['query'][_0x7c16('0x19')]);_0x1cb5e9['offset']=qs[_0x7c16('0x1c')](_0x582135[_0x7c16('0x2f')][_0x7c16('0x1c')]);}_0x1cb5e9[_0x7c16('0x38')]=qs[_0x7c16('0x39')](_0x582135['query'][_0x7c16('0x39')]);_0x1cb5e9[_0x7c16('0x3a')]=qs[_0x7c16('0x31')](_[_0x7c16('0x3b')](_0x582135[_0x7c16('0x2f')],_0x1d5cef[_0x7c16('0x31')]),_0x2340f2);if(_0x582135['query'][_0x7c16('0x3c')]){_0x1cb5e9[_0x7c16('0x3a')]=_['merge'](_0x1cb5e9['where'],{'$or':_['map'](_0x2340f2,function(_0x389b95){if(_0x389b95[_0x7c16('0x2b')]!==_0x7c16('0x3d')){var _0x5103c7={};_0x5103c7[_0x389b95['name']]={'$like':'%'+_0x582135['query'][_0x7c16('0x3c')]+'%'};return _0x5103c7;}})});}_0x1cb5e9=_['merge']({},_0x1cb5e9,_0x582135['options']);var _0x4507be={'where':_0x1cb5e9['where']};return db['Sound']['count'](_0x4507be)[_0x7c16('0x21')](function(_0x1ccea7){_0x1f38b1[_0x7c16('0x1b')]=_0x1ccea7;if(_0x582135[_0x7c16('0x2f')][_0x7c16('0x3e')]){_0x1cb5e9['include']=[{'all':!![]}];}return db[_0x7c16('0x2a')]['findAll'](_0x1cb5e9);})[_0x7c16('0x21')](function(_0x6798ce){_0x1f38b1['rows']=_0x6798ce;return _0x1f38b1;})[_0x7c16('0x21')](respondWithFilteredResult(_0x26515a,_0x1cb5e9))[_0x7c16('0x3f')](handleError(_0x26515a,null));};exports['show']=function(_0x494153,_0x19677e){var _0x379fc1={'raw':!![],'where':{'id':_0x494153[_0x7c16('0x40')]['id']}},_0x533118={};_0x533118[_0x7c16('0x2d')]=_[_0x7c16('0x30')](db[_0x7c16('0x2a')]['rawAttributes']);_0x533118[_0x7c16('0x2f')]=_['keys'](_0x494153['query']);_0x533118[_0x7c16('0x31')]=_[_0x7c16('0x32')](_0x533118[_0x7c16('0x2d')],_0x533118[_0x7c16('0x2f')]);_0x379fc1[_0x7c16('0x33')]=_['intersection'](_0x533118[_0x7c16('0x2d')],qs[_0x7c16('0x34')](_0x494153['query']['fields']));_0x379fc1[_0x7c16('0x33')]=_0x379fc1[_0x7c16('0x33')][_0x7c16('0x35')]?_0x379fc1['attributes']:_0x533118[_0x7c16('0x2d')];if(_0x494153[_0x7c16('0x2f')][_0x7c16('0x3e')]){_0x379fc1['include']=[{'all':!![]}];}_0x379fc1=_[_0x7c16('0x41')]({},_0x379fc1,_0x494153[_0x7c16('0x42')]);return db['Sound']['find'](_0x379fc1)[_0x7c16('0x21')](handleEntityNotFound(_0x19677e,null))[_0x7c16('0x21')](respondWithResult(_0x19677e,null))[_0x7c16('0x3f')](handleError(_0x19677e,null));};exports[_0x7c16('0x43')]=function(_0x41373c,_0x557966,_0x5232aa){return db['Sound']['find']({'where':{'id':_0x41373c[_0x7c16('0x40')]['id']},'attributes':[_0x7c16('0x27'),'save_name',_0x7c16('0x44')],'raw':!![]})[_0x7c16('0x21')](handleEntityNotFound(_0x557966,null))['then'](function(_0x3a81c6){if(_0x3a81c6){if(!fs['existsSync'](path[_0x7c16('0x45')](config['root'],_0x7c16('0x46'),_0x3a81c6[_0x7c16('0x47')]+'.'+_0x3a81c6[_0x7c16('0x44')]))){throw new db[(_0x7c16('0x48'))][(_0x7c16('0x49'))](_0x7c16('0x4a'));}return _0x557966[_0x7c16('0x43')](path['join'](config['root'],_0x7c16('0x46'),util[_0x7c16('0x4b')](_0x7c16('0x4c'),_0x3a81c6['save_name'],_0x3a81c6['converted_format'])),util['format']('%s.%s',_0x3a81c6[_0x7c16('0x27')],_0x3a81c6[_0x7c16('0x44')]));}return null;})['catch'](handleError(_0x557966,null));};exports[_0x7c16('0x4d')]=function(_0x25aca8,_0x31f606,_0x4dcc0b){var _0x5b0c91={'name':path[_0x7c16('0x4e')](_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x50')],path[_0x7c16('0x51')](_0x25aca8['file'][_0x7c16('0x50')])),'save_name':path[_0x7c16('0x4e')](_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x52')],path[_0x7c16('0x51')](_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x52')])),'original_format':path[_0x7c16('0x51')](_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x50')])['substring'](0x1),'converted_format':_0x7c16('0x53')};var _0x29f153=path[_0x7c16('0x45')](config['root'],'server/files/sounds/original',_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x52')]);var _0x3b1ff4=path[_0x7c16('0x45')](config[_0x7c16('0x54')],'server/files/sounds/converted',util[_0x7c16('0x4b')](_0x7c16('0x4c'),path[_0x7c16('0x4e')](_0x25aca8[_0x7c16('0x4f')][_0x7c16('0x52')],path[_0x7c16('0x51')](_0x25aca8[_0x7c16('0x4f')]['filename'])),_0x5b0c91[_0x7c16('0x44')]));logger[_0x7c16('0x55')](_0x7c16('0x56'),_0x29f153);logger[_0x7c16('0x55')](_0x7c16('0x57'),_0x3b1ff4);try{fs[_0x7c16('0x58')](_0x29f153,parseInt(_0x7c16('0x59'),0x8));var _0x37577d=sox[_0x7c16('0x5a')](_0x29f153,_0x3b1ff4,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x37577d['on']('error',function(_0x130e42){fs['unlinkSync'](_0x29f153);_0x31f606['status'](0x1f4)[_0x7c16('0x28')](_0x130e42);});_0x37577d['on'](_0x7c16('0x5b'),function(_0x2e79ca){_0x5b0c91['original_duration']=isNaN(_0x2e79ca[_0x7c16('0x5c')])?null:_0x2e79ca[_0x7c16('0x5c')]*0x3e8;_0x5b0c91[_0x7c16('0x5d')]=isNaN(_0x2e79ca['sampleCount'])?null:_0x2e79ca[_0x7c16('0x5e')];_0x5b0c91[_0x7c16('0x5f')]=isNaN(_0x2e79ca[_0x7c16('0x60')])?null:_0x2e79ca[_0x7c16('0x60')];_0x5b0c91[_0x7c16('0x61')]=isNaN(_0x2e79ca[_0x7c16('0x62')])?null:_0x2e79ca['bitRate'];_0x5b0c91[_0x7c16('0x63')]=isNaN(_0x2e79ca[_0x7c16('0x64')])?null:_0x2e79ca[_0x7c16('0x64')];});_0x37577d['on'](_0x7c16('0x65'),function(_0x214f15){_0x5b0c91[_0x7c16('0x66')]=isNaN(_0x214f15[_0x7c16('0x5c')])?null:_0x214f15[_0x7c16('0x5c')]*0x3e8;_0x5b0c91['converted_sampleCount']=isNaN(_0x214f15[_0x7c16('0x5e')])?null:_0x214f15[_0x7c16('0x5e')];_0x5b0c91[_0x7c16('0x67')]=isNaN(_0x214f15['channelCount'])?null:_0x214f15[_0x7c16('0x60')];_0x5b0c91[_0x7c16('0x68')]=isNaN(_0x214f15[_0x7c16('0x62')])?null:_0x214f15[_0x7c16('0x62')];_0x5b0c91[_0x7c16('0x69')]=isNaN(_0x214f15[_0x7c16('0x64')])?null:_0x214f15[_0x7c16('0x64')];});_0x37577d['on'](_0x7c16('0x18'),function(){fs['chmodSync'](_0x3b1ff4,parseInt(_0x7c16('0x59'),0x8));return db[_0x7c16('0x2a')][_0x7c16('0x4d')](_0x5b0c91)[_0x7c16('0x21')](function(_0x438a78){var _0x31f5ce={'role':_0x25aca8[_0x7c16('0x6a')][_0x7c16('0x6b')],'userProfileId':_0x25aca8[_0x7c16('0x6a')][_0x7c16('0x6c')]};if(!_0x31f5ce)throw new Error(_0x7c16('0x6d'));if(_0x31f5ce[_0x7c16('0x6b')]==='user'){var _0x29b3cf=_0x438a78[_0x7c16('0x23')]({'plain':!![]});return db['UserProfileSection'][_0x7c16('0x6e')]({'where':{'name':'Sounds','userProfileId':_0x31f5ce['userProfileId']},'raw':!![]})[_0x7c16('0x21')](function(_0x33ec18){if(_0x33ec18&&_0x33ec18[_0x7c16('0x6f')]===0x0){return db[_0x7c16('0x70')][_0x7c16('0x4d')]({'name':_0x29b3cf[_0x7c16('0x27')],'resourceId':_0x29b3cf['id'],'type':_0x33ec18[_0x7c16('0x27')],'sectionId':_0x33ec18['id']},{})[_0x7c16('0x21')](function(){return _0x438a78;});}else{return _0x438a78;}})[_0x7c16('0x3f')](function(_0xc55eae){logger[_0x7c16('0x25')]('Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s',_0xc55eae);throw _0xc55eae;});}return _0x438a78;})[_0x7c16('0x21')](respondWithResult(_0x31f606,null))[_0x7c16('0x3f')](handleError(_0x31f606,null));});_0x37577d[_0x7c16('0x71')]();}catch(_0x473224){_0x31f606[_0x7c16('0x17')](0x1f4)[_0x7c16('0x28')](_0x473224);}};exports['update']=function(_0x47564d,_0x496123,_0x7e0071){return db[_0x7c16('0x2a')]['find']({'where':{'id':_0x47564d[_0x7c16('0x40')]['id']}})['then'](handleEntityNotFound(_0x496123,null))[_0x7c16('0x21')](saveUpdates(_[_0x7c16('0x3b')](_0x47564d[_0x7c16('0x6a')],['name',_0x7c16('0x72')]),null))[_0x7c16('0x21')](respondWithResult(_0x496123,null))[_0x7c16('0x3f')](handleError(_0x496123,null));};exports[_0x7c16('0x73')]=function(_0x4d9a9f,_0x3771fd,_0xccfa72){return db[_0x7c16('0x2a')][_0x7c16('0x6e')]({'where':{'id':_0x4d9a9f['params']['id']}})[_0x7c16('0x21')](function(_0xc9f740){if(_0xc9f740){return _0xc9f740[_0x7c16('0x22')]();}})[_0x7c16('0x21')](function(_0x447418){var _0x22473b=path['join'](config[_0x7c16('0x54')],_0x7c16('0x74'),util['format'](_0x7c16('0x4c'),_0x447418[_0x7c16('0x47')],_0x447418[_0x7c16('0x75')]));var _0x4f4ab5=path[_0x7c16('0x45')](config[_0x7c16('0x54')],_0x7c16('0x46'),util[_0x7c16('0x4b')]('%s.%s',_0x447418[_0x7c16('0x47')],_0x447418['converted_format']));fs['unlinkSync'](_0x22473b);fs[_0x7c16('0x76')](_0x4f4ab5);return _0x447418;})[_0x7c16('0x21')](function(_0x29e57f){if(_0x29e57f){return _0x3771fd[_0x7c16('0x17')](0xcc)['end']();}})[_0x7c16('0x21')](handleEntityNotFound(_0x3771fd,null))[_0x7c16('0x3f')](handleError(_0x3771fd,null));};