Built motion from commit fe909640.|2.6.8
[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 _0x4743=['role','body','user','UserProfileSection','userProfileId','autoAssociation','Unable\x20to\x20associate\x20the\x20resource\x20to\x20the\x20User\x20Profile:\x20%s','start','update','pick','description','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','ioredis','../../config/license/hardware','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','end','offset','undefined','count','limit','set','json','apply','reject','save','destroy','get','Sounds','UserProfileResource','then','status','error','stack','name','send','index','Sound','rawAttributes','type','model','map','query','keys','filters','intersection','fields','attributes','length','hasOwnProperty','sort','where','filter','merge','includeAll','include','rows','catch','params','options','find','download','converted_format','join','root','server/files/sounds/converted','save_name','Sequelize','ValidationError','format','%s.%s','basename','originalname','extname','file','filename','substring','server/files/sounds/original','info','Sound\x20upload\x20filepath:\x20%s','Sound\x20upload\x20destination:\x20%s','chmodSync','0777','transcode','unlinkSync','src','duration','original_sampleCount','sampleCount','original_channelCount','original_bitRate','bitRate','original_sampleRate','sampleRate','converted_duration','converted_sampleCount','converted_channelCount','channelCount','converted_bitRate','converted_sampleRate','create'];(function(_0x19e35c,_0x29cd1e){var _0x3e1fe8=function(_0x17258b){while(--_0x17258b){_0x19e35c['push'](_0x19e35c['shift']());}};_0x3e1fe8(++_0x29cd1e);}(_0x4743,0x101));var _0x3474=function(_0x422ac3,_0x48bbd4){_0x422ac3=_0x422ac3-0x0;var _0x53c0d4=_0x4743[_0x422ac3];return _0x53c0d4;};'use strict';var emlformat=require(_0x3474('0x0'));var rimraf=require(_0x3474('0x1'));var zipdir=require(_0x3474('0x2'));var jsonpatch=require(_0x3474('0x3'));var rp=require(_0x3474('0x4'));var moment=require(_0x3474('0x5'));var BPromise=require(_0x3474('0x6'));var Mustache=require(_0x3474('0x7'));var util=require(_0x3474('0x8'));var path=require('path');var sox=require(_0x3474('0x9'));var csv=require(_0x3474('0xa'));var ejs=require(_0x3474('0xb'));var fs=require('fs');var fs_extra=require(_0x3474('0xc'));var _=require(_0x3474('0xd'));var squel=require(_0x3474('0xe'));var crypto=require(_0x3474('0xf'));var jsforce=require(_0x3474('0x10'));var deskjs=require(_0x3474('0x11'));var toCsv=require(_0x3474('0xa'));var querystring=require(_0x3474('0x12'));var Papa=require(_0x3474('0x13'));var Redis=require(_0x3474('0x14'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x3474('0x15'));var logger=require('../../config/logger')(_0x3474('0x16'));var utils=require(_0x3474('0x17'));var config=require(_0x3474('0x18'));var licenseUtil=require('../../config/license/util');var db=require(_0x3474('0x19'))['db'];function respondWithStatusCode(_0x13f48d,_0xf67259){_0xf67259=_0xf67259||0xcc;return function(_0x181436){if(_0x181436){return _0x13f48d[_0x3474('0x1a')](_0xf67259);}return _0x13f48d['status'](_0xf67259)[_0x3474('0x1b')]();};}function respondWithResult(_0x343ba3,_0x22bdb7){_0x22bdb7=_0x22bdb7||0xc8;return function(_0x69916f){if(_0x69916f){return _0x343ba3['status'](_0x22bdb7)['json'](_0x69916f);}};}function respondWithFilteredResult(_0x16cefa,_0x7eecea){return function(_0x209e4b){if(_0x209e4b){var _0xf4d3c2=typeof _0x7eecea[_0x3474('0x1c')]===_0x3474('0x1d')&&typeof _0x7eecea['limit']===_0x3474('0x1d');var _0x31a2f5=_0x209e4b[_0x3474('0x1e')];var _0x39ba76=_0xf4d3c2?0x0:_0x7eecea['offset'];var _0x2fd2dd=_0xf4d3c2?_0x209e4b[_0x3474('0x1e')]:_0x7eecea[_0x3474('0x1c')]+_0x7eecea[_0x3474('0x1f')];var _0x2198fd;if(_0x2fd2dd>=_0x31a2f5){_0x2fd2dd=_0x31a2f5;_0x2198fd=0xc8;}else{_0x2198fd=0xce;}_0x16cefa['status'](_0x2198fd);return _0x16cefa[_0x3474('0x20')]('Content-Range',_0x39ba76+'-'+_0x2fd2dd+'/'+_0x31a2f5)[_0x3474('0x21')](_0x209e4b);}return null;};}function patchUpdates(_0x30d8fa){return function(_0x193a05){try{jsonpatch[_0x3474('0x22')](_0x193a05,_0x30d8fa,!![]);}catch(_0x1d1eed){return BPromise[_0x3474('0x23')](_0x1d1eed);}return _0x193a05[_0x3474('0x24')]();};}function saveUpdates(_0x171837,_0x5b5c0a){return function(_0x1efe2f){if(_0x1efe2f){return _0x1efe2f['update'](_0x171837)['then'](function(_0x17df90){return _0x17df90;});}return null;};}function removeEntity(_0x4e8435,_0x232551){return function(_0x3eead9){if(_0x3eead9){return _0x3eead9[_0x3474('0x25')]()['then'](function(){var _0x5502d6=_0x3eead9[_0x3474('0x26')]({'plain':!![]});var _0x24aeb4=_0x3474('0x27');return db[_0x3474('0x28')][_0x3474('0x25')]({'where':{'type':_0x24aeb4,'resourceId':_0x5502d6['id']}})[_0x3474('0x29')](function(){return _0x3eead9;});})[_0x3474('0x29')](function(){_0x4e8435[_0x3474('0x2a')](0xcc)[_0x3474('0x1b')]();});}};}function handleEntityNotFound(_0x360c98,_0x522436){return function(_0x358588){if(!_0x358588){_0x360c98[_0x3474('0x1a')](0x194);}return _0x358588;};}function handleError(_0x4b76c2,_0x5589a2){_0x5589a2=_0x5589a2||0x1f4;return function(_0x1fc59c){logger[_0x3474('0x2b')](_0x1fc59c[_0x3474('0x2c')]);if(_0x1fc59c['name']){delete _0x1fc59c[_0x3474('0x2d')];}_0x4b76c2[_0x3474('0x2a')](_0x5589a2)[_0x3474('0x2e')](_0x1fc59c);};}exports[_0x3474('0x2f')]=function(_0x104c4c,_0x3c3f77){var _0x12ecc7={},_0x1d5b0b={},_0x4d2fd2={'count':0x0,'rows':[]};var _0x31825e=_['map'](db[_0x3474('0x30')][_0x3474('0x31')],function(_0x29efe4){return{'name':_0x29efe4['fieldName'],'type':_0x29efe4[_0x3474('0x32')]['key']};});_0x1d5b0b[_0x3474('0x33')]=_[_0x3474('0x34')](_0x31825e,_0x3474('0x2d'));_0x1d5b0b[_0x3474('0x35')]=_[_0x3474('0x36')](_0x104c4c[_0x3474('0x35')]);_0x1d5b0b[_0x3474('0x37')]=_[_0x3474('0x38')](_0x1d5b0b[_0x3474('0x33')],_0x1d5b0b[_0x3474('0x35')]);_0x12ecc7['attributes']=_[_0x3474('0x38')](_0x1d5b0b['model'],qs[_0x3474('0x39')](_0x104c4c[_0x3474('0x35')][_0x3474('0x39')]));_0x12ecc7[_0x3474('0x3a')]=_0x12ecc7[_0x3474('0x3a')][_0x3474('0x3b')]?_0x12ecc7[_0x3474('0x3a')]:_0x1d5b0b[_0x3474('0x33')];if(!_0x104c4c[_0x3474('0x35')][_0x3474('0x3c')]('nolimit')){_0x12ecc7[_0x3474('0x1f')]=qs[_0x3474('0x1f')](_0x104c4c[_0x3474('0x35')][_0x3474('0x1f')]);_0x12ecc7[_0x3474('0x1c')]=qs[_0x3474('0x1c')](_0x104c4c[_0x3474('0x35')][_0x3474('0x1c')]);}_0x12ecc7['order']=qs[_0x3474('0x3d')](_0x104c4c[_0x3474('0x35')][_0x3474('0x3d')]);_0x12ecc7[_0x3474('0x3e')]=qs['filters'](_['pick'](_0x104c4c[_0x3474('0x35')],_0x1d5b0b[_0x3474('0x37')]),_0x31825e);if(_0x104c4c['query'][_0x3474('0x3f')]){_0x12ecc7[_0x3474('0x3e')]=_[_0x3474('0x40')](_0x12ecc7[_0x3474('0x3e')],{'$or':_[_0x3474('0x34')](_0x31825e,function(_0x23eecf){if(_0x23eecf[_0x3474('0x32')]!=='VIRTUAL'){var _0x4774fe={};_0x4774fe[_0x23eecf['name']]={'$like':'%'+_0x104c4c[_0x3474('0x35')]['filter']+'%'};return _0x4774fe;}})});}_0x12ecc7=_[_0x3474('0x40')]({},_0x12ecc7,_0x104c4c['options']);var _0x4b61c9={'where':_0x12ecc7['where']};return db[_0x3474('0x30')]['count'](_0x4b61c9)[_0x3474('0x29')](function(_0x589e4d){_0x4d2fd2['count']=_0x589e4d;if(_0x104c4c[_0x3474('0x35')][_0x3474('0x41')]){_0x12ecc7[_0x3474('0x42')]=[{'all':!![]}];}return db[_0x3474('0x30')]['findAll'](_0x12ecc7);})[_0x3474('0x29')](function(_0x4139d0){_0x4d2fd2[_0x3474('0x43')]=_0x4139d0;return _0x4d2fd2;})['then'](respondWithFilteredResult(_0x3c3f77,_0x12ecc7))[_0x3474('0x44')](handleError(_0x3c3f77,null));};exports['show']=function(_0x49738f,_0x49aaed){var _0x1b025b={'raw':!![],'where':{'id':_0x49738f[_0x3474('0x45')]['id']}},_0x21639b={};_0x21639b[_0x3474('0x33')]=_[_0x3474('0x36')](db[_0x3474('0x30')][_0x3474('0x31')]);_0x21639b['query']=_[_0x3474('0x36')](_0x49738f[_0x3474('0x35')]);_0x21639b['filters']=_[_0x3474('0x38')](_0x21639b[_0x3474('0x33')],_0x21639b[_0x3474('0x35')]);_0x1b025b[_0x3474('0x3a')]=_[_0x3474('0x38')](_0x21639b[_0x3474('0x33')],qs['fields'](_0x49738f[_0x3474('0x35')][_0x3474('0x39')]));_0x1b025b[_0x3474('0x3a')]=_0x1b025b[_0x3474('0x3a')]['length']?_0x1b025b[_0x3474('0x3a')]:_0x21639b[_0x3474('0x33')];if(_0x49738f[_0x3474('0x35')][_0x3474('0x41')]){_0x1b025b[_0x3474('0x42')]=[{'all':!![]}];}_0x1b025b=_['merge']({},_0x1b025b,_0x49738f[_0x3474('0x46')]);return db[_0x3474('0x30')][_0x3474('0x47')](_0x1b025b)[_0x3474('0x29')](handleEntityNotFound(_0x49aaed,null))['then'](respondWithResult(_0x49aaed,null))[_0x3474('0x44')](handleError(_0x49aaed,null));};exports[_0x3474('0x48')]=function(_0x4799cf,_0x493536,_0x48c4f8){return db[_0x3474('0x30')][_0x3474('0x47')]({'where':{'id':_0x4799cf['params']['id']},'attributes':[_0x3474('0x2d'),'save_name',_0x3474('0x49')],'raw':!![]})['then'](handleEntityNotFound(_0x493536,null))[_0x3474('0x29')](function(_0x3e7147){if(_0x3e7147){if(!fs['existsSync'](path[_0x3474('0x4a')](config[_0x3474('0x4b')],_0x3474('0x4c'),_0x3e7147[_0x3474('0x4d')]+'.'+_0x3e7147[_0x3474('0x49')]))){throw new db[(_0x3474('0x4e'))][(_0x3474('0x4f'))]('Sound\x20not\x20found');}return _0x493536[_0x3474('0x48')](path[_0x3474('0x4a')](config['root'],'server/files/sounds/converted',util[_0x3474('0x50')]('%s.%s',_0x3e7147[_0x3474('0x4d')],_0x3e7147[_0x3474('0x49')])),util[_0x3474('0x50')](_0x3474('0x51'),_0x3e7147['name'],_0x3e7147[_0x3474('0x49')]));}return null;})['catch'](handleError(_0x493536,null));};exports['create']=function(_0x262382,_0x2dd68d,_0x40729c){var _0x5dbbf2={'name':path[_0x3474('0x52')](_0x262382['file'][_0x3474('0x53')],path[_0x3474('0x54')](_0x262382[_0x3474('0x55')][_0x3474('0x53')])),'save_name':path[_0x3474('0x52')](_0x262382[_0x3474('0x55')][_0x3474('0x56')],path['extname'](_0x262382[_0x3474('0x55')][_0x3474('0x56')])),'original_format':path[_0x3474('0x54')](_0x262382[_0x3474('0x55')][_0x3474('0x53')])[_0x3474('0x57')](0x1),'converted_format':'wav'};var _0xedc0bd=path[_0x3474('0x4a')](config[_0x3474('0x4b')],_0x3474('0x58'),_0x262382['file'][_0x3474('0x56')]);var _0x59d227=path['join'](config[_0x3474('0x4b')],_0x3474('0x4c'),util[_0x3474('0x50')](_0x3474('0x51'),path[_0x3474('0x52')](_0x262382['file'][_0x3474('0x56')],path[_0x3474('0x54')](_0x262382[_0x3474('0x55')][_0x3474('0x56')])),_0x5dbbf2['converted_format']));logger[_0x3474('0x59')](_0x3474('0x5a'),_0xedc0bd);logger['info'](_0x3474('0x5b'),_0x59d227);try{fs[_0x3474('0x5c')](_0xedc0bd,parseInt(_0x3474('0x5d'),0x8));var _0x2f62b1=sox[_0x3474('0x5e')](_0xedc0bd,_0x59d227,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x2f62b1['on']('error',function(_0x11570c){fs[_0x3474('0x5f')](_0xedc0bd);_0x2dd68d[_0x3474('0x2a')](0x1f4)[_0x3474('0x2e')](_0x11570c);});_0x2f62b1['on'](_0x3474('0x60'),function(_0xb227bc){_0x5dbbf2['original_duration']=isNaN(_0xb227bc[_0x3474('0x61')])?null:_0xb227bc['duration']*0x3e8;_0x5dbbf2[_0x3474('0x62')]=isNaN(_0xb227bc[_0x3474('0x63')])?null:_0xb227bc[_0x3474('0x63')];_0x5dbbf2[_0x3474('0x64')]=isNaN(_0xb227bc['channelCount'])?null:_0xb227bc['channelCount'];_0x5dbbf2[_0x3474('0x65')]=isNaN(_0xb227bc[_0x3474('0x66')])?null:_0xb227bc[_0x3474('0x66')];_0x5dbbf2[_0x3474('0x67')]=isNaN(_0xb227bc[_0x3474('0x68')])?null:_0xb227bc[_0x3474('0x68')];});_0x2f62b1['on']('dest',function(_0x42d43c){_0x5dbbf2[_0x3474('0x69')]=isNaN(_0x42d43c[_0x3474('0x61')])?null:_0x42d43c[_0x3474('0x61')]*0x3e8;_0x5dbbf2[_0x3474('0x6a')]=isNaN(_0x42d43c['sampleCount'])?null:_0x42d43c[_0x3474('0x63')];_0x5dbbf2[_0x3474('0x6b')]=isNaN(_0x42d43c[_0x3474('0x6c')])?null:_0x42d43c[_0x3474('0x6c')];_0x5dbbf2[_0x3474('0x6d')]=isNaN(_0x42d43c[_0x3474('0x66')])?null:_0x42d43c[_0x3474('0x66')];_0x5dbbf2[_0x3474('0x6e')]=isNaN(_0x42d43c[_0x3474('0x68')])?null:_0x42d43c[_0x3474('0x68')];});_0x2f62b1['on'](_0x3474('0x1b'),function(){fs[_0x3474('0x5c')](_0x59d227,parseInt(_0x3474('0x5d'),0x8));return db[_0x3474('0x30')][_0x3474('0x6f')](_0x5dbbf2)[_0x3474('0x29')](function(_0x381549){var _0x500364={'role':_0x262382['body'][_0x3474('0x70')],'userProfileId':_0x262382[_0x3474('0x71')]['userProfileId']};if(!_0x500364)throw new Error('Unable\x20to\x20retrieve\x20the\x20current\x20user');if(_0x500364['role']===_0x3474('0x72')){var _0x4a352d=_0x381549[_0x3474('0x26')]({'plain':!![]});return db[_0x3474('0x73')][_0x3474('0x47')]({'where':{'name':_0x3474('0x27'),'userProfileId':_0x500364[_0x3474('0x74')]},'raw':!![]})[_0x3474('0x29')](function(_0x5f06b3){if(_0x5f06b3&&_0x5f06b3[_0x3474('0x75')]===0x0){return db['UserProfileResource'][_0x3474('0x6f')]({'name':_0x4a352d['name'],'resourceId':_0x4a352d['id'],'type':_0x5f06b3[_0x3474('0x2d')],'sectionId':_0x5f06b3['id']},{})[_0x3474('0x29')](function(){return _0x381549;});}else{return _0x381549;}})[_0x3474('0x44')](function(_0x1d705f){logger[_0x3474('0x2b')](_0x3474('0x76'),_0x1d705f);throw _0x1d705f;});}return _0x381549;})['then'](respondWithResult(_0x2dd68d,null))['catch'](handleError(_0x2dd68d,null));});_0x2f62b1[_0x3474('0x77')]();}catch(_0x3336de){_0x2dd68d[_0x3474('0x2a')](0x1f4)['send'](_0x3336de);}};exports[_0x3474('0x78')]=function(_0x36514e,_0x169fcd,_0x415a6e){return db['Sound'][_0x3474('0x47')]({'where':{'id':_0x36514e[_0x3474('0x45')]['id']}})[_0x3474('0x29')](handleEntityNotFound(_0x169fcd,null))[_0x3474('0x29')](saveUpdates(_[_0x3474('0x79')](_0x36514e[_0x3474('0x71')],[_0x3474('0x2d'),_0x3474('0x7a')]),null))[_0x3474('0x29')](respondWithResult(_0x169fcd,null))[_0x3474('0x44')](handleError(_0x169fcd,null));};exports['delete']=function(_0x4bb924,_0x5650d9,_0x557867){return db[_0x3474('0x30')][_0x3474('0x47')]({'where':{'id':_0x4bb924[_0x3474('0x45')]['id']}})[_0x3474('0x29')](function(_0x14bcb6){if(_0x14bcb6){return _0x14bcb6['destroy']();}})[_0x3474('0x29')](function(_0x20ab73){var _0x5595b7=path['join'](config[_0x3474('0x4b')],'server/files/sounds/original',util[_0x3474('0x50')]('%s.%s',_0x20ab73['save_name'],_0x20ab73['original_format']));var _0xa139f0=path['join'](config['root'],'server/files/sounds/converted',util[_0x3474('0x50')](_0x3474('0x51'),_0x20ab73['save_name'],_0x20ab73['converted_format']));fs[_0x3474('0x5f')](_0x5595b7);fs[_0x3474('0x5f')](_0xa139f0);return _0x20ab73;})[_0x3474('0x29')](function(_0x242d63){if(_0x242d63){return _0x5650d9[_0x3474('0x2a')](0xcc)['end']();}})[_0x3474('0x29')](handleEntityNotFound(_0x5650d9,null))['catch'](handleError(_0x5650d9,null));};