var express = require('express'); var url = require('url'); var router = express.Router(); var ljConst = require("../common/LjConst"); var ljRetState = require("../common/LjRetState"); var logHelper = require("../helper/LjLogHelper.js"); var httpHelper = require("../helper/LjHttpHelper"); var fileHelper = require("../helper/LjFileHelper"); var apiHelper = require("../helper/LjApiHelper"); var ljStringHelper = require("../helper/LjStringHelper"); /** * 下载文件 */ router.get("/downloadFile",function(req,res,next) { var arg = url.parse(req.url, true).query; var courseId = arg.courseId; var eid = arg.eid; var uid = arg.uid; var token = arg.token; var lessonId = arg.lessonId; //获取参数 logHelper.debug(arg); //校验参数 if (ljStringHelper.isNull(courseId)) { logHelper.info("null object of courseId"); ljRetState.code = 2; ljRetState.msg ="null object of courseId" res.json(ljRetState); return; } if (ljStringHelper.isNull(lessonId)) { logHelper.info("null object of lessonId"); ljRetState.code = 2; ljRetState.msg ="null object of lessonId" res.json(ljRetState); return; } if (ljStringHelper.isNull(eid)) { logHelper.info("null object of eid"); ljRetState.code = 2; ljRetState.msg ="null object of eid" res.json(ljRetState); return; } if (ljStringHelper.isNull(uid)) { logHelper.info("null object of uid"); ljRetState.code = 2; ljRetState.msg ="null object of uid" res.json(ljRetState); return; } if (ljStringHelper.isNull(token)) { logHelper.info("null object of token"); ljRetState.code = 2; ljRetState.msg ="null object of token" res.json(ljRetState); return; } var opt = {}; opt.eid = eid; opt.uid = uid; opt.token = token; opt.lessonId = lessonId; opt.courseId = courseId; opt.path = "/lesson/" + opt.lessonId; opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid); opt.method = "get"; httpHelper.request(opt, function (error, res, body) { let obj = JSON.parse(body); var lessonJsonObj = JSON.parse(body); if (lessonJsonObj.code == 200) { /*课件-Begin*/ //课程集合 var courseList = lessonJsonObj.data.list; //课程数 var courseListSize = courseList.length; //遍历课件 for (var i = 0; i < courseListSize; i++) { var lessonware = courseList[i]; if(lessonware.type == ljConst.RES_TYPE_IMAGE || lessonware.type == ljConst.RES_TYPE_VIDEO){ //资源集合 var resourceList = lessonware.list; //资源数 var resourceSize = resourceList.length; for (var j = 0; j < resourceSize; j++) { logHelper.debug(resourceList[j].url); var url = resourceList[j].url; fileHelper.downloadFile(url) } }else if(lessonware.type == ljConst.RES_TYPE_AUDIO){ //有声资源集合 var audioResourceList = lessonware.list; //资源数 var resourceSize = audioResourceList.length; for (var j = 0; j < resourceSize; j++) { var img = audioResourceList[j].img; var audio = audioResourceList[j].audio; logHelper.debug(img); logHelper.debug(audio); var imgUrl = img.url; var audioUrl = audio.url; if(ljStringHelper.isNonNull(imgUrl)){ fileHelper.downloadFile(imgUrl); } if(ljStringHelper.isNonNull(audioUrl)){ fileHelper.downloadFile(audioUrl); } } } } /*课件-End*/ //保存记录 //addDownload(opt) return "ok"; } else { logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj); return "error"; } }).on("end",function() { test(); }); }); /** * 读取文件 */ router.get("/readFile",function(req,res) { var arg = url.parse(req.url, true).query; var fileDirStr = arg.n; var fileDirNew = fileDirStr.replace(/\*/g,'\\'); var fileDir = fileHelper.getFileDir(fileDirNew) var fileProp = fileHelper.getFileProperty(fileDir); if(fileProp.fileType == ljConst.VIDEO_TYPE_M3U8) { fileHelper.readDecryptedStream(res,fileDir,"video/mp4") } else { fileHelper.readDecryptedStream(res,fileDir,"image/jpeg") } }); function test() { console.log(11); } /** * 删除下载文件 */ router.get("/delFile",function(req,res) { var arg = url.parse(req.url, true).query; var courseId = arg.courseId; var eid = arg.eid; var uid = arg.uid; var token = arg.token; var lessonId = arg.lessonId; //获取参数 logHelper.debug(arg); //校验参数 if (ljStringHelper.isNull(courseId)) { logHelper.info("null object of courseId"); return "null object of courseId"; } if (ljStringHelper.isNull(lessonId)) { logHelper.info("null object of lessonId"); return "null object of courseId"; } if (ljStringHelper.isNull(eid)) { logHelper.info("null object of eid"); return "null object of eid"; } if (ljStringHelper.isNull(uid)) { logHelper.info("null object of uid"); return "null object of uid"; } if (ljStringHelper.isNull(token)) { logHelper.info("null object of token"); return "null object of token"; } var opt = {}; opt.eid = eid; opt.uid = uid; opt.token = token; opt.lessonId = lessonId; opt.courseId = courseId; opt.path = "/lesson/" + opt.lessonId; opt.url = apiHelper.getApiForCourse(opt.lessonId, opt.courseId, opt.eid); opt.method = "get"; httpHelper.request(opt, function (error, res, body) { let obj = JSON.parse(body); var lessonJsonObj = JSON.parse(body); if (lessonJsonObj.code == 200) { /*课件-Begin*/ //课程集合 var courseList = lessonJsonObj.data.list; //课程数 var courseListSize = courseList.length; //遍历课件 for (var i = 0; i < courseListSize; i++) { //资源集合 var resourceList = courseList[i].list; //资源数 var resourceSize = resourceList.length; for (var j = 0; j < resourceSize; j++) { logHelper.debug(resourceList[j].url); var url = resourceList[j].url; fileHelper.downloadFile(url) } } /*课件-End*/ //保存记录 process.nextTick(addDownload); return "ok"; } else { logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj); return "error"; } }); }); var addDownload = function(opt) { opt.url = apiHelper.getApiForAddDownload(opt.lessonId,opt.courseId, null); opt.method = "post"; opt.path = "/callback/download/add"; httpHelper.request(opt, function (error, res, body) { var retObj = JSON.parse(body); logHelper.debug(retObj); if (retObj.code == 200) { logHelper.info("Succeed to call api[" + opt.path + "]"); return true; } else { logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]"); return false; } }); } var delDownload = function(opt) { opt.url = apiHelper.getApiForDelDownload(opt.lessonId, null); opt.method = "post"; opt.path = "/callback/download/delete"; httpHelper.request(opt, function (error, res, body) { var retObj = JSON.parse(body); logHelper.debug(retObj); if (retObj.code == 200) { logHelper.info("Succeed to call api[" + opt.path + "]"); return true; } else { logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]"); return false; } }); } module.exports = router;