Built motion from commit (unavailable).|2.5.21
[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 _0xa004=['keys','filters','query','intersection','fields','attributes','length','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','options','count','includeAll','include','findAll','show','params','rawAttributes','find','catch','save_name','converted_format','join','root','server/files/sounds/converted','Sequelize','Sound\x20not\x20found','format','%s.%s','create','basename','file','originalname','extname','filename','wav','server/files/sounds/original','Sound\x20upload\x20filepath:\x20%s','info','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','unlinkSync','src','original_duration','duration','original_sampleCount','sampleCount','original_channelCount','channelCount','bitRate','original_sampleRate','sampleRate','dest','converted_duration','converted_channelCount','converted_bitRate','converted_sampleRate','body','role','userProfileId','Unable\x20to\x20retrieve\x20the\x20current\x20user','user','UserProfileSection','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','description','delete','original_format','html-pdf','eml-format','rimraf','fast-json-patch','moment','bluebird','mustache','path','sox','to-csv','ejs','lodash','squel','crypto','jsforce','querystring','ioredis','../../components/parsers/advancedSearch','../../config/logger','api','../../config/utils','../../config/license/util','sendStatus','status','end','json','offset','limit','set','apply','reject','save','update','then','get','Sounds','UserProfileResource','destroy','error','stack','name','send','map','Sound','fieldName','type','key','model'];(function(_0x5a609d,_0x5ceef8){var _0x184849=function(_0x4db0dc){while(--_0x4db0dc){_0x5a609d['push'](_0x5a609d['shift']());}};_0x184849(++_0x5ceef8);}(_0xa004,0x1b8));var _0x4a00=function(_0x3dd15e,_0x1f0015){_0x3dd15e=_0x3dd15e-0x0;var _0x231fd0=_0xa004[_0x3dd15e];return _0x231fd0;};'use strict';var pdf=require(_0x4a00('0x0'));var emlformat=require(_0x4a00('0x1'));var rimraf=require(_0x4a00('0x2'));var zipdir=require('zip-dir');var jsonpatch=require(_0x4a00('0x3'));var rp=require('request-promise');var moment=require(_0x4a00('0x4'));var BPromise=require(_0x4a00('0x5'));var Mustache=require(_0x4a00('0x6'));var util=require('util');var path=require(_0x4a00('0x7'));var sox=require(_0x4a00('0x8'));var csv=require(_0x4a00('0x9'));var ejs=require(_0x4a00('0xa'));var fs=require('fs');var _=require(_0x4a00('0xb'));var squel=require(_0x4a00('0xc'));var crypto=require(_0x4a00('0xd'));var jsforce=require(_0x4a00('0xe'));var deskjs=require('desk.js');var toCsv=require('to-csv');var querystring=require(_0x4a00('0xf'));var Papa=require('papaparse');var Redis=require(_0x4a00('0x10'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x4a00('0x11'));var hardwareService=require('../../config/license/hardware');var logger=require(_0x4a00('0x12'))(_0x4a00('0x13'));var utils=require(_0x4a00('0x14'));var config=require('../../config/environment');var licenseUtil=require(_0x4a00('0x15'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x4cdfd7,_0x1d2647){_0x1d2647=_0x1d2647||0xcc;return function(_0x1df022){if(_0x1df022){return _0x4cdfd7[_0x4a00('0x16')](_0x1d2647);}return _0x4cdfd7[_0x4a00('0x17')](_0x1d2647)[_0x4a00('0x18')]();};}function respondWithResult(_0x4b4246,_0x569a64){_0x569a64=_0x569a64||0xc8;return function(_0x2d57ed){if(_0x2d57ed){return _0x4b4246['status'](_0x569a64)[_0x4a00('0x19')](_0x2d57ed);}};}function respondWithFilteredResult(_0x5cc1d7,_0x4aea8f){return function(_0x3085ed){if(_0x3085ed){var _0x20340c=_0x3085ed['count'],_0x2a3974=_0x4aea8f[_0x4a00('0x1a')],_0x4ffd9a=_0x4aea8f[_0x4a00('0x1a')]+_0x4aea8f[_0x4a00('0x1b')],_0x55d026;if(_0x4ffd9a>=_0x20340c){_0x4ffd9a=_0x20340c;_0x55d026=0xc8;}else{_0x55d026=0xce;}_0x5cc1d7[_0x4a00('0x17')](_0x55d026);return _0x5cc1d7[_0x4a00('0x1c')]('Content-Range',_0x2a3974+'-'+_0x4ffd9a+'/'+_0x20340c)[_0x4a00('0x19')](_0x3085ed);}return null;};}function patchUpdates(_0x26016e){return function(_0x111b49){try{jsonpatch[_0x4a00('0x1d')](_0x111b49,_0x26016e,!![]);}catch(_0x378efc){return BPromise[_0x4a00('0x1e')](_0x378efc);}return _0x111b49[_0x4a00('0x1f')]();};}function saveUpdates(_0x1ab033,_0x462aee){return function(_0x16a285){if(_0x16a285){return _0x16a285[_0x4a00('0x20')](_0x1ab033)[_0x4a00('0x21')](function(_0x249cc0){return _0x249cc0;});}return null;};}function removeEntity(_0x3ad5d1,_0xc92d62){return function(_0xb5bd4a){if(_0xb5bd4a){return _0xb5bd4a['destroy']()[_0x4a00('0x21')](function(){var _0xcb2232=_0xb5bd4a[_0x4a00('0x22')]({'plain':!![]});var _0x4b00e7=_0x4a00('0x23');return db[_0x4a00('0x24')][_0x4a00('0x25')]({'where':{'type':_0x4b00e7,'resourceId':_0xcb2232['id']}})[_0x4a00('0x21')](function(){return _0xb5bd4a;});})[_0x4a00('0x21')](function(){_0x3ad5d1[_0x4a00('0x17')](0xcc)[_0x4a00('0x18')]();});}};}function handleEntityNotFound(_0x256835,_0x1e0496){return function(_0x34d263){if(!_0x34d263){_0x256835[_0x4a00('0x16')](0x194);}return _0x34d263;};}function handleError(_0x39063e,_0x2d71bd){_0x2d71bd=_0x2d71bd||0x1f4;return function(_0x13d889){logger[_0x4a00('0x26')](_0x13d889[_0x4a00('0x27')]);if(_0x13d889[_0x4a00('0x28')]){delete _0x13d889[_0x4a00('0x28')];}_0x39063e[_0x4a00('0x17')](_0x2d71bd)[_0x4a00('0x29')](_0x13d889);};}exports['index']=function(_0x3dfedb,_0x51612b){var _0x28e92c={},_0xcbdf1f={},_0x1e7fff={'count':0x0,'rows':[]};var _0x265101=_[_0x4a00('0x2a')](db[_0x4a00('0x2b')]['rawAttributes'],function(_0x3c8d09){return{'name':_0x3c8d09[_0x4a00('0x2c')],'type':_0x3c8d09[_0x4a00('0x2d')][_0x4a00('0x2e')]};});_0xcbdf1f[_0x4a00('0x2f')]=_[_0x4a00('0x2a')](_0x265101,_0x4a00('0x28'));_0xcbdf1f['query']=_[_0x4a00('0x30')](_0x3dfedb['query']);_0xcbdf1f[_0x4a00('0x31')]=_['intersection'](_0xcbdf1f[_0x4a00('0x2f')],_0xcbdf1f[_0x4a00('0x32')]);_0x28e92c['attributes']=_[_0x4a00('0x33')](_0xcbdf1f[_0x4a00('0x2f')],qs[_0x4a00('0x34')](_0x3dfedb[_0x4a00('0x32')][_0x4a00('0x34')]));_0x28e92c[_0x4a00('0x35')]=_0x28e92c[_0x4a00('0x35')][_0x4a00('0x36')]?_0x28e92c[_0x4a00('0x35')]:_0xcbdf1f['model'];if(!_0x3dfedb[_0x4a00('0x32')]['hasOwnProperty'](_0x4a00('0x37'))){_0x28e92c[_0x4a00('0x1b')]=qs['limit'](_0x3dfedb[_0x4a00('0x32')][_0x4a00('0x1b')]);_0x28e92c[_0x4a00('0x1a')]=qs['offset'](_0x3dfedb[_0x4a00('0x32')][_0x4a00('0x1a')]);}_0x28e92c[_0x4a00('0x38')]=qs['sort'](_0x3dfedb[_0x4a00('0x32')][_0x4a00('0x39')]);_0x28e92c[_0x4a00('0x3a')]=qs['filters'](_[_0x4a00('0x3b')](_0x3dfedb['query'],_0xcbdf1f[_0x4a00('0x31')]),_0x265101);if(_0x3dfedb['query'][_0x4a00('0x3c')]){_0x28e92c[_0x4a00('0x3a')]=_[_0x4a00('0x3d')](_0x28e92c[_0x4a00('0x3a')],{'$or':_[_0x4a00('0x2a')](_0x265101,function(_0x3a5996){if(_0x3a5996[_0x4a00('0x2d')]!==_0x4a00('0x3e')){var _0x52d8dd={};_0x52d8dd[_0x3a5996[_0x4a00('0x28')]]={'$like':'%'+_0x3dfedb['query']['filter']+'%'};return _0x52d8dd;}})});}_0x28e92c=_[_0x4a00('0x3d')]({},_0x28e92c,_0x3dfedb[_0x4a00('0x3f')]);var _0x5f0cb6={'where':_0x28e92c[_0x4a00('0x3a')]};return db['Sound'][_0x4a00('0x40')](_0x5f0cb6)['then'](function(_0xe1c290){_0x1e7fff[_0x4a00('0x40')]=_0xe1c290;if(_0x3dfedb[_0x4a00('0x32')][_0x4a00('0x41')]){_0x28e92c[_0x4a00('0x42')]=[{'all':!![]}];}return db[_0x4a00('0x2b')][_0x4a00('0x43')](_0x28e92c);})['then'](function(_0x41dd9b){_0x1e7fff['rows']=_0x41dd9b;return _0x1e7fff;})['then'](respondWithFilteredResult(_0x51612b,_0x28e92c))['catch'](handleError(_0x51612b,null));};exports[_0x4a00('0x44')]=function(_0x522097,_0x6fe70e){var _0x53f8a8={'raw':!![],'where':{'id':_0x522097[_0x4a00('0x45')]['id']}},_0x29feb8={};_0x29feb8[_0x4a00('0x2f')]=_[_0x4a00('0x30')](db[_0x4a00('0x2b')][_0x4a00('0x46')]);_0x29feb8[_0x4a00('0x32')]=_[_0x4a00('0x30')](_0x522097[_0x4a00('0x32')]);_0x29feb8[_0x4a00('0x31')]=_[_0x4a00('0x33')](_0x29feb8[_0x4a00('0x2f')],_0x29feb8[_0x4a00('0x32')]);_0x53f8a8[_0x4a00('0x35')]=_['intersection'](_0x29feb8[_0x4a00('0x2f')],qs[_0x4a00('0x34')](_0x522097[_0x4a00('0x32')][_0x4a00('0x34')]));_0x53f8a8[_0x4a00('0x35')]=_0x53f8a8[_0x4a00('0x35')][_0x4a00('0x36')]?_0x53f8a8[_0x4a00('0x35')]:_0x29feb8[_0x4a00('0x2f')];if(_0x522097[_0x4a00('0x32')][_0x4a00('0x41')]){_0x53f8a8[_0x4a00('0x42')]=[{'all':!![]}];}_0x53f8a8=_[_0x4a00('0x3d')]({},_0x53f8a8,_0x522097[_0x4a00('0x3f')]);return db[_0x4a00('0x2b')][_0x4a00('0x47')](_0x53f8a8)[_0x4a00('0x21')](handleEntityNotFound(_0x6fe70e,null))[_0x4a00('0x21')](respondWithResult(_0x6fe70e,null))[_0x4a00('0x48')](handleError(_0x6fe70e,null));};exports['download']=function(_0x1eaa5c,_0x5253c4,_0x5a961c){return db['Sound'][_0x4a00('0x47')]({'where':{'id':_0x1eaa5c[_0x4a00('0x45')]['id']},'attributes':['name',_0x4a00('0x49'),_0x4a00('0x4a')],'raw':!![]})[_0x4a00('0x21')](handleEntityNotFound(_0x5253c4,null))[_0x4a00('0x21')](function(_0x1bfdfa){if(_0x1bfdfa){if(!fs['existsSync'](path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],_0x4a00('0x4d'),_0x1bfdfa[_0x4a00('0x49')]+'.'+_0x1bfdfa[_0x4a00('0x4a')]))){throw new db[(_0x4a00('0x4e'))]['ValidationError'](_0x4a00('0x4f'));}return _0x5253c4['download'](path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],_0x4a00('0x4d'),util[_0x4a00('0x50')](_0x4a00('0x51'),_0x1bfdfa[_0x4a00('0x49')],_0x1bfdfa['converted_format'])),util[_0x4a00('0x50')]('%s.%s',_0x1bfdfa[_0x4a00('0x28')],_0x1bfdfa['converted_format']));}return null;})[_0x4a00('0x48')](handleError(_0x5253c4,null));};exports[_0x4a00('0x52')]=function(_0x1ae7f5,_0x4a7163,_0x257e2f){var _0x5cc657={'name':path[_0x4a00('0x53')](_0x1ae7f5[_0x4a00('0x54')][_0x4a00('0x55')],path[_0x4a00('0x56')](_0x1ae7f5[_0x4a00('0x54')][_0x4a00('0x55')])),'save_name':path[_0x4a00('0x53')](_0x1ae7f5[_0x4a00('0x54')]['filename'],path[_0x4a00('0x56')](_0x1ae7f5['file'][_0x4a00('0x57')])),'original_format':path['extname'](_0x1ae7f5['file']['originalname'])['substring'](0x1),'converted_format':_0x4a00('0x58')};var _0x14b659=path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],_0x4a00('0x59'),_0x1ae7f5[_0x4a00('0x54')][_0x4a00('0x57')]);var _0x2238f2=path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],'server/files/sounds/converted',util['format']('%s.%s',path['basename'](_0x1ae7f5[_0x4a00('0x54')][_0x4a00('0x57')],path[_0x4a00('0x56')](_0x1ae7f5['file']['filename'])),_0x5cc657[_0x4a00('0x4a')]));logger['info'](_0x4a00('0x5a'),_0x14b659);logger[_0x4a00('0x5b')](_0x4a00('0x5c'),_0x2238f2);try{fs[_0x4a00('0x5d')](_0x14b659,parseInt(_0x4a00('0x5e'),0x8));var _0x40d6f9=sox['transcode'](_0x14b659,_0x2238f2,{'sampleRate':0x1f40,'format':_0x4a00('0x58'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x40d6f9['on'](_0x4a00('0x26'),function(_0x49d180){fs[_0x4a00('0x5f')](_0x14b659);_0x4a7163[_0x4a00('0x17')](0x1f4)[_0x4a00('0x29')](_0x49d180);});_0x40d6f9['on'](_0x4a00('0x60'),function(_0x2b8fb8){_0x5cc657[_0x4a00('0x61')]=isNaN(_0x2b8fb8[_0x4a00('0x62')])?null:_0x2b8fb8[_0x4a00('0x62')]*0x3e8;_0x5cc657[_0x4a00('0x63')]=isNaN(_0x2b8fb8[_0x4a00('0x64')])?null:_0x2b8fb8['sampleCount'];_0x5cc657[_0x4a00('0x65')]=isNaN(_0x2b8fb8[_0x4a00('0x66')])?null:_0x2b8fb8[_0x4a00('0x66')];_0x5cc657['original_bitRate']=isNaN(_0x2b8fb8[_0x4a00('0x67')])?null:_0x2b8fb8[_0x4a00('0x67')];_0x5cc657[_0x4a00('0x68')]=isNaN(_0x2b8fb8[_0x4a00('0x69')])?null:_0x2b8fb8[_0x4a00('0x69')];});_0x40d6f9['on'](_0x4a00('0x6a'),function(_0x621eb1){_0x5cc657[_0x4a00('0x6b')]=isNaN(_0x621eb1[_0x4a00('0x62')])?null:_0x621eb1[_0x4a00('0x62')]*0x3e8;_0x5cc657['converted_sampleCount']=isNaN(_0x621eb1[_0x4a00('0x64')])?null:_0x621eb1[_0x4a00('0x64')];_0x5cc657[_0x4a00('0x6c')]=isNaN(_0x621eb1[_0x4a00('0x66')])?null:_0x621eb1[_0x4a00('0x66')];_0x5cc657[_0x4a00('0x6d')]=isNaN(_0x621eb1[_0x4a00('0x67')])?null:_0x621eb1[_0x4a00('0x67')];_0x5cc657[_0x4a00('0x6e')]=isNaN(_0x621eb1['sampleRate'])?null:_0x621eb1[_0x4a00('0x69')];});_0x40d6f9['on']('end',function(){fs[_0x4a00('0x5d')](_0x2238f2,parseInt(_0x4a00('0x5e'),0x8));return db[_0x4a00('0x2b')]['create'](_0x5cc657)[_0x4a00('0x21')](function(_0x23c639){var _0x3e82dd={'role':_0x1ae7f5[_0x4a00('0x6f')][_0x4a00('0x70')],'userProfileId':_0x1ae7f5[_0x4a00('0x6f')][_0x4a00('0x71')]};if(!_0x3e82dd)throw new Error(_0x4a00('0x72'));if(_0x3e82dd[_0x4a00('0x70')]===_0x4a00('0x73')){var _0x2a0194=_0x23c639['get']({'plain':!![]});return db[_0x4a00('0x74')][_0x4a00('0x47')]({'where':{'name':'Sounds','userProfileId':_0x3e82dd[_0x4a00('0x71')]},'raw':!![]})[_0x4a00('0x21')](function(_0x15567a){if(_0x15567a&&_0x15567a[_0x4a00('0x75')]===0x0){return db[_0x4a00('0x24')][_0x4a00('0x52')]({'name':_0x2a0194[_0x4a00('0x28')],'resourceId':_0x2a0194['id'],'type':_0x15567a['name'],'sectionId':_0x15567a['id']},{})[_0x4a00('0x21')](function(){return _0x23c639;});}else{return _0x23c639;}})['catch'](function(_0x17199f){logger[_0x4a00('0x26')](_0x4a00('0x76'),_0x17199f);throw _0x17199f;});}return _0x23c639;})[_0x4a00('0x21')](respondWithResult(_0x4a7163,null))[_0x4a00('0x48')](handleError(_0x4a7163,null));});_0x40d6f9['start']();}catch(_0x2ae22c){_0x4a7163[_0x4a00('0x17')](0x1f4)[_0x4a00('0x29')](_0x2ae22c);}};exports['update']=function(_0x8b3b8d,_0x1ff6f5,_0x209c6f){return db[_0x4a00('0x2b')][_0x4a00('0x47')]({'where':{'id':_0x8b3b8d['params']['id']}})['then'](handleEntityNotFound(_0x1ff6f5,null))[_0x4a00('0x21')](saveUpdates(_[_0x4a00('0x3b')](_0x8b3b8d[_0x4a00('0x6f')],[_0x4a00('0x28'),_0x4a00('0x77')]),null))[_0x4a00('0x21')](respondWithResult(_0x1ff6f5,null))[_0x4a00('0x48')](handleError(_0x1ff6f5,null));};exports[_0x4a00('0x78')]=function(_0x311e7c,_0x3be136,_0x5d7db9){return db[_0x4a00('0x2b')]['find']({'where':{'id':_0x311e7c[_0x4a00('0x45')]['id']}})[_0x4a00('0x21')](function(_0x3e2eb7){if(_0x3e2eb7){return _0x3e2eb7[_0x4a00('0x25')]();}})[_0x4a00('0x21')](function(_0x3ab267){var _0x4fb553=path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],_0x4a00('0x59'),util[_0x4a00('0x50')]('%s.%s',_0x3ab267[_0x4a00('0x49')],_0x3ab267[_0x4a00('0x79')]));var _0x5ae469=path[_0x4a00('0x4b')](config[_0x4a00('0x4c')],_0x4a00('0x4d'),util[_0x4a00('0x50')](_0x4a00('0x51'),_0x3ab267[_0x4a00('0x49')],_0x3ab267[_0x4a00('0x4a')]));fs['unlinkSync'](_0x4fb553);fs['unlinkSync'](_0x5ae469);return _0x3ab267;})[_0x4a00('0x21')](function(_0x3d631d){if(_0x3d631d){return _0x3be136[_0x4a00('0x17')](0xcc)[_0x4a00('0x18')]();}})['then'](handleEntityNotFound(_0x3be136,null))[_0x4a00('0x48')](handleError(_0x3be136,null));};