kpiplus
Version:
KPI+
1 lines • 1.51 kB
JavaScript
var model=require("../../models/mailreceived"),controller=require("../../controllers/controller"),underscore=require("underscore"),domain=require("../../configs").domain,path=require("path"),fs=require("fs");module.exports=function(t){this.contr=new controller(t,model,"mailreceived",{sort:{date_created:-1},notNeedRight:!0,onFinding:function(t,n,e){n.user_created=t.email,e(null,n)},onView:function(t,n,e,a){n.forEach(function(n){n.attachments&&n.mail&&n.mail.html&&n.attachments.forEach(function(e){var a=domain+"/api/attachments/"+n._id+"/"+e.contentId+"?access_token="+t.token,i="cid:"+e.contentId;n.mail.html=n.mail.html.split(i).join(a)})}),e(null,n)},onDeleted:function(t,n,e){n.attachments&&n.attachments.length>0&&n.attachments.forEach(function(t){var n=t.path,e=__dirname;e=path.dirname(e),e=path.dirname(e),n=path.join(e,n),fs.existsSync(n)&&fs.unlink(n)}),e(null,n)}}),this.contr.route(function(t){t.route("/attachments/:emailId/:contentId").get(function(t,n,e){var a=t.params.emailId,i=t.params.contentId;model.findOne({_id:a},{attachments:1}).lean().exec(function(t,e){if(t)return n.send(t);if(e&&e.attachments){var a=underscore.find(e.attachments,function(t){return t.contentId==i});if(a){var r=a.path,o=__dirname;o=path.dirname(o),o=path.dirname(o),r=path.join(o,r),fs.existsSync(r)?n.download(r,a.fileName):n.status(400).send("File này không tồn tại")}else n.status(400).send("File này không tồn tại")}else n.status(400).send("Không tìm thấy file này")})})}),this.contr.route()};