Built motion from commit 14588770.|2.5.38
[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 _0xcf5b=['filename','mimetype','download','ChatOfflineMessageId','root','join','basename','rimraf','zip-dir','fast-json-patch','request-promise','moment','bluebird','mustache','util','path','to-csv','ejs','fs-extra','lodash','crypto','jsforce','desk.js','papaparse','../../components/auth/service','../../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','reject','update','destroy','then','end','sendStatus','error','stack','send','Attachment','rawAttributes','fieldName','type','model','name','query','keys','filters','intersection','attributes','length','order','sort','where','pick','filter','merge','options','includeAll','include','findAll','rows','catch','show','params','fields','find','get','createdAt','updatedAt','body','omit','create','file','originalname'];(function(_0x2016d8,_0x100b3d){var _0xe519af=function(_0x585b14){while(--_0x585b14){_0x2016d8['push'](_0x2016d8['shift']());}};_0xe519af(++_0x100b3d);}(_0xcf5b,0xb3));var _0xbcf5=function(_0x5cb97e,_0x132a39){_0x5cb97e=_0x5cb97e-0x0;var _0x1714b4=_0xcf5b[_0x5cb97e];return _0x1714b4;};'use strict';var emlformat=require('eml-format');var rimraf=require(_0xbcf5('0x0'));var zipdir=require(_0xbcf5('0x1'));var jsonpatch=require(_0xbcf5('0x2'));var rp=require(_0xbcf5('0x3'));var moment=require(_0xbcf5('0x4'));var BPromise=require(_0xbcf5('0x5'));var Mustache=require(_0xbcf5('0x6'));var util=require(_0xbcf5('0x7'));var path=require(_0xbcf5('0x8'));var sox=require('sox');var csv=require(_0xbcf5('0x9'));var ejs=require(_0xbcf5('0xa'));var fs=require('fs');var fs_extra=require(_0xbcf5('0xb'));var _=require(_0xbcf5('0xc'));var squel=require('squel');var crypto=require(_0xbcf5('0xd'));var jsforce=require(_0xbcf5('0xe'));var deskjs=require(_0xbcf5('0xf'));var toCsv=require(_0xbcf5('0x9'));var querystring=require('querystring');var Papa=require(_0xbcf5('0x10'));var Redis=require('ioredis');var authService=require(_0xbcf5('0x11'));var qs=require(_0xbcf5('0x12'));var as=require('../../components/parsers/advancedSearch');var hardwareService=require(_0xbcf5('0x13'));var logger=require(_0xbcf5('0x14'))(_0xbcf5('0x15'));var utils=require(_0xbcf5('0x16'));var config=require(_0xbcf5('0x17'));var licenseUtil=require(_0xbcf5('0x18'));var db=require(_0xbcf5('0x19'))['db'];function respondWithStatusCode(_0x12c7e5,_0x524511){_0x524511=_0x524511||0xcc;return function(_0x363592){if(_0x363592){return _0x12c7e5['sendStatus'](_0x524511);}return _0x12c7e5[_0xbcf5('0x1a')](_0x524511)['end']();};}function respondWithResult(_0x20faf9,_0x37a832){_0x37a832=_0x37a832||0xc8;return function(_0xfdaa1c){if(_0xfdaa1c){return _0x20faf9[_0xbcf5('0x1a')](_0x37a832)[_0xbcf5('0x1b')](_0xfdaa1c);}};}function respondWithFilteredResult(_0x37bd31,_0x2757f5){return function(_0x3163f3){if(_0x3163f3){var _0x28e047=typeof _0x2757f5[_0xbcf5('0x1c')]===_0xbcf5('0x1d')&&typeof _0x2757f5[_0xbcf5('0x1e')]===_0xbcf5('0x1d');var _0x3562a9=_0x3163f3[_0xbcf5('0x1f')];var _0x19af52=_0x28e047?0x0:_0x2757f5[_0xbcf5('0x1c')];var _0x38b785=_0x28e047?_0x3163f3['count']:_0x2757f5[_0xbcf5('0x1c')]+_0x2757f5['limit'];var _0xaf0501;if(_0x38b785>=_0x3562a9){_0x38b785=_0x3562a9;_0xaf0501=0xc8;}else{_0xaf0501=0xce;}_0x37bd31['status'](_0xaf0501);return _0x37bd31[_0xbcf5('0x20')](_0xbcf5('0x21'),_0x19af52+'-'+_0x38b785+'/'+_0x3562a9)[_0xbcf5('0x1b')](_0x3163f3);}return null;};}function patchUpdates(_0x1e87d7){return function(_0x223f0b){try{jsonpatch['apply'](_0x223f0b,_0x1e87d7,!![]);}catch(_0x851ea4){return BPromise[_0xbcf5('0x22')](_0x851ea4);}return _0x223f0b['save']();};}function saveUpdates(_0x1c6e83,_0x3718d8){return function(_0x42c5cd){if(_0x42c5cd){return _0x42c5cd[_0xbcf5('0x23')](_0x1c6e83)['then'](function(_0x314db3){return _0x314db3;});}return null;};}function removeEntity(_0x33d480,_0x13d934){return function(_0x364407){if(_0x364407){return _0x364407[_0xbcf5('0x24')]()[_0xbcf5('0x25')](function(){_0x33d480[_0xbcf5('0x1a')](0xcc)[_0xbcf5('0x26')]();});}};}function handleEntityNotFound(_0x5565c7,_0x1dad81){return function(_0x51a2a4){if(!_0x51a2a4){_0x5565c7[_0xbcf5('0x27')](0x194);}return _0x51a2a4;};}function handleError(_0x3c3f45,_0x5367d0){_0x5367d0=_0x5367d0||0x1f4;return function(_0x1777d4){logger[_0xbcf5('0x28')](_0x1777d4[_0xbcf5('0x29')]);if(_0x1777d4['name']){delete _0x1777d4['name'];}_0x3c3f45['status'](_0x5367d0)[_0xbcf5('0x2a')](_0x1777d4);};}exports['index']=function(_0x256679,_0x2ee124){var _0x4c2c49={},_0x3b52fe={},_0x5e6beb={'count':0x0,'rows':[]};var _0x42e462=_['map'](db[_0xbcf5('0x2b')][_0xbcf5('0x2c')],function(_0x9f75e8){return{'name':_0x9f75e8[_0xbcf5('0x2d')],'type':_0x9f75e8[_0xbcf5('0x2e')]['key']};});_0x3b52fe[_0xbcf5('0x2f')]=_['map'](_0x42e462,_0xbcf5('0x30'));_0x3b52fe[_0xbcf5('0x31')]=_[_0xbcf5('0x32')](_0x256679[_0xbcf5('0x31')]);_0x3b52fe[_0xbcf5('0x33')]=_[_0xbcf5('0x34')](_0x3b52fe[_0xbcf5('0x2f')],_0x3b52fe[_0xbcf5('0x31')]);_0x4c2c49[_0xbcf5('0x35')]=_[_0xbcf5('0x34')](_0x3b52fe[_0xbcf5('0x2f')],qs['fields'](_0x256679[_0xbcf5('0x31')]['fields']));_0x4c2c49[_0xbcf5('0x35')]=_0x4c2c49[_0xbcf5('0x35')][_0xbcf5('0x36')]?_0x4c2c49[_0xbcf5('0x35')]:_0x3b52fe[_0xbcf5('0x2f')];if(!_0x256679['query']['hasOwnProperty']('nolimit')){_0x4c2c49[_0xbcf5('0x1e')]=qs['limit'](_0x256679[_0xbcf5('0x31')]['limit']);_0x4c2c49[_0xbcf5('0x1c')]=qs[_0xbcf5('0x1c')](_0x256679[_0xbcf5('0x31')][_0xbcf5('0x1c')]);}_0x4c2c49[_0xbcf5('0x37')]=qs['sort'](_0x256679['query'][_0xbcf5('0x38')]);_0x4c2c49[_0xbcf5('0x39')]=qs[_0xbcf5('0x33')](_[_0xbcf5('0x3a')](_0x256679[_0xbcf5('0x31')],_0x3b52fe['filters']),_0x42e462);if(_0x256679[_0xbcf5('0x31')][_0xbcf5('0x3b')]){_0x4c2c49['where']=_['merge'](_0x4c2c49[_0xbcf5('0x39')],{'$or':_['map'](_0x42e462,function(_0x4942eb){if(_0x4942eb[_0xbcf5('0x2e')]!=='VIRTUAL'){var _0x1b548d={};_0x1b548d[_0x4942eb[_0xbcf5('0x30')]]={'$like':'%'+_0x256679[_0xbcf5('0x31')][_0xbcf5('0x3b')]+'%'};return _0x1b548d;}})});}_0x4c2c49=_[_0xbcf5('0x3c')]({},_0x4c2c49,_0x256679[_0xbcf5('0x3d')]);var _0x564faa={'where':_0x4c2c49['where']};return db[_0xbcf5('0x2b')][_0xbcf5('0x1f')](_0x564faa)[_0xbcf5('0x25')](function(_0x25fa91){_0x5e6beb['count']=_0x25fa91;if(_0x256679[_0xbcf5('0x31')][_0xbcf5('0x3e')]){_0x4c2c49[_0xbcf5('0x3f')]=[{'all':!![]}];}return db[_0xbcf5('0x2b')][_0xbcf5('0x40')](_0x4c2c49);})['then'](function(_0xfce913){_0x5e6beb[_0xbcf5('0x41')]=_0xfce913;return _0x5e6beb;})['then'](respondWithFilteredResult(_0x2ee124,_0x4c2c49))[_0xbcf5('0x42')](handleError(_0x2ee124,null));};exports[_0xbcf5('0x43')]=function(_0x490959,_0x5b83b0){var _0x5a94f5={'raw':!![],'where':{'id':_0x490959[_0xbcf5('0x44')]['id']}},_0x4f4ff9={};_0x4f4ff9[_0xbcf5('0x2f')]=_[_0xbcf5('0x32')](db[_0xbcf5('0x2b')]['rawAttributes']);_0x4f4ff9['query']=_[_0xbcf5('0x32')](_0x490959[_0xbcf5('0x31')]);_0x4f4ff9[_0xbcf5('0x33')]=_[_0xbcf5('0x34')](_0x4f4ff9['model'],_0x4f4ff9[_0xbcf5('0x31')]);_0x5a94f5[_0xbcf5('0x35')]=_[_0xbcf5('0x34')](_0x4f4ff9[_0xbcf5('0x2f')],qs[_0xbcf5('0x45')](_0x490959[_0xbcf5('0x31')][_0xbcf5('0x45')]));_0x5a94f5[_0xbcf5('0x35')]=_0x5a94f5['attributes'][_0xbcf5('0x36')]?_0x5a94f5['attributes']:_0x4f4ff9[_0xbcf5('0x2f')];if(_0x490959[_0xbcf5('0x31')][_0xbcf5('0x3e')]){_0x5a94f5['include']=[{'all':!![]}];}_0x5a94f5=_[_0xbcf5('0x3c')]({},_0x5a94f5,_0x490959[_0xbcf5('0x3d')]);return db[_0xbcf5('0x2b')][_0xbcf5('0x46')](_0x5a94f5)[_0xbcf5('0x25')](handleEntityNotFound(_0x5b83b0,null))[_0xbcf5('0x25')](respondWithResult(_0x5b83b0,null))[_0xbcf5('0x42')](handleError(_0x5b83b0,null));};exports['clone']=function(_0x257bf3,_0x12ee26){var _0x338f4b={'where':{'id':_0x257bf3[_0xbcf5('0x44')]['id']}},_0x27106f={};_0x27106f[_0xbcf5('0x2f')]=_[_0xbcf5('0x32')](db[_0xbcf5('0x2b')][_0xbcf5('0x2c')]);_0x338f4b[_0xbcf5('0x35')]=_[_0xbcf5('0x34')](_0x27106f[_0xbcf5('0x2f')],qs[_0xbcf5('0x45')](_0x257bf3[_0xbcf5('0x31')]['fields']));_0x338f4b['attributes']=_0x338f4b['attributes'][_0xbcf5('0x36')]?_0x338f4b[_0xbcf5('0x35')]:_0x27106f['model'];if(_0x257bf3[_0xbcf5('0x31')][_0xbcf5('0x3e')]){_0x338f4b[_0xbcf5('0x3f')]=[{'all':!![]}];}_0x338f4b=_[_0xbcf5('0x3c')]({},_0x338f4b,_0x257bf3['options']);return db[_0xbcf5('0x2b')][_0xbcf5('0x46')](_0x338f4b)[_0xbcf5('0x25')](handleEntityNotFound(_0x12ee26,null))[_0xbcf5('0x25')](function(_0x2450ab){if(_0x2450ab){var _0x13ac85=_0x2450ab[_0xbcf5('0x47')]({'plain':!![]});_0x13ac85=qs['omit'](_0x13ac85,['id',_0xbcf5('0x48'),_0xbcf5('0x49')]);_0x257bf3[_0xbcf5('0x4a')]=_[_0xbcf5('0x4b')](_0x257bf3['body'],['id',_0xbcf5('0x48'),'updatedAt']);return db[_0xbcf5('0x2b')][_0xbcf5('0x4c')](_[_0xbcf5('0x3c')](_0x13ac85,_0x257bf3[_0xbcf5('0x4a')]),{'include':_0x257bf3[_0xbcf5('0x31')]['includeAll']?[{'all':!![]}]:undefined});}})[_0xbcf5('0x25')](respondWithResult(_0x12ee26,0xc9))[_0xbcf5('0x42')](handleError(_0x12ee26,null));};exports[_0xbcf5('0x23')]=function(_0x139fc2,_0x1f355b){if(_0x139fc2['body']['id']){delete _0x139fc2['body']['id'];}return db[_0xbcf5('0x2b')][_0xbcf5('0x46')]({'where':{'id':_0x139fc2['params']['id']}})[_0xbcf5('0x25')](handleEntityNotFound(_0x1f355b,null))[_0xbcf5('0x25')](saveUpdates(_0x139fc2[_0xbcf5('0x4a')],null))['then'](respondWithResult(_0x1f355b,null))['catch'](handleError(_0x1f355b,null));};exports[_0xbcf5('0x24')]=function(_0x28f0b1,_0x3040f5){return db['Attachment'][_0xbcf5('0x46')]({'where':{'id':_0x28f0b1['params']['id']}})[_0xbcf5('0x25')](handleEntityNotFound(_0x3040f5,null))[_0xbcf5('0x25')](removeEntity(_0x3040f5,null))[_0xbcf5('0x42')](handleError(_0x3040f5,null));};exports[_0xbcf5('0x4c')]=function(_0x704b5c,_0x775dba,_0xa2100){_0x704b5c[_0xbcf5('0x4a')]['name']=_0x704b5c[_0xbcf5('0x4d')][_0xbcf5('0x4e')];_0x704b5c[_0xbcf5('0x4a')]['basename']=_0x704b5c[_0xbcf5('0x4d')][_0xbcf5('0x4f')];_0x704b5c[_0xbcf5('0x4a')][_0xbcf5('0x2e')]=_0x704b5c[_0xbcf5('0x4d')][_0xbcf5('0x50')];return db[_0xbcf5('0x2b')]['create'](_0x704b5c[_0xbcf5('0x4a')])[_0xbcf5('0x25')](respondWithResult(_0x775dba,0xc9))[_0xbcf5('0x42')](handleError(_0x775dba,null));};exports[_0xbcf5('0x51')]=function(_0xd0c8e9,_0x452f15,_0x4f8134){return db[_0xbcf5('0x2b')]['findOne']({'where':{'id':_0xd0c8e9['params']['id']},'raw':!![]})[_0xbcf5('0x25')](handleEntityNotFound(_0x452f15,null))[_0xbcf5('0x25')](function(_0x8a4bd7){if(_0x8a4bd7){var _0x2f2d1f=_0x8a4bd7[_0xbcf5('0x52')]!==null;var _0x5efaee=path['join'](config[_0xbcf5('0x53')],'server/files/attachments/');if(_0x2f2d1f){_0x5efaee=path[_0xbcf5('0x54')](_0x5efaee,'offline-chat/');}var _0x1f5320=path[_0xbcf5('0x54')](_0x5efaee,_0x8a4bd7[_0xbcf5('0x55')]);return _0x452f15[_0xbcf5('0x51')](_0x1f5320);}})[_0xbcf5('0x42')](handleError(_0x452f15,null));};