Built motion from commit eddce310.|2.6.12
[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 _0x49b3=['set','apply','reject','then','destroy','error','stack','name','send','map','Attachment','type','key','query','keys','filters','intersection','model','attributes','fields','length','hasOwnProperty','nolimit','order','sort','where','pick','filter','merge','includeAll','include','findAll','rows','catch','show','params','rawAttributes','options','find','get','omit','createdAt','body','updatedAt','create','file','originalname','basename','filename','mimetype','ChatOfflineMessageId','join','server/files/attachments/','offline-chat/','download','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','path','sox','ejs','fs-extra','squel','crypto','desk.js','to-csv','querystring','ioredis','../../components/parsers/advancedSearch','../../config/license/hardware','../../config/logger','../../config/license/util','sendStatus','status','end','json','offset','undefined','limit','count'];(function(_0x5dc03e,_0x54e60c){var _0x466a07=function(_0x2ce0cd){while(--_0x2ce0cd){_0x5dc03e['push'](_0x5dc03e['shift']());}};_0x466a07(++_0x54e60c);}(_0x49b3,0x8b));var _0x349b=function(_0x40bffc,_0x3a45d7){_0x40bffc=_0x40bffc-0x0;var _0x3007e9=_0x49b3[_0x40bffc];return _0x3007e9;};'use strict';var emlformat=require(_0x349b('0x0'));var rimraf=require(_0x349b('0x1'));var zipdir=require(_0x349b('0x2'));var jsonpatch=require(_0x349b('0x3'));var rp=require(_0x349b('0x4'));var moment=require(_0x349b('0x5'));var BPromise=require(_0x349b('0x6'));var Mustache=require('mustache');var util=require('util');var path=require(_0x349b('0x7'));var sox=require(_0x349b('0x8'));var csv=require('to-csv');var ejs=require(_0x349b('0x9'));var fs=require('fs');var fs_extra=require(_0x349b('0xa'));var _=require('lodash');var squel=require(_0x349b('0xb'));var crypto=require(_0x349b('0xc'));var jsforce=require('jsforce');var deskjs=require(_0x349b('0xd'));var toCsv=require(_0x349b('0xe'));var querystring=require(_0x349b('0xf'));var Papa=require('papaparse');var Redis=require(_0x349b('0x10'));var authService=require('../../components/auth/service');var qs=require('../../components/parsers/qs');var as=require(_0x349b('0x11'));var hardwareService=require(_0x349b('0x12'));var logger=require(_0x349b('0x13'))('api');var utils=require('../../config/utils');var config=require('../../config/environment');var licenseUtil=require(_0x349b('0x14'));var db=require('../../mysqldb')['db'];function respondWithStatusCode(_0x133d89,_0x23f0a7){_0x23f0a7=_0x23f0a7||0xcc;return function(_0x4b1757){if(_0x4b1757){return _0x133d89[_0x349b('0x15')](_0x23f0a7);}return _0x133d89[_0x349b('0x16')](_0x23f0a7)[_0x349b('0x17')]();};}function respondWithResult(_0x46f18b,_0x111803){_0x111803=_0x111803||0xc8;return function(_0x7f69d0){if(_0x7f69d0){return _0x46f18b[_0x349b('0x16')](_0x111803)[_0x349b('0x18')](_0x7f69d0);}};}function respondWithFilteredResult(_0x324e8a,_0x71af83){return function(_0x3b11c0){if(_0x3b11c0){var _0x2261aa=typeof _0x71af83[_0x349b('0x19')]===_0x349b('0x1a')&&typeof _0x71af83[_0x349b('0x1b')]===_0x349b('0x1a');var _0x1d0474=_0x3b11c0[_0x349b('0x1c')];var _0x13060e=_0x2261aa?0x0:_0x71af83['offset'];var _0x58e69a=_0x2261aa?_0x3b11c0[_0x349b('0x1c')]:_0x71af83['offset']+_0x71af83[_0x349b('0x1b')];var _0xf672f;if(_0x58e69a>=_0x1d0474){_0x58e69a=_0x1d0474;_0xf672f=0xc8;}else{_0xf672f=0xce;}_0x324e8a[_0x349b('0x16')](_0xf672f);return _0x324e8a[_0x349b('0x1d')]('Content-Range',_0x13060e+'-'+_0x58e69a+'/'+_0x1d0474)[_0x349b('0x18')](_0x3b11c0);}return null;};}function patchUpdates(_0x2a8b3c){return function(_0x39838d){try{jsonpatch[_0x349b('0x1e')](_0x39838d,_0x2a8b3c,!![]);}catch(_0x193b6e){return BPromise[_0x349b('0x1f')](_0x193b6e);}return _0x39838d['save']();};}function saveUpdates(_0x17b538,_0x3b032c){return function(_0x2e0b1e){if(_0x2e0b1e){return _0x2e0b1e['update'](_0x17b538)[_0x349b('0x20')](function(_0x291eb5){return _0x291eb5;});}return null;};}function removeEntity(_0x7be0aa,_0xda9e4e){return function(_0x4c152e){if(_0x4c152e){return _0x4c152e[_0x349b('0x21')]()['then'](function(){_0x7be0aa[_0x349b('0x16')](0xcc)[_0x349b('0x17')]();});}};}function handleEntityNotFound(_0x3e405f,_0x1f4af6){return function(_0x219c52){if(!_0x219c52){_0x3e405f[_0x349b('0x15')](0x194);}return _0x219c52;};}function handleError(_0x588987,_0x294972){_0x294972=_0x294972||0x1f4;return function(_0x256788){logger[_0x349b('0x22')](_0x256788[_0x349b('0x23')]);if(_0x256788[_0x349b('0x24')]){delete _0x256788[_0x349b('0x24')];}_0x588987['status'](_0x294972)[_0x349b('0x25')](_0x256788);};}exports['index']=function(_0x31cf7a,_0x3cf614){var _0x5c16b7={},_0x1dbdb6={},_0x3648c6={'count':0x0,'rows':[]};var _0x32958a=_[_0x349b('0x26')](db[_0x349b('0x27')]['rawAttributes'],function(_0x3c590a){return{'name':_0x3c590a['fieldName'],'type':_0x3c590a[_0x349b('0x28')][_0x349b('0x29')]};});_0x1dbdb6['model']=_[_0x349b('0x26')](_0x32958a,_0x349b('0x24'));_0x1dbdb6[_0x349b('0x2a')]=_[_0x349b('0x2b')](_0x31cf7a[_0x349b('0x2a')]);_0x1dbdb6[_0x349b('0x2c')]=_[_0x349b('0x2d')](_0x1dbdb6[_0x349b('0x2e')],_0x1dbdb6[_0x349b('0x2a')]);_0x5c16b7[_0x349b('0x2f')]=_[_0x349b('0x2d')](_0x1dbdb6[_0x349b('0x2e')],qs[_0x349b('0x30')](_0x31cf7a['query']['fields']));_0x5c16b7[_0x349b('0x2f')]=_0x5c16b7[_0x349b('0x2f')][_0x349b('0x31')]?_0x5c16b7[_0x349b('0x2f')]:_0x1dbdb6[_0x349b('0x2e')];if(!_0x31cf7a[_0x349b('0x2a')][_0x349b('0x32')](_0x349b('0x33'))){_0x5c16b7[_0x349b('0x1b')]=qs[_0x349b('0x1b')](_0x31cf7a[_0x349b('0x2a')][_0x349b('0x1b')]);_0x5c16b7[_0x349b('0x19')]=qs[_0x349b('0x19')](_0x31cf7a[_0x349b('0x2a')]['offset']);}_0x5c16b7[_0x349b('0x34')]=qs[_0x349b('0x35')](_0x31cf7a['query'][_0x349b('0x35')]);_0x5c16b7[_0x349b('0x36')]=qs['filters'](_[_0x349b('0x37')](_0x31cf7a['query'],_0x1dbdb6[_0x349b('0x2c')]),_0x32958a);if(_0x31cf7a[_0x349b('0x2a')][_0x349b('0x38')]){_0x5c16b7['where']=_[_0x349b('0x39')](_0x5c16b7[_0x349b('0x36')],{'$or':_[_0x349b('0x26')](_0x32958a,function(_0x3c32c9){if(_0x3c32c9[_0x349b('0x28')]!=='VIRTUAL'){var _0xe74641={};_0xe74641[_0x3c32c9['name']]={'$like':'%'+_0x31cf7a[_0x349b('0x2a')]['filter']+'%'};return _0xe74641;}})});}_0x5c16b7=_['merge']({},_0x5c16b7,_0x31cf7a['options']);var _0x565bfb={'where':_0x5c16b7[_0x349b('0x36')]};return db[_0x349b('0x27')][_0x349b('0x1c')](_0x565bfb)['then'](function(_0x4bd8f4){_0x3648c6['count']=_0x4bd8f4;if(_0x31cf7a[_0x349b('0x2a')][_0x349b('0x3a')]){_0x5c16b7[_0x349b('0x3b')]=[{'all':!![]}];}return db[_0x349b('0x27')][_0x349b('0x3c')](_0x5c16b7);})[_0x349b('0x20')](function(_0x45b6b1){_0x3648c6[_0x349b('0x3d')]=_0x45b6b1;return _0x3648c6;})[_0x349b('0x20')](respondWithFilteredResult(_0x3cf614,_0x5c16b7))[_0x349b('0x3e')](handleError(_0x3cf614,null));};exports[_0x349b('0x3f')]=function(_0x17b582,_0x41dcb7){var _0x2e1345={'raw':!![],'where':{'id':_0x17b582[_0x349b('0x40')]['id']}},_0xa053ad={};_0xa053ad[_0x349b('0x2e')]=_[_0x349b('0x2b')](db[_0x349b('0x27')][_0x349b('0x41')]);_0xa053ad[_0x349b('0x2a')]=_[_0x349b('0x2b')](_0x17b582[_0x349b('0x2a')]);_0xa053ad[_0x349b('0x2c')]=_[_0x349b('0x2d')](_0xa053ad[_0x349b('0x2e')],_0xa053ad['query']);_0x2e1345[_0x349b('0x2f')]=_['intersection'](_0xa053ad[_0x349b('0x2e')],qs[_0x349b('0x30')](_0x17b582[_0x349b('0x2a')][_0x349b('0x30')]));_0x2e1345[_0x349b('0x2f')]=_0x2e1345['attributes'][_0x349b('0x31')]?_0x2e1345[_0x349b('0x2f')]:_0xa053ad['model'];if(_0x17b582[_0x349b('0x2a')][_0x349b('0x3a')]){_0x2e1345[_0x349b('0x3b')]=[{'all':!![]}];}_0x2e1345=_[_0x349b('0x39')]({},_0x2e1345,_0x17b582[_0x349b('0x42')]);return db[_0x349b('0x27')][_0x349b('0x43')](_0x2e1345)['then'](handleEntityNotFound(_0x41dcb7,null))[_0x349b('0x20')](respondWithResult(_0x41dcb7,null))['catch'](handleError(_0x41dcb7,null));};exports['clone']=function(_0x2a73e6,_0x3cc9fc){var _0x348067={'where':{'id':_0x2a73e6[_0x349b('0x40')]['id']}},_0x4cf231={};_0x4cf231[_0x349b('0x2e')]=_[_0x349b('0x2b')](db[_0x349b('0x27')][_0x349b('0x41')]);_0x348067[_0x349b('0x2f')]=_[_0x349b('0x2d')](_0x4cf231['model'],qs[_0x349b('0x30')](_0x2a73e6[_0x349b('0x2a')][_0x349b('0x30')]));_0x348067[_0x349b('0x2f')]=_0x348067[_0x349b('0x2f')]['length']?_0x348067[_0x349b('0x2f')]:_0x4cf231[_0x349b('0x2e')];if(_0x2a73e6[_0x349b('0x2a')]['includeAll']){_0x348067['include']=[{'all':!![]}];}_0x348067=_['merge']({},_0x348067,_0x2a73e6[_0x349b('0x42')]);return db[_0x349b('0x27')][_0x349b('0x43')](_0x348067)[_0x349b('0x20')](handleEntityNotFound(_0x3cc9fc,null))[_0x349b('0x20')](function(_0x7a6b4d){if(_0x7a6b4d){var _0x2b3550=_0x7a6b4d[_0x349b('0x44')]({'plain':!![]});_0x2b3550=qs[_0x349b('0x45')](_0x2b3550,['id',_0x349b('0x46'),'updatedAt']);_0x2a73e6[_0x349b('0x47')]=_[_0x349b('0x45')](_0x2a73e6['body'],['id','createdAt',_0x349b('0x48')]);return db[_0x349b('0x27')]['create'](_[_0x349b('0x39')](_0x2b3550,_0x2a73e6[_0x349b('0x47')]),{'include':_0x2a73e6[_0x349b('0x2a')][_0x349b('0x3a')]?[{'all':!![]}]:undefined});}})['then'](respondWithResult(_0x3cc9fc,0xc9))['catch'](handleError(_0x3cc9fc,null));};exports['update']=function(_0x37354d,_0x307a5b){if(_0x37354d[_0x349b('0x47')]['id']){delete _0x37354d[_0x349b('0x47')]['id'];}return db['Attachment'][_0x349b('0x43')]({'where':{'id':_0x37354d[_0x349b('0x40')]['id']}})[_0x349b('0x20')](handleEntityNotFound(_0x307a5b,null))[_0x349b('0x20')](saveUpdates(_0x37354d[_0x349b('0x47')],null))[_0x349b('0x20')](respondWithResult(_0x307a5b,null))[_0x349b('0x3e')](handleError(_0x307a5b,null));};exports['destroy']=function(_0x2a0b5c,_0x4f2cb3){return db['Attachment'][_0x349b('0x43')]({'where':{'id':_0x2a0b5c[_0x349b('0x40')]['id']}})[_0x349b('0x20')](handleEntityNotFound(_0x4f2cb3,null))[_0x349b('0x20')](removeEntity(_0x4f2cb3,null))['catch'](handleError(_0x4f2cb3,null));};exports[_0x349b('0x49')]=function(_0x44af3b,_0x482113,_0x5f1a1c){_0x44af3b[_0x349b('0x47')]['name']=_0x44af3b[_0x349b('0x4a')][_0x349b('0x4b')];_0x44af3b[_0x349b('0x47')][_0x349b('0x4c')]=_0x44af3b[_0x349b('0x4a')][_0x349b('0x4d')];_0x44af3b[_0x349b('0x47')][_0x349b('0x28')]=_0x44af3b['file'][_0x349b('0x4e')];return db[_0x349b('0x27')][_0x349b('0x49')](_0x44af3b[_0x349b('0x47')])[_0x349b('0x20')](respondWithResult(_0x482113,0xc9))[_0x349b('0x3e')](handleError(_0x482113,null));};exports['download']=function(_0x166b62,_0x301a2c,_0x218c7f){return db[_0x349b('0x27')]['findOne']({'where':{'id':_0x166b62['params']['id']},'raw':!![]})[_0x349b('0x20')](handleEntityNotFound(_0x301a2c,null))['then'](function(_0x2b83b0){if(_0x2b83b0){var _0x190f4a=_0x2b83b0[_0x349b('0x4f')]!==null;var _0xfc2698=path[_0x349b('0x50')](config['root'],_0x349b('0x51'));if(_0x190f4a){_0xfc2698=path[_0x349b('0x50')](_0xfc2698,_0x349b('0x52'));}var _0x2c84ac=path[_0x349b('0x50')](_0xfc2698,_0x2b83b0['basename']);return _0x301a2c[_0x349b('0x53')](_0x2c84ac);}})[_0x349b('0x3e')](handleError(_0x301a2c,null));};