123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- 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;
|