4ac3c34a6c4533caf58548b7e114014335a5ebe9
[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 _0xa3fc=['hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','VIRTUAL','includeAll','findAll','rows','catch','params','keys','include','options','find','clone','get','omit','body','createdAt','updatedAt','create','file','originalname','download','findOne','ChatOfflineMessageId','join','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','sox','to-csv','ejs','fs-extra','lodash','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','status','json','offset','undefined','limit','count','set','Content-Range','save','update','then','destroy','end','sendStatus','error','stack','name','index','map','Attachment','rawAttributes','fieldName','type','key','query','filters','intersection','model','attributes','fields','length'];(function(_0x266618,_0x18f637){var _0x54dbf5=function(_0x1f7985){while(--_0x1f7985){_0x266618['push'](_0x266618['shift']());}};_0x54dbf5(++_0x18f637);}(_0xa3fc,0x187));var _0xca3f=function(_0x4b47b2,_0x20185f){_0x4b47b2=_0x4b47b2-0x0;var _0x49574b=_0xa3fc[_0x4b47b2];return _0x49574b;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xca3f('0x0'));var zipdir=require(_0xca3f('0x1'));var jsonpatch=require(_0xca3f('0x2'));var rp=require(_0xca3f('0x3'));var moment=require(_0xca3f('0x4'));var BPromise=require(_0xca3f('0x5'));var Mustache=require(_0xca3f('0x6'));var util=require(_0xca3f('0x7'));var path=require(_0xca3f('0x8'));var sox=require(_0xca3f('0x9'));var csv=require(_0xca3f('0xa'));var ejs=require(_0xca3f('0xb'));var fs=require('fs');var fs_extra=require(_0xca3f('0xc'));var _=require(_0xca3f('0xd'));var squel=require(_0xca3f('0xe'));var crypto=require(_0xca3f('0xf'));var jsforce=require(_0xca3f('0x10'));var deskjs=require(_0xca3f('0x11'));var toCsv=require('to-csv');var querystring=require(_0xca3f('0x12'));var Papa=require(_0xca3f('0x13'));var Redis=require('ioredis');var authService=require('../../components/auth/service');var qs=require(_0xca3f('0x14'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xca3f('0x15'));var logger=require(_0xca3f('0x16'))(_0xca3f('0x17'));var utils=require(_0xca3f('0x18'));var config=require(_0xca3f('0x19'));var licenseUtil=require(_0xca3f('0x1a'));var db=require(_0xca3f('0x1b'))['db'];function respondWithStatusCode(_0x2b21e8,_0x1c624b){_0x1c624b=_0x1c624b||0xcc;return function(_0x2cc4f0){if(_0x2cc4f0){return _0x2b21e8['sendStatus'](_0x1c624b);}return _0x2b21e8[_0xca3f('0x1c')](_0x1c624b)['end']();};}function respondWithResult(_0x435637,_0x4ff9ef){_0x4ff9ef=_0x4ff9ef||0xc8;return function(_0x581421){if(_0x581421){return _0x435637[_0xca3f('0x1c')](_0x4ff9ef)[_0xca3f('0x1d')](_0x581421);}};}function respondWithFilteredResult(_0x40af1b,_0x45c405){return function(_0x294463){if(_0x294463){var _0x533cc3=typeof _0x45c405[_0xca3f('0x1e')]===_0xca3f('0x1f')&&typeof _0x45c405[_0xca3f('0x20')]==='undefined';var _0x53e62d=_0x294463[_0xca3f('0x21')];var _0x5723ba=_0x533cc3?0x0:_0x45c405[_0xca3f('0x1e')];var _0x2b6c03=_0x533cc3?_0x294463[_0xca3f('0x21')]:_0x45c405['offset']+_0x45c405['limit'];var _0xee2898;if(_0x2b6c03>=_0x53e62d){_0x2b6c03=_0x53e62d;_0xee2898=0xc8;}else{_0xee2898=0xce;}_0x40af1b[_0xca3f('0x1c')](_0xee2898);return _0x40af1b[_0xca3f('0x22')](_0xca3f('0x23'),_0x5723ba+'-'+_0x2b6c03+'/'+_0x53e62d)[_0xca3f('0x1d')](_0x294463);}return null;};}function patchUpdates(_0x401215){return function(_0x45a9e9){try{jsonpatch['apply'](_0x45a9e9,_0x401215,!![]);}catch(_0x36cbbe){return BPromise['reject'](_0x36cbbe);}return _0x45a9e9[_0xca3f('0x24')]();};}function saveUpdates(_0x4de96,_0x31fdc8){return function(_0x2734b1){if(_0x2734b1){return _0x2734b1[_0xca3f('0x25')](_0x4de96)[_0xca3f('0x26')](function(_0x3e6a4c){return _0x3e6a4c;});}return null;};}function removeEntity(_0x360f3b,_0x3005c9){return function(_0x41babd){if(_0x41babd){return _0x41babd[_0xca3f('0x27')]()[_0xca3f('0x26')](function(){_0x360f3b[_0xca3f('0x1c')](0xcc)[_0xca3f('0x28')]();});}};}function handleEntityNotFound(_0x9a6050,_0x514728){return function(_0x532c01){if(!_0x532c01){_0x9a6050[_0xca3f('0x29')](0x194);}return _0x532c01;};}function handleError(_0x160b40,_0x88acdb){_0x88acdb=_0x88acdb||0x1f4;return function(_0x254d1d){logger[_0xca3f('0x2a')](_0x254d1d[_0xca3f('0x2b')]);if(_0x254d1d['name']){delete _0x254d1d[_0xca3f('0x2c')];}_0x160b40['status'](_0x88acdb)['send'](_0x254d1d);};}exports[_0xca3f('0x2d')]=function(_0x57f3e4,_0x207145){var _0x3ae11f={},_0x33bc1b={},_0x303563={'count':0x0,'rows':[]};var _0x1e84f4=_[_0xca3f('0x2e')](db[_0xca3f('0x2f')][_0xca3f('0x30')],function(_0x128a87){return{'name':_0x128a87[_0xca3f('0x31')],'type':_0x128a87[_0xca3f('0x32')][_0xca3f('0x33')]};});_0x33bc1b['model']=_['map'](_0x1e84f4,_0xca3f('0x2c'));_0x33bc1b['query']=_['keys'](_0x57f3e4[_0xca3f('0x34')]);_0x33bc1b[_0xca3f('0x35')]=_[_0xca3f('0x36')](_0x33bc1b[_0xca3f('0x37')],_0x33bc1b[_0xca3f('0x34')]);_0x3ae11f[_0xca3f('0x38')]=_[_0xca3f('0x36')](_0x33bc1b['model'],qs[_0xca3f('0x39')](_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x39')]));_0x3ae11f['attributes']=_0x3ae11f[_0xca3f('0x38')][_0xca3f('0x3a')]?_0x3ae11f[_0xca3f('0x38')]:_0x33bc1b[_0xca3f('0x37')];if(!_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x3b')](_0xca3f('0x3c'))){_0x3ae11f[_0xca3f('0x20')]=qs['limit'](_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x20')]);_0x3ae11f[_0xca3f('0x1e')]=qs[_0xca3f('0x1e')](_0x57f3e4[_0xca3f('0x34')]['offset']);}_0x3ae11f[_0xca3f('0x3d')]=qs[_0xca3f('0x3e')](_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x3e')]);_0x3ae11f[_0xca3f('0x3f')]=qs[_0xca3f('0x35')](_[_0xca3f('0x40')](_0x57f3e4[_0xca3f('0x34')],_0x33bc1b[_0xca3f('0x35')]),_0x1e84f4);if(_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x41')]){_0x3ae11f[_0xca3f('0x3f')]=_[_0xca3f('0x42')](_0x3ae11f[_0xca3f('0x3f')],{'$or':_[_0xca3f('0x2e')](_0x1e84f4,function(_0x588fbd){if(_0x588fbd['type']!==_0xca3f('0x43')){var _0x4f1a6d={};_0x4f1a6d[_0x588fbd[_0xca3f('0x2c')]]={'$like':'%'+_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x41')]+'%'};return _0x4f1a6d;}})});}_0x3ae11f=_['merge']({},_0x3ae11f,_0x57f3e4['options']);var _0x28c32e={'where':_0x3ae11f[_0xca3f('0x3f')]};return db[_0xca3f('0x2f')][_0xca3f('0x21')](_0x28c32e)[_0xca3f('0x26')](function(_0x49d597){_0x303563[_0xca3f('0x21')]=_0x49d597;if(_0x57f3e4[_0xca3f('0x34')][_0xca3f('0x44')]){_0x3ae11f['include']=[{'all':!![]}];}return db[_0xca3f('0x2f')][_0xca3f('0x45')](_0x3ae11f);})[_0xca3f('0x26')](function(_0x4cb430){_0x303563[_0xca3f('0x46')]=_0x4cb430;return _0x303563;})[_0xca3f('0x26')](respondWithFilteredResult(_0x207145,_0x3ae11f))[_0xca3f('0x47')](handleError(_0x207145,null));};exports['show']=function(_0x3e9f48,_0x1a91f3){var _0x2dc8dc={'raw':!![],'where':{'id':_0x3e9f48[_0xca3f('0x48')]['id']}},_0x1b897c={};_0x1b897c[_0xca3f('0x37')]=_[_0xca3f('0x49')](db['Attachment'][_0xca3f('0x30')]);_0x1b897c[_0xca3f('0x34')]=_[_0xca3f('0x49')](_0x3e9f48[_0xca3f('0x34')]);_0x1b897c[_0xca3f('0x35')]=_['intersection'](_0x1b897c[_0xca3f('0x37')],_0x1b897c[_0xca3f('0x34')]);_0x2dc8dc[_0xca3f('0x38')]=_['intersection'](_0x1b897c[_0xca3f('0x37')],qs[_0xca3f('0x39')](_0x3e9f48[_0xca3f('0x34')][_0xca3f('0x39')]));_0x2dc8dc['attributes']=_0x2dc8dc[_0xca3f('0x38')]['length']?_0x2dc8dc[_0xca3f('0x38')]:_0x1b897c[_0xca3f('0x37')];if(_0x3e9f48['query'][_0xca3f('0x44')]){_0x2dc8dc[_0xca3f('0x4a')]=[{'all':!![]}];}_0x2dc8dc=_[_0xca3f('0x42')]({},_0x2dc8dc,_0x3e9f48[_0xca3f('0x4b')]);return db['Attachment'][_0xca3f('0x4c')](_0x2dc8dc)[_0xca3f('0x26')](handleEntityNotFound(_0x1a91f3,null))['then'](respondWithResult(_0x1a91f3,null))[_0xca3f('0x47')](handleError(_0x1a91f3,null));};exports[_0xca3f('0x4d')]=function(_0x2f22e2,_0x27bc5c){var _0x14f68d={'where':{'id':_0x2f22e2[_0xca3f('0x48')]['id']}},_0x54b1b4={};_0x54b1b4[_0xca3f('0x37')]=_[_0xca3f('0x49')](db[_0xca3f('0x2f')][_0xca3f('0x30')]);_0x14f68d['attributes']=_[_0xca3f('0x36')](_0x54b1b4[_0xca3f('0x37')],qs[_0xca3f('0x39')](_0x2f22e2['query'][_0xca3f('0x39')]));_0x14f68d[_0xca3f('0x38')]=_0x14f68d[_0xca3f('0x38')][_0xca3f('0x3a')]?_0x14f68d['attributes']:_0x54b1b4[_0xca3f('0x37')];if(_0x2f22e2[_0xca3f('0x34')][_0xca3f('0x44')]){_0x14f68d[_0xca3f('0x4a')]=[{'all':!![]}];}_0x14f68d=_[_0xca3f('0x42')]({},_0x14f68d,_0x2f22e2[_0xca3f('0x4b')]);return db['Attachment'][_0xca3f('0x4c')](_0x14f68d)[_0xca3f('0x26')](handleEntityNotFound(_0x27bc5c,null))[_0xca3f('0x26')](function(_0x3f8dfb){if(_0x3f8dfb){var _0x4967f1=_0x3f8dfb[_0xca3f('0x4e')]({'plain':!![]});_0x4967f1=qs['omit'](_0x4967f1,['id','createdAt','updatedAt']);_0x2f22e2['body']=_[_0xca3f('0x4f')](_0x2f22e2[_0xca3f('0x50')],['id',_0xca3f('0x51'),_0xca3f('0x52')]);return db[_0xca3f('0x2f')]['create'](_[_0xca3f('0x42')](_0x4967f1,_0x2f22e2[_0xca3f('0x50')]),{'include':_0x2f22e2[_0xca3f('0x34')]['includeAll']?[{'all':!![]}]:undefined});}})['then'](respondWithResult(_0x27bc5c,0xc9))['catch'](handleError(_0x27bc5c,null));};exports[_0xca3f('0x25')]=function(_0x51142b,_0x16bca1){if(_0x51142b[_0xca3f('0x50')]['id']){delete _0x51142b['body']['id'];}return db[_0xca3f('0x2f')]['find']({'where':{'id':_0x51142b[_0xca3f('0x48')]['id']}})[_0xca3f('0x26')](handleEntityNotFound(_0x16bca1,null))['then'](saveUpdates(_0x51142b[_0xca3f('0x50')],null))['then'](respondWithResult(_0x16bca1,null))[_0xca3f('0x47')](handleError(_0x16bca1,null));};exports[_0xca3f('0x27')]=function(_0x568b58,_0x4b4f2c){return db[_0xca3f('0x2f')][_0xca3f('0x4c')]({'where':{'id':_0x568b58[_0xca3f('0x48')]['id']}})[_0xca3f('0x26')](handleEntityNotFound(_0x4b4f2c,null))[_0xca3f('0x26')](removeEntity(_0x4b4f2c,null))[_0xca3f('0x47')](handleError(_0x4b4f2c,null));};exports[_0xca3f('0x53')]=function(_0x141ded,_0x7a7490,_0x27f6d7){_0x141ded[_0xca3f('0x50')][_0xca3f('0x2c')]=_0x141ded[_0xca3f('0x54')][_0xca3f('0x55')];_0x141ded['body']['basename']=_0x141ded['file']['filename'];_0x141ded[_0xca3f('0x50')][_0xca3f('0x32')]=_0x141ded['file']['mimetype'];return db['Attachment'][_0xca3f('0x53')](_0x141ded[_0xca3f('0x50')])[_0xca3f('0x26')](respondWithResult(_0x7a7490,0xc9))['catch'](handleError(_0x7a7490,null));};exports[_0xca3f('0x56')]=function(_0x5d3f2f,_0x3e3bb8,_0x15c9b6){return db[_0xca3f('0x2f')][_0xca3f('0x57')]({'where':{'id':_0x5d3f2f['params']['id']},'raw':!![]})[_0xca3f('0x26')](handleEntityNotFound(_0x3e3bb8,null))[_0xca3f('0x26')](function(_0x48bde4){if(_0x48bde4){var _0x29ac05=_0x48bde4[_0xca3f('0x58')]!==null;var _0x32412b=path[_0xca3f('0x59')](config['root'],'server/files/attachments/');if(_0x29ac05){_0x32412b=path[_0xca3f('0x59')](_0x32412b,'offline-chat/');}var _0x227787=path[_0xca3f('0x59')](_0x32412b,_0x48bde4['basename']);return _0x3e3bb8[_0xca3f('0x56')](_0x227787);}})[_0xca3f('0x47')](handleError(_0x3e3bb8,null));};