ea9fd3cc0e169e813d7ea46eda1dfc80f32579b2
[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 _0xa2e0=['send','Attachment','rawAttributes','type','key','query','filters','model','attributes','intersection','fields','length','order','sort','where','pick','filter','map','VIRTUAL','merge','options','include','rows','catch','show','params','keys','includeAll','find','clone','get','omit','createdAt','updatedAt','body','create','originalname','basename','filename','file','mimetype','download','findOne','join','root','server/files/attachments/','eml-format','rimraf','zip-dir','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','squel','crypto','jsforce','desk.js','querystring','papaparse','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../config/license/util','../../mysqldb','sendStatus','status','json','offset','undefined','count','limit','apply','reject','save','update','then','destroy','error','name'];(function(_0x2bfc73,_0x4b5079){var _0x4c88a9=function(_0x5e87d5){while(--_0x5e87d5){_0x2bfc73['push'](_0x2bfc73['shift']());}};_0x4c88a9(++_0x4b5079);}(_0xa2e0,0xde));var _0x0a2e=function(_0x2ae0ec,_0x1c8bea){_0x2ae0ec=_0x2ae0ec-0x0;var _0x5f1592=_0xa2e0[_0x2ae0ec];return _0x5f1592;};'use strict';var emlformat=require(_0x0a2e('0x0'));var rimraf=require(_0x0a2e('0x1'));var zipdir=require(_0x0a2e('0x2'));var jsonpatch=require('fast-json-patch');var rp=require(_0x0a2e('0x3'));var moment=require(_0x0a2e('0x4'));var BPromise=require(_0x0a2e('0x5'));var Mustache=require(_0x0a2e('0x6'));var util=require(_0x0a2e('0x7'));var path=require(_0x0a2e('0x8'));var sox=require('sox');var csv=require(_0x0a2e('0x9'));var ejs=require(_0x0a2e('0xa'));var fs=require('fs');var fs_extra=require(_0x0a2e('0xb'));var _=require('lodash');var squel=require(_0x0a2e('0xc'));var crypto=require(_0x0a2e('0xd'));var jsforce=require(_0x0a2e('0xe'));var deskjs=require(_0x0a2e('0xf'));var toCsv=require(_0x0a2e('0x9'));var querystring=require(_0x0a2e('0x10'));var Papa=require(_0x0a2e('0x11'));var Redis=require('ioredis');var authService=require(_0x0a2e('0x12'));var qs=require(_0x0a2e('0x13'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0x0a2e('0x14'));var logger=require(_0x0a2e('0x15'))(_0x0a2e('0x16'));var utils=require(_0x0a2e('0x17'));var config=require(_0x0a2e('0x18'));var licenseUtil=require(_0x0a2e('0x19'));var db=require(_0x0a2e('0x1a'))['db'];function respondWithStatusCode(_0x216316,_0x3985bc){_0x3985bc=_0x3985bc||0xcc;return function(_0x573a4f){if(_0x573a4f){return _0x216316[_0x0a2e('0x1b')](_0x3985bc);}return _0x216316['status'](_0x3985bc)['end']();};}function respondWithResult(_0x3a5b59,_0x4003b7){_0x4003b7=_0x4003b7||0xc8;return function(_0x3da0fc){if(_0x3da0fc){return _0x3a5b59[_0x0a2e('0x1c')](_0x4003b7)[_0x0a2e('0x1d')](_0x3da0fc);}};}function respondWithFilteredResult(_0x3ff605,_0x51319f){return function(_0x30d625){if(_0x30d625){var _0x48eaf7=typeof _0x51319f[_0x0a2e('0x1e')]===_0x0a2e('0x1f')&&typeof _0x51319f['limit']==='undefined';var _0x48c2d4=_0x30d625['count'];var _0x3b8867=_0x48eaf7?0x0:_0x51319f[_0x0a2e('0x1e')];var _0x204895=_0x48eaf7?_0x30d625[_0x0a2e('0x20')]:_0x51319f[_0x0a2e('0x1e')]+_0x51319f[_0x0a2e('0x21')];var _0x56b613;if(_0x204895>=_0x48c2d4){_0x204895=_0x48c2d4;_0x56b613=0xc8;}else{_0x56b613=0xce;}_0x3ff605['status'](_0x56b613);return _0x3ff605['set']('Content-Range',_0x3b8867+'-'+_0x204895+'/'+_0x48c2d4)[_0x0a2e('0x1d')](_0x30d625);}return null;};}function patchUpdates(_0x3e5dd9){return function(_0x3ae22b){try{jsonpatch[_0x0a2e('0x22')](_0x3ae22b,_0x3e5dd9,!![]);}catch(_0x24f86e){return BPromise[_0x0a2e('0x23')](_0x24f86e);}return _0x3ae22b[_0x0a2e('0x24')]();};}function saveUpdates(_0x3d2a87,_0x12c1ac){return function(_0x2b713f){if(_0x2b713f){return _0x2b713f[_0x0a2e('0x25')](_0x3d2a87)[_0x0a2e('0x26')](function(_0x1a2ff4){return _0x1a2ff4;});}return null;};}function removeEntity(_0x10a284,_0x4c78e7){return function(_0x5d9e1c){if(_0x5d9e1c){return _0x5d9e1c[_0x0a2e('0x27')]()[_0x0a2e('0x26')](function(){_0x10a284[_0x0a2e('0x1c')](0xcc)['end']();});}};}function handleEntityNotFound(_0x526b19,_0x3814f5){return function(_0x568edc){if(!_0x568edc){_0x526b19[_0x0a2e('0x1b')](0x194);}return _0x568edc;};}function handleError(_0x1d8d41,_0x247539){_0x247539=_0x247539||0x1f4;return function(_0x501381){logger[_0x0a2e('0x28')](_0x501381['stack']);if(_0x501381[_0x0a2e('0x29')]){delete _0x501381[_0x0a2e('0x29')];}_0x1d8d41['status'](_0x247539)[_0x0a2e('0x2a')](_0x501381);};}exports['index']=function(_0x5994e2,_0x294735){var _0x2172fc={},_0x4ff44c={},_0x570174={'count':0x0,'rows':[]};var _0x40a72e=_['map'](db[_0x0a2e('0x2b')][_0x0a2e('0x2c')],function(_0x22107b){return{'name':_0x22107b['fieldName'],'type':_0x22107b[_0x0a2e('0x2d')][_0x0a2e('0x2e')]};});_0x4ff44c['model']=_['map'](_0x40a72e,_0x0a2e('0x29'));_0x4ff44c[_0x0a2e('0x2f')]=_['keys'](_0x5994e2['query']);_0x4ff44c[_0x0a2e('0x30')]=_['intersection'](_0x4ff44c[_0x0a2e('0x31')],_0x4ff44c[_0x0a2e('0x2f')]);_0x2172fc[_0x0a2e('0x32')]=_[_0x0a2e('0x33')](_0x4ff44c[_0x0a2e('0x31')],qs['fields'](_0x5994e2[_0x0a2e('0x2f')][_0x0a2e('0x34')]));_0x2172fc[_0x0a2e('0x32')]=_0x2172fc[_0x0a2e('0x32')][_0x0a2e('0x35')]?_0x2172fc[_0x0a2e('0x32')]:_0x4ff44c[_0x0a2e('0x31')];if(!_0x5994e2[_0x0a2e('0x2f')]['hasOwnProperty']('nolimit')){_0x2172fc[_0x0a2e('0x21')]=qs['limit'](_0x5994e2[_0x0a2e('0x2f')][_0x0a2e('0x21')]);_0x2172fc[_0x0a2e('0x1e')]=qs['offset'](_0x5994e2[_0x0a2e('0x2f')][_0x0a2e('0x1e')]);}_0x2172fc[_0x0a2e('0x36')]=qs['sort'](_0x5994e2[_0x0a2e('0x2f')][_0x0a2e('0x37')]);_0x2172fc[_0x0a2e('0x38')]=qs[_0x0a2e('0x30')](_[_0x0a2e('0x39')](_0x5994e2[_0x0a2e('0x2f')],_0x4ff44c[_0x0a2e('0x30')]),_0x40a72e);if(_0x5994e2[_0x0a2e('0x2f')][_0x0a2e('0x3a')]){_0x2172fc[_0x0a2e('0x38')]=_['merge'](_0x2172fc['where'],{'$or':_[_0x0a2e('0x3b')](_0x40a72e,function(_0x48f1ec){if(_0x48f1ec[_0x0a2e('0x2d')]!==_0x0a2e('0x3c')){var _0x185fae={};_0x185fae[_0x48f1ec[_0x0a2e('0x29')]]={'$like':'%'+_0x5994e2[_0x0a2e('0x2f')]['filter']+'%'};return _0x185fae;}})});}_0x2172fc=_[_0x0a2e('0x3d')]({},_0x2172fc,_0x5994e2[_0x0a2e('0x3e')]);var _0x500d56={'where':_0x2172fc['where']};return db[_0x0a2e('0x2b')][_0x0a2e('0x20')](_0x500d56)[_0x0a2e('0x26')](function(_0x9f37b0){_0x570174[_0x0a2e('0x20')]=_0x9f37b0;if(_0x5994e2[_0x0a2e('0x2f')]['includeAll']){_0x2172fc[_0x0a2e('0x3f')]=[{'all':!![]}];}return db['Attachment']['findAll'](_0x2172fc);})[_0x0a2e('0x26')](function(_0x10c025){_0x570174[_0x0a2e('0x40')]=_0x10c025;return _0x570174;})[_0x0a2e('0x26')](respondWithFilteredResult(_0x294735,_0x2172fc))[_0x0a2e('0x41')](handleError(_0x294735,null));};exports[_0x0a2e('0x42')]=function(_0x21b4eb,_0x335a5c){var _0x2baa5c={'raw':!![],'where':{'id':_0x21b4eb[_0x0a2e('0x43')]['id']}},_0x2ff1a1={};_0x2ff1a1['model']=_[_0x0a2e('0x44')](db[_0x0a2e('0x2b')][_0x0a2e('0x2c')]);_0x2ff1a1[_0x0a2e('0x2f')]=_[_0x0a2e('0x44')](_0x21b4eb[_0x0a2e('0x2f')]);_0x2ff1a1[_0x0a2e('0x30')]=_[_0x0a2e('0x33')](_0x2ff1a1['model'],_0x2ff1a1[_0x0a2e('0x2f')]);_0x2baa5c[_0x0a2e('0x32')]=_[_0x0a2e('0x33')](_0x2ff1a1[_0x0a2e('0x31')],qs[_0x0a2e('0x34')](_0x21b4eb[_0x0a2e('0x2f')][_0x0a2e('0x34')]));_0x2baa5c['attributes']=_0x2baa5c[_0x0a2e('0x32')][_0x0a2e('0x35')]?_0x2baa5c[_0x0a2e('0x32')]:_0x2ff1a1['model'];if(_0x21b4eb[_0x0a2e('0x2f')][_0x0a2e('0x45')]){_0x2baa5c[_0x0a2e('0x3f')]=[{'all':!![]}];}_0x2baa5c=_[_0x0a2e('0x3d')]({},_0x2baa5c,_0x21b4eb['options']);return db[_0x0a2e('0x2b')][_0x0a2e('0x46')](_0x2baa5c)[_0x0a2e('0x26')](handleEntityNotFound(_0x335a5c,null))['then'](respondWithResult(_0x335a5c,null))[_0x0a2e('0x41')](handleError(_0x335a5c,null));};exports[_0x0a2e('0x47')]=function(_0x5097cc,_0x3c1601){var _0x3ff6ad={'where':{'id':_0x5097cc[_0x0a2e('0x43')]['id']}},_0x5c8e3d={};_0x5c8e3d[_0x0a2e('0x31')]=_[_0x0a2e('0x44')](db[_0x0a2e('0x2b')]['rawAttributes']);_0x3ff6ad['attributes']=_['intersection'](_0x5c8e3d['model'],qs[_0x0a2e('0x34')](_0x5097cc[_0x0a2e('0x2f')][_0x0a2e('0x34')]));_0x3ff6ad[_0x0a2e('0x32')]=_0x3ff6ad[_0x0a2e('0x32')][_0x0a2e('0x35')]?_0x3ff6ad[_0x0a2e('0x32')]:_0x5c8e3d[_0x0a2e('0x31')];if(_0x5097cc[_0x0a2e('0x2f')][_0x0a2e('0x45')]){_0x3ff6ad[_0x0a2e('0x3f')]=[{'all':!![]}];}_0x3ff6ad=_[_0x0a2e('0x3d')]({},_0x3ff6ad,_0x5097cc[_0x0a2e('0x3e')]);return db[_0x0a2e('0x2b')][_0x0a2e('0x46')](_0x3ff6ad)[_0x0a2e('0x26')](handleEntityNotFound(_0x3c1601,null))[_0x0a2e('0x26')](function(_0x1ede7a){if(_0x1ede7a){var _0xeae078=_0x1ede7a[_0x0a2e('0x48')]({'plain':!![]});_0xeae078=qs[_0x0a2e('0x49')](_0xeae078,['id',_0x0a2e('0x4a'),_0x0a2e('0x4b')]);_0x5097cc[_0x0a2e('0x4c')]=_['omit'](_0x5097cc['body'],['id',_0x0a2e('0x4a'),_0x0a2e('0x4b')]);return db[_0x0a2e('0x2b')][_0x0a2e('0x4d')](_[_0x0a2e('0x3d')](_0xeae078,_0x5097cc['body']),{'include':_0x5097cc['query']['includeAll']?[{'all':!![]}]:undefined});}})[_0x0a2e('0x26')](respondWithResult(_0x3c1601,0xc9))[_0x0a2e('0x41')](handleError(_0x3c1601,null));};exports['update']=function(_0x5a1023,_0x19c62c){if(_0x5a1023['body']['id']){delete _0x5a1023[_0x0a2e('0x4c')]['id'];}return db[_0x0a2e('0x2b')][_0x0a2e('0x46')]({'where':{'id':_0x5a1023[_0x0a2e('0x43')]['id']}})[_0x0a2e('0x26')](handleEntityNotFound(_0x19c62c,null))[_0x0a2e('0x26')](saveUpdates(_0x5a1023[_0x0a2e('0x4c')],null))[_0x0a2e('0x26')](respondWithResult(_0x19c62c,null))['catch'](handleError(_0x19c62c,null));};exports['destroy']=function(_0xacf370,_0x16f789){return db[_0x0a2e('0x2b')]['find']({'where':{'id':_0xacf370[_0x0a2e('0x43')]['id']}})['then'](handleEntityNotFound(_0x16f789,null))[_0x0a2e('0x26')](removeEntity(_0x16f789,null))[_0x0a2e('0x41')](handleError(_0x16f789,null));};exports[_0x0a2e('0x4d')]=function(_0x55d1e2,_0x2a937c,_0x284687){_0x55d1e2[_0x0a2e('0x4c')][_0x0a2e('0x29')]=_0x55d1e2['file'][_0x0a2e('0x4e')];_0x55d1e2['body'][_0x0a2e('0x4f')]=_0x55d1e2['file'][_0x0a2e('0x50')];_0x55d1e2[_0x0a2e('0x4c')]['type']=_0x55d1e2[_0x0a2e('0x51')][_0x0a2e('0x52')];return db[_0x0a2e('0x2b')][_0x0a2e('0x4d')](_0x55d1e2[_0x0a2e('0x4c')])[_0x0a2e('0x26')](respondWithResult(_0x2a937c,0xc9))[_0x0a2e('0x41')](handleError(_0x2a937c,null));};exports[_0x0a2e('0x53')]=function(_0x5d7c31,_0xcf2c3a,_0x529533){return db['Attachment'][_0x0a2e('0x54')]({'where':{'id':_0x5d7c31['params']['id']},'raw':!![]})[_0x0a2e('0x26')](handleEntityNotFound(_0xcf2c3a,null))[_0x0a2e('0x26')](function(_0x40d9ad){if(_0x40d9ad){var _0x13f804=_0x40d9ad['ChatOfflineMessageId']!==null;var _0x11a713=path[_0x0a2e('0x55')](config[_0x0a2e('0x56')],_0x0a2e('0x57'));if(_0x13f804){_0x11a713=path['join'](_0x11a713,'offline-chat/');}var _0x5881d0=path['join'](_0x11a713,_0x40d9ad['basename']);return _0xcf2c3a[_0x0a2e('0x53')](_0x5881d0);}})[_0x0a2e('0x41')](handleError(_0xcf2c3a,null));};