123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- var express = require('express');
- var mac = require('getmac');//获取mac地址
- var os = require('os');
- var fs = require('fs');
- 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");
- /**
- * 获取MAC地址
- */
- router.get("/getMac", function (req, res)
- {
- var mac = apiHelper.getApiForGetMac();
- ljRetState.code = 200;
- ljRetState.data = mac;
- ljRetState.success = true;
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.writeHead(200, {'Access-Control-Allow-Origin':'*'});
- res.json(ljRetState);
- return;
- });
- /**
- * 下载文件
- */
- 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;
- //获取总共资源数
- var res_total = 0;
- for (var i = 0; i < courseListSize; i++)
- {
- var courseObj = courseList[i];
- if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
- {
- //资源集合
- var resourceList = courseList[i].list;
- //资源数
- var resourceSize = resourceList.length;
- res_total += resourceSize
- }
- else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
- {
- if (courseObj.list.length > 0)
- {
- var audioObj = courseObj.list[0];
- var image = audioObj.img;
- var audio = audioObj.audio;
- if (image != null && image.url != "")
- {
- res_total += 1;
- }
- if (audio != null && audio.url != "")
- {
- res_total += 1;
- }
- }
- }
- }
- logHelper.info("res_total:" + res_total);
- /*课件-End*/
- //添加下载记录
- opt.resTotal = res_total;
- opt.resSize = 0;
- addDownload(opt);
- logHelper.info("Begin to download file with lessonId【" + opt.lessonId + "】");
- //添加下载记录
- //遍历课件
- for (var i = 0; i < courseListSize; i++)
- {
- var courseObj = courseList[i];
- if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
- {
- //资源集合
- 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, opt);
- }
- }//处理语音
- else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
- {
- if (courseObj.list.length > 0)
- {
- var audioObj = courseObj.list[0];
- var image = audioObj.img;
- var audio = audioObj.audio;
- //下载图片
- if (image != null && image.url != "")
- {
- fileHelper.downloadFile(image.url, opt);
- }
- //下载语音
- if (audio != null && audio.url != "")
- {
- fileHelper.downloadFile(audio.url, opt);
- }
- }
- }
- }
- /*课件-End*/
- return "ok";
- }
- else
- {
- logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
- return "error";
- }
- })
- ljRetState.code = 200;
- ljRetState.msg = "null object of uid"
- ljRetState.success = true;
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.json(ljRetState);
- return;
- });
- /**
- * 读取文件
- */
- router.get("/readFile", function (req, res)
- {
- var arg = url.parse(req.url, true).query;
- var fileDirStr = arg.n;
- var platform = os.platform();
- var fileDirNew = "";
- if (platform == "win32")
- {
- fileDirNew = fileDirStr.replace(/\*/g, '\\');
- }
- else
- {
- fileDirNew = fileDirStr.replace(/\*/g, '/');
- }
- var fileDir = fileHelper.getFileDir(fileDirNew)
- var fileProp = fileHelper.getFileProperty(fileDir);
- console.info(fileDir);
- //判断文件是否存在
- if (!fs.existsSync(fileDir))
- {
- ljRetState.code = 1;
- ljRetState.msg = "文件没有找到"
- ljRetState.success = false;
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.json(ljRetState);
- return false;
- }
- logHelper.info(fileProp.fileType);
- if (fileProp.fileType == ljConst.VIDEO_TYPE_MP4)
- {
- logHelper.info(fileDir);
- fileHelper.readDecryptedStream(res, fileDir, "video/mp4")
- }
- else
- {
- fileHelper.readDecryptedStream(res, fileDir, "image/jpeg")
- }
- });
- /**
- * 删除下载文件
- */
- 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");
- 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 courseObj = courseList[i];
- //处理图片删除
- if (courseObj.type == ljConst.RES_TYPE_IMAGE)
- {
- //资源集合
- var resourceList = courseObj.list;
- //资源数
- var resourceSize = resourceList.length;
- for (var j = 0; j < resourceSize; j++)
- {
- var url = resourceList[j].url;
- var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
- var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
- //判断文件是否存在
- if (fs.existsSync(delFileDir))
- {
- //删除路径
- fs.unlinkSync(delFileDir);
- }
- }
- }//处理视频删除
- else if (courseObj.type == ljConst.RES_TYPE_VIDEO)
- {
- //资源集合
- var resourceList = courseObj.list;
- //资源数
- var resourceSize = resourceList.length;
- for (var j = 0; j < resourceSize; j++)
- {
- var url = resourceList[j].url;
- var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
- var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp4";
- //判断文件是否存在
- if (fs.existsSync(delFileDir))
- {
- //删除路径
- fs.unlinkSync(delFileDir);
- }
- }
- }
- //处理语音删除
- else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
- {
- if (courseObj.list.length > 0)
- {
- var audioObj = courseObj.list[0];
- var image = audioObj.img;
- var audio = audioObj.audio;
- //删除图片
- if (image != null && image.url != "")
- {
- var delFileProp = fileHelper.getFilePropertyNoMkdir(image.url);
- var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
- //判断文件是否存在
- if (fs.existsSync(delFileDir))
- {
- //删除路径
- fs.unlinkSync(delFileDir);
- }
- }
- //删除语音
- if (audio != null && audio.url != "")
- {
- var delFileProp = fileHelper.getFilePropertyNoMkdir(audio.url);
- var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp3";
- //判断文件是否存在
- if (fs.existsSync(delFileDir))
- {
- //删除路径
- fs.unlinkSync(delFileDir);
- }
- }
- }
- }
- }
- /*课件-End*/
- //删除记录
- delDownload(opt);
- return "ok";
- }
- else
- {
- logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
- return "error";
- }
- });
- ljRetState.code = 2;
- ljRetState.msg = "null object of uid"
- ljRetState.success = true;
- res.setHeader("Access-Control-Allow-Origin", "*");
- res.json(ljRetState);
- return "";
- });
- var addDownload = function (opt)
- {
- var macAddressStr = "";
- mac.getMac(function (err, macAddress)
- {
- if (err) throw err;
- macAddressStr = macAddress; //获取mac地址
- });
- opt.url = apiHelper.getApiForAddDownload(opt.lessonId, opt.courseId, opt.resTotal, opt.resSize);
- 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 + "] with lessonId["+opt.lessonId+"]");
- return true;
- }
- else
- {
- logHelper.error("Failed to call api[" + opt.path + "] with lessonId["+opt.lessonId+"],caused by error[" + retObj + "]");
- return false;
- }
- });
- }
- var delDownload = function (opt)
- {
- opt.url = apiHelper.getApiForDelDownload(opt.lessonId, null);
- opt.method = "delete";
- 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;
|