Built motion from commit b5413ace.|2.6.9
[motion2.git] / server / api / attachment / attachment.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 _0xc48b=['update','then','destroy','end','error','name','send','index','Attachment','rawAttributes','fieldName','key','model','map','query','keys','filters','intersection','attributes','fields','hasOwnProperty','nolimit','order','sort','where','pick','merge','type','VIRTUAL','options','includeAll','include','catch','show','params','length','clone','find','get','omit','createdAt','body','create','file','originalname','filename','mimetype','findOne','root','server/files/attachments/','join','offline-chat/','basename','download','eml-format','rimraf','zip-dir','request-promise','moment','mustache','util','path','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','ioredis','../../components/auth/service','../../components/parsers/qs','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','sendStatus','status','json','offset','limit','undefined','count','set','apply','save'];(function(_0x45313f,_0x46caf4){var _0x402910=function(_0x4883d8){while(--_0x4883d8){_0x45313f['push'](_0x45313f['shift']());}};_0x402910(++_0x46caf4);}(_0xc48b,0x91));var _0xbc48=function(_0x519228,_0x2bcbc2){_0x519228=_0x519228-0x0;var _0xc3c605=_0xc48b[_0x519228];return _0xc3c605;};'use strict';var emlformat=require(_0xbc48('0x0'));var rimraf=require(_0xbc48('0x1'));var zipdir=require(_0xbc48('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0xbc48('0x3'));var moment=require(_0xbc48('0x4'));var BPromise=require('bluebird');var Mustache=require(_0xbc48('0x5'));var util=require(_0xbc48('0x6'));var path=require(_0xbc48('0x7'));var sox=require('sox');var csv=require(_0xbc48('0x8'));var ejs=require(_0xbc48('0x9'));var fs=require('fs');var fs_extra=require(_0xbc48('0xa'));var _=require(_0xbc48('0xb'));var squel=require(_0xbc48('0xc'));var crypto=require(_0xbc48('0xd'));var jsforce=require(_0xbc48('0xe'));var deskjs=require(_0xbc48('0xf'));var toCsv=require(_0xbc48('0x8'));var querystring=require(_0xbc48('0x10'));var Papa=require('papaparse');var Redis=require(_0xbc48('0x11'));var authService=require(_0xbc48('0x12'));var qs=require(_0xbc48('0x13'));var as=require(_0xbc48('0x14'));var hardwareService=require(_0xbc48('0x15'));var logger=require(_0xbc48('0x16'))(_0xbc48('0x17'));var utils=require(_0xbc48('0x18'));var config=require(_0xbc48('0x19'));var licenseUtil=require(_0xbc48('0x1a'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x292420,_0x19bc6b){_0x19bc6b=_0x19bc6b||0xcc;return function(_0x1ddb11){if(_0x1ddb11){return _0x292420[_0xbc48('0x1b')](_0x19bc6b);}return _0x292420[_0xbc48('0x1c')](_0x19bc6b)['end']();};}function respondWithResult(_0x41954a,_0x2aac5a){_0x2aac5a=_0x2aac5a||0xc8;return function(_0x543918){if(_0x543918){return _0x41954a[_0xbc48('0x1c')](_0x2aac5a)[_0xbc48('0x1d')](_0x543918);}};}function respondWithFilteredResult(_0x21890a,_0x5654b3){return function(_0x1d1e0b){if(_0x1d1e0b){var _0x2e8764=typeof _0x5654b3[_0xbc48('0x1e')]==='undefined'&&typeof _0x5654b3[_0xbc48('0x1f')]===_0xbc48('0x20');var _0x5d6cd9=_0x1d1e0b[_0xbc48('0x21')];var _0x866bd2=_0x2e8764?0x0:_0x5654b3[_0xbc48('0x1e')];var _0x23a335=_0x2e8764?_0x1d1e0b[_0xbc48('0x21')]:_0x5654b3[_0xbc48('0x1e')]+_0x5654b3[_0xbc48('0x1f')];var _0x1a05c0;if(_0x23a335>=_0x5d6cd9){_0x23a335=_0x5d6cd9;_0x1a05c0=0xc8;}else{_0x1a05c0=0xce;}_0x21890a[_0xbc48('0x1c')](_0x1a05c0);return _0x21890a[_0xbc48('0x22')]('Content-Range',_0x866bd2+'-'+_0x23a335+'/'+_0x5d6cd9)[_0xbc48('0x1d')](_0x1d1e0b);}return null;};}function patchUpdates(_0x2ab5af){return function(_0x121336){try{jsonpatch[_0xbc48('0x23')](_0x121336,_0x2ab5af,!![]);}catch(_0x2c68d6){return BPromise['reject'](_0x2c68d6);}return _0x121336[_0xbc48('0x24')]();};}function saveUpdates(_0x34f585,_0x17aa00){return function(_0x5f0f1c){if(_0x5f0f1c){return _0x5f0f1c[_0xbc48('0x25')](_0x34f585)[_0xbc48('0x26')](function(_0x2d9d86){return _0x2d9d86;});}return null;};}function removeEntity(_0x59b8bd,_0x47eaa6){return function(_0x3dc990){if(_0x3dc990){return _0x3dc990[_0xbc48('0x27')]()[_0xbc48('0x26')](function(){_0x59b8bd[_0xbc48('0x1c')](0xcc)[_0xbc48('0x28')]();});}};}function handleEntityNotFound(_0x4c3a66,_0x10d28f){return function(_0xbd53a5){if(!_0xbd53a5){_0x4c3a66['sendStatus'](0x194);}return _0xbd53a5;};}function handleError(_0x4ba854,_0x30c9b1){_0x30c9b1=_0x30c9b1||0x1f4;return function(_0x16c6c2){logger[_0xbc48('0x29')](_0x16c6c2['stack']);if(_0x16c6c2['name']){delete _0x16c6c2[_0xbc48('0x2a')];}_0x4ba854[_0xbc48('0x1c')](_0x30c9b1)[_0xbc48('0x2b')](_0x16c6c2);};}exports[_0xbc48('0x2c')]=function(_0x1e09ce,_0x7182b9){var _0x47ecdc={},_0x228842={},_0x18a6e3={'count':0x0,'rows':[]};var _0x20ee42=_['map'](db[_0xbc48('0x2d')][_0xbc48('0x2e')],function(_0x21de0b){return{'name':_0x21de0b[_0xbc48('0x2f')],'type':_0x21de0b['type'][_0xbc48('0x30')]};});_0x228842[_0xbc48('0x31')]=_[_0xbc48('0x32')](_0x20ee42,_0xbc48('0x2a'));_0x228842[_0xbc48('0x33')]=_[_0xbc48('0x34')](_0x1e09ce['query']);_0x228842[_0xbc48('0x35')]=_[_0xbc48('0x36')](_0x228842['model'],_0x228842[_0xbc48('0x33')]);_0x47ecdc[_0xbc48('0x37')]=_[_0xbc48('0x36')](_0x228842[_0xbc48('0x31')],qs[_0xbc48('0x38')](_0x1e09ce[_0xbc48('0x33')][_0xbc48('0x38')]));_0x47ecdc[_0xbc48('0x37')]=_0x47ecdc[_0xbc48('0x37')]['length']?_0x47ecdc[_0xbc48('0x37')]:_0x228842[_0xbc48('0x31')];if(!_0x1e09ce[_0xbc48('0x33')][_0xbc48('0x39')](_0xbc48('0x3a'))){_0x47ecdc[_0xbc48('0x1f')]=qs['limit'](_0x1e09ce[_0xbc48('0x33')][_0xbc48('0x1f')]);_0x47ecdc[_0xbc48('0x1e')]=qs['offset'](_0x1e09ce[_0xbc48('0x33')]['offset']);}_0x47ecdc[_0xbc48('0x3b')]=qs['sort'](_0x1e09ce['query'][_0xbc48('0x3c')]);_0x47ecdc[_0xbc48('0x3d')]=qs[_0xbc48('0x35')](_[_0xbc48('0x3e')](_0x1e09ce[_0xbc48('0x33')],_0x228842[_0xbc48('0x35')]),_0x20ee42);if(_0x1e09ce[_0xbc48('0x33')]['filter']){_0x47ecdc[_0xbc48('0x3d')]=_[_0xbc48('0x3f')](_0x47ecdc[_0xbc48('0x3d')],{'$or':_[_0xbc48('0x32')](_0x20ee42,function(_0x5106e4){if(_0x5106e4[_0xbc48('0x40')]!==_0xbc48('0x41')){var _0x1bb4c4={};_0x1bb4c4[_0x5106e4[_0xbc48('0x2a')]]={'$like':'%'+_0x1e09ce[_0xbc48('0x33')]['filter']+'%'};return _0x1bb4c4;}})});}_0x47ecdc=_[_0xbc48('0x3f')]({},_0x47ecdc,_0x1e09ce[_0xbc48('0x42')]);var _0x522692={'where':_0x47ecdc[_0xbc48('0x3d')]};return db[_0xbc48('0x2d')][_0xbc48('0x21')](_0x522692)[_0xbc48('0x26')](function(_0x39a86c){_0x18a6e3['count']=_0x39a86c;if(_0x1e09ce[_0xbc48('0x33')][_0xbc48('0x43')]){_0x47ecdc[_0xbc48('0x44')]=[{'all':!![]}];}return db['Attachment']['findAll'](_0x47ecdc);})[_0xbc48('0x26')](function(_0x1dd524){_0x18a6e3['rows']=_0x1dd524;return _0x18a6e3;})[_0xbc48('0x26')](respondWithFilteredResult(_0x7182b9,_0x47ecdc))[_0xbc48('0x45')](handleError(_0x7182b9,null));};exports[_0xbc48('0x46')]=function(_0x4fe7df,_0x93166c){var _0x4b35aa={'raw':!![],'where':{'id':_0x4fe7df[_0xbc48('0x47')]['id']}},_0x208a5c={};_0x208a5c['model']=_[_0xbc48('0x34')](db['Attachment'][_0xbc48('0x2e')]);_0x208a5c['query']=_[_0xbc48('0x34')](_0x4fe7df[_0xbc48('0x33')]);_0x208a5c['filters']=_[_0xbc48('0x36')](_0x208a5c['model'],_0x208a5c[_0xbc48('0x33')]);_0x4b35aa['attributes']=_[_0xbc48('0x36')](_0x208a5c['model'],qs[_0xbc48('0x38')](_0x4fe7df[_0xbc48('0x33')][_0xbc48('0x38')]));_0x4b35aa[_0xbc48('0x37')]=_0x4b35aa[_0xbc48('0x37')][_0xbc48('0x48')]?_0x4b35aa[_0xbc48('0x37')]:_0x208a5c[_0xbc48('0x31')];if(_0x4fe7df[_0xbc48('0x33')]['includeAll']){_0x4b35aa[_0xbc48('0x44')]=[{'all':!![]}];}_0x4b35aa=_[_0xbc48('0x3f')]({},_0x4b35aa,_0x4fe7df[_0xbc48('0x42')]);return db[_0xbc48('0x2d')]['find'](_0x4b35aa)[_0xbc48('0x26')](handleEntityNotFound(_0x93166c,null))[_0xbc48('0x26')](respondWithResult(_0x93166c,null))['catch'](handleError(_0x93166c,null));};exports[_0xbc48('0x49')]=function(_0x33e370,_0x34c6a9){var _0x5ab517={'where':{'id':_0x33e370[_0xbc48('0x47')]['id']}},_0x1a3f09={};_0x1a3f09[_0xbc48('0x31')]=_[_0xbc48('0x34')](db['Attachment'][_0xbc48('0x2e')]);_0x5ab517[_0xbc48('0x37')]=_[_0xbc48('0x36')](_0x1a3f09['model'],qs[_0xbc48('0x38')](_0x33e370['query'][_0xbc48('0x38')]));_0x5ab517[_0xbc48('0x37')]=_0x5ab517[_0xbc48('0x37')][_0xbc48('0x48')]?_0x5ab517[_0xbc48('0x37')]:_0x1a3f09[_0xbc48('0x31')];if(_0x33e370['query'][_0xbc48('0x43')]){_0x5ab517[_0xbc48('0x44')]=[{'all':!![]}];}_0x5ab517=_['merge']({},_0x5ab517,_0x33e370[_0xbc48('0x42')]);return db['Attachment'][_0xbc48('0x4a')](_0x5ab517)[_0xbc48('0x26')](handleEntityNotFound(_0x34c6a9,null))[_0xbc48('0x26')](function(_0x4239cc){if(_0x4239cc){var _0x3e147c=_0x4239cc[_0xbc48('0x4b')]({'plain':!![]});_0x3e147c=qs[_0xbc48('0x4c')](_0x3e147c,['id',_0xbc48('0x4d'),'updatedAt']);_0x33e370['body']=_[_0xbc48('0x4c')](_0x33e370[_0xbc48('0x4e')],['id',_0xbc48('0x4d'),'updatedAt']);return db[_0xbc48('0x2d')][_0xbc48('0x4f')](_['merge'](_0x3e147c,_0x33e370['body']),{'include':_0x33e370[_0xbc48('0x33')]['includeAll']?[{'all':!![]}]:undefined});}})[_0xbc48('0x26')](respondWithResult(_0x34c6a9,0xc9))['catch'](handleError(_0x34c6a9,null));};exports['update']=function(_0x41c0d7,_0x43520a){if(_0x41c0d7[_0xbc48('0x4e')]['id']){delete _0x41c0d7['body']['id'];}return db[_0xbc48('0x2d')]['find']({'where':{'id':_0x41c0d7[_0xbc48('0x47')]['id']}})[_0xbc48('0x26')](handleEntityNotFound(_0x43520a,null))[_0xbc48('0x26')](saveUpdates(_0x41c0d7['body'],null))[_0xbc48('0x26')](respondWithResult(_0x43520a,null))[_0xbc48('0x45')](handleError(_0x43520a,null));};exports['destroy']=function(_0x3cac65,_0x217fb4){return db[_0xbc48('0x2d')]['find']({'where':{'id':_0x3cac65['params']['id']}})['then'](handleEntityNotFound(_0x217fb4,null))[_0xbc48('0x26')](removeEntity(_0x217fb4,null))[_0xbc48('0x45')](handleError(_0x217fb4,null));};exports[_0xbc48('0x4f')]=function(_0xe4b9c2,_0x230f2b,_0x50646c){_0xe4b9c2[_0xbc48('0x4e')]['name']=_0xe4b9c2[_0xbc48('0x50')][_0xbc48('0x51')];_0xe4b9c2[_0xbc48('0x4e')]['basename']=_0xe4b9c2[_0xbc48('0x50')][_0xbc48('0x52')];_0xe4b9c2[_0xbc48('0x4e')][_0xbc48('0x40')]=_0xe4b9c2[_0xbc48('0x50')][_0xbc48('0x53')];return db[_0xbc48('0x2d')][_0xbc48('0x4f')](_0xe4b9c2[_0xbc48('0x4e')])[_0xbc48('0x26')](respondWithResult(_0x230f2b,0xc9))[_0xbc48('0x45')](handleError(_0x230f2b,null));};exports['download']=function(_0x57e8bd,_0x245127,_0x3df221){return db[_0xbc48('0x2d')][_0xbc48('0x54')]({'where':{'id':_0x57e8bd[_0xbc48('0x47')]['id']},'raw':!![]})[_0xbc48('0x26')](handleEntityNotFound(_0x245127,null))[_0xbc48('0x26')](function(_0x54715e){if(_0x54715e){var _0x527b17=_0x54715e['ChatOfflineMessageId']!==null;var _0x5e7a8e=path['join'](config[_0xbc48('0x55')],_0xbc48('0x56'));if(_0x527b17){_0x5e7a8e=path[_0xbc48('0x57')](_0x5e7a8e,_0xbc48('0x58'));}var _0x50e600=path[_0xbc48('0x57')](_0x5e7a8e,_0x54715e[_0xbc48('0x59')]);return _0x245127[_0xbc48('0x5a')](_0x50e600);}})['catch'](handleError(_0x245127,null));};