UNPKG

kpiplus

Version:

KPI+

1 lines 4.71 kB
var model=require("../../models/file"),app=require("../../models/app"),follow=require("../../models/follow"),path=require("path"),fs=require("fs"),controller=require("../../controllers/controller"),async=require("async"),underscore=require("underscore"),email=require("../../libs/email"),notify=require("../../models/notification"),loadTemplate=require("../../libs/load-template"),request=require("request"),google_config=require("../../configs").google;module.exports=function(e){this.contr=new controller(e,model,"file",{sort:{ten_file:1}});var n=function(e){var n=e.file.name,t=__dirname;t=path.dirname(t),t=path.dirname(t),n=path.join(t,"uploads",n),fs.existsSync(n)&&fs.unlink(n,function(e){if(e)return console.log(e)})},t=function(e,n){var t={refresh_token:e,client_id:google_config.clientID,client_secret:google_config.clientSecret,grant_type:"refresh_token"};request.post({url:"https://www.googleapis.com/oauth2/v3/token",form:t},function(t,i,o){if(t)return n(t);var r=JSON.parse(o).access_token;r||console.log(o,e),n(null,r)})};this.contr.creating=function(e,i,o){i.file?app.findOne({_id:e.current_id_app}).lean().exec(function(e,r){return e||!r?o("Không thể tải file lên máy chủ\n"+e):r.google_drive_refresh_token?void t(r.google_drive_refresh_token,function(e,t){if(e)n(i),o("Không thể lấy token từ Google\n"+e);else{var l=i.file.name,a=__dirname;a=path.dirname(a),a=path.dirname(a),l=path.join(a,"uploads",l),fs.existsSync(l)?fs.readFile(l,function(e,l){n(i),e?o("Không thể tải file lên google drive\n"+e):request.post({url:"https://www.googleapis.com/upload/drive/v2/files",qs:{uploadType:"multipart"},headers:{Authorization:"Bearer "+t},multipart:[{"Content-Type":"application/json; charset=UTF-8",body:JSON.stringify({title:i.file.originalname.split(" ").join("")})},{"Content-Type":"image/png",body:l}]},function(n,t,l){if(e)return o("Không thể tải file lên google drive\n"+e);l=JSON.parse(l),i.refresh_token=r.google_drive_refresh_token,i.account=r.google_drive_email,i.server="google",i.meta=l,o(null,i)})}):o("Không thể tải file lên google drive")}}):o(null,i)}):o(null,i)},this.contr.created=function(e,n,t){n.id_link?async.parallel({get_follow:function(e){follow.find({id_object:n.id_link}).lean().exec(function(n,t){if(n)return e(null);var i=null;t&&(i=underscore.pluck(t,"user_created")),e(null,i)})},get_obj:function(e){if(n.collection_link)try{require("../../models/"+n.collection_link).findOne({_id:n.id_link}).lean().exec(function(n,t){if(n||!t)return e(null);var i=[];i.push(t.user_created),t.phu_trach&&i.push(t.phu_trach),t.attends&&t.attends.forEach(function(e){i.push(e)}),2==t.visible_to&&t.visible_to_users&&t.visible_to_users.forEach(function(e){i.push(e)}),e(null,i)})}catch(n){console.log(n),e(null)}else e(null)}},function(i,o){if(i)return t(null,n);var r=[];o.get_follow&&o.get_follow.forEach(function(e){r.push(e)}),o.get_obj&&o.get_obj.forEach(function(e){r.push(e)}),r=underscore.uniq(r),(r=underscore.filter(r,function(e){return e!=n.user_created})).forEach(function(t){loadTemplate("add-file.html",{file:n,user:e},function(e,i){if(e)return console.log(e);email.sendHtml({to:{name:t,address:t},subject:"Thêm một file vào '"+n.title_topic+"'",html:i},function(e,n){e&&console.error("Khong the gui email thong bao gui file cho email "+t+"\n"+e)})}),notify.createNotification(n.user_created,t,"Thêm một file vào '"+n.title_topic+"'","<p> Xem chi tiết <a href='"+n.url_topic+"'>tại đây</a></p>")}),t(null,n)}):t(null,n)},this.contr.deleted=function(e,n,t){var i=n.file.name,o=__dirname;o=path.dirname(o),o=path.dirname(o),i=path.join(o,"uploads",i),fs.existsSync(i)&&fs.unlink(i,function(e){if(e)return console.log(e)}),t(null,n)},this.contr.route(function(e){e.route("/:id_app/file/download/:id").get(function(e,n,i){var o=e.params.id;app.findOne({_id:e.user.current_id_app}).lean().exec(function(e,i){if(e||!i)return n.status(400).send("Không thể tải file này");model.findOne({_id:o}).lean().exec(function(e,o){if(e)return n.status(400).send(e);if(o)if("google"==o.server&&o.meta){o.file_id;var r=o.refresh_token;o.account==i.google_drive_email&&(r=i.google_drive_refresh_token),t(r,function(e,t){if(e||!t)return n.status(400).send("Không thể tải file này.");request.get({url:o.meta.downloadUrl,encoding:null,headers:{Authorization:"Bearer "+t}},function(e,t){if(e)return n.status(400).send(e);n.attachment(o.file.originalname),t.pipe(n)})})}else{var l=o.file.name,a=__dirname;a=path.dirname(a),a=path.dirname(a),l=path.join(a,"uploads",l),fs.existsSync(l)?n.download(l,o.file.originalname):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")})})})})};