lesson.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. var express = require('express');
  2. var url = require('url');
  3. var router = express.Router();
  4. var ljConst = require("../common/LjConst");
  5. var ljRetState = require("../common/LjRetState");
  6. var logHelper = require("../helper/LjLogHelper.js");
  7. var httpHelper = require("../helper/LjHttpHelper");
  8. var fileHelper = require("../helper/LjFileHelper");
  9. var apiHelper = require("../helper/LjApiHelper");
  10. var ljStringHelper = require("../helper/LjStringHelper");
  11. /**
  12. * 下载文件
  13. */
  14. router.get("/downloadFile",function(req,res,next)
  15. {
  16. var arg = url.parse(req.url, true).query;
  17. var courseId = arg.courseId;
  18. var eid = arg.eid;
  19. var uid = arg.uid;
  20. var token = arg.token;
  21. var lessonId = arg.lessonId;
  22. //获取参数
  23. logHelper.debug(arg);
  24. //校验参数
  25. if (ljStringHelper.isNull(courseId))
  26. {
  27. logHelper.info("null object of courseId");
  28. ljRetState.code = 2;
  29. ljRetState.msg ="null object of courseId"
  30. res.json(ljRetState);
  31. return;
  32. }
  33. if (ljStringHelper.isNull(lessonId))
  34. {
  35. logHelper.info("null object of lessonId");
  36. ljRetState.code = 2;
  37. ljRetState.msg ="null object of lessonId"
  38. res.json(ljRetState);
  39. return;
  40. }
  41. if (ljStringHelper.isNull(eid))
  42. {
  43. logHelper.info("null object of eid");
  44. ljRetState.code = 2;
  45. ljRetState.msg ="null object of eid"
  46. res.json(ljRetState);
  47. return;
  48. }
  49. if (ljStringHelper.isNull(uid))
  50. {
  51. logHelper.info("null object of uid");
  52. ljRetState.code = 2;
  53. ljRetState.msg ="null object of uid"
  54. res.json(ljRetState);
  55. return;
  56. }
  57. if (ljStringHelper.isNull(token))
  58. {
  59. logHelper.info("null object of token");
  60. ljRetState.code = 2;
  61. ljRetState.msg ="null object of token"
  62. res.json(ljRetState);
  63. return;
  64. }
  65. var opt = {};
  66. opt.eid = eid;
  67. opt.uid = uid;
  68. opt.token = token;
  69. opt.lessonId = lessonId;
  70. opt.courseId = courseId;
  71. opt.path = "/lesson/" + opt.lessonId;
  72. opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid);
  73. opt.method = "get";
  74. httpHelper.request(opt, function (error, res, body)
  75. {
  76. let obj = JSON.parse(body);
  77. var lessonJsonObj = JSON.parse(body);
  78. if (lessonJsonObj.code == 200)
  79. {
  80. /*课件-Begin*/
  81. //课程集合
  82. var courseList = lessonJsonObj.data.list;
  83. //课程数
  84. var courseListSize = courseList.length;
  85. //遍历课件
  86. for (var i = 0; i < courseListSize; i++)
  87. {
  88. var lessonware = courseList[i];
  89. if(lessonware.type == ljConst.RES_TYPE_IMAGE || lessonware.type == ljConst.RES_TYPE_VIDEO){
  90. //资源集合
  91. var resourceList = lessonware.list;
  92. //资源数
  93. var resourceSize = resourceList.length;
  94. for (var j = 0; j < resourceSize; j++)
  95. {
  96. logHelper.debug(resourceList[j].url);
  97. var url = resourceList[j].url;
  98. fileHelper.downloadFile(url)
  99. }
  100. }else if(lessonware.type == ljConst.RES_TYPE_AUDIO){
  101. //有声资源集合
  102. var audioResourceList = lessonware.list;
  103. //资源数
  104. var resourceSize = audioResourceList.length;
  105. for (var j = 0; j < resourceSize; j++)
  106. {
  107. var img = audioResourceList[j].img;
  108. var audio = audioResourceList[j].audio;
  109. logHelper.debug(img);
  110. logHelper.debug(audio);
  111. var imgUrl = img.url;
  112. var audioUrl = audio.url;
  113. if(ljStringHelper.isNonNull(imgUrl)){
  114. fileHelper.downloadFile(imgUrl);
  115. }
  116. if(ljStringHelper.isNonNull(audioUrl)){
  117. fileHelper.downloadFile(audioUrl);
  118. }
  119. }
  120. }
  121. }
  122. /*课件-End*/
  123. //保存记录
  124. //addDownload(opt)
  125. return "ok";
  126. }
  127. else
  128. {
  129. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  130. return "error";
  131. }
  132. }).on("end",function()
  133. {
  134. test();
  135. });
  136. });
  137. /**
  138. * 读取文件
  139. */
  140. router.get("/readFile",function(req,res)
  141. {
  142. var arg = url.parse(req.url, true).query;
  143. var fileDirStr = arg.n;
  144. var fileDirNew = fileDirStr.replace(/\*/g,'\\');
  145. var fileDir = fileHelper.getFileDir(fileDirNew)
  146. var fileProp = fileHelper.getFileProperty(fileDir);
  147. if(fileProp.fileType == ljConst.VIDEO_TYPE_M3U8)
  148. {
  149. fileHelper.readDecryptedStream(res,fileDir,"video/mp4")
  150. }
  151. else
  152. {
  153. fileHelper.readDecryptedStream(res,fileDir,"image/jpeg")
  154. }
  155. });
  156. function test()
  157. {
  158. console.log(11);
  159. }
  160. /**
  161. * 删除下载文件
  162. */
  163. router.get("/delFile",function(req,res)
  164. {
  165. var arg = url.parse(req.url, true).query;
  166. var courseId = arg.courseId;
  167. var eid = arg.eid;
  168. var uid = arg.uid;
  169. var token = arg.token;
  170. var lessonId = arg.lessonId;
  171. //获取参数
  172. logHelper.debug(arg);
  173. //校验参数
  174. if (ljStringHelper.isNull(courseId))
  175. {
  176. logHelper.info("null object of courseId");
  177. return "null object of courseId";
  178. }
  179. if (ljStringHelper.isNull(lessonId))
  180. {
  181. logHelper.info("null object of lessonId");
  182. return "null object of courseId";
  183. }
  184. if (ljStringHelper.isNull(eid))
  185. {
  186. logHelper.info("null object of eid");
  187. return "null object of eid";
  188. }
  189. if (ljStringHelper.isNull(uid))
  190. {
  191. logHelper.info("null object of uid");
  192. return "null object of uid";
  193. }
  194. if (ljStringHelper.isNull(token))
  195. {
  196. logHelper.info("null object of token");
  197. return "null object of token";
  198. }
  199. var opt = {};
  200. opt.eid = eid;
  201. opt.uid = uid;
  202. opt.token = token;
  203. opt.lessonId = lessonId;
  204. opt.courseId = courseId;
  205. opt.path = "/lesson/" + opt.lessonId;
  206. opt.url = apiHelper.getApiForCourse(opt.lessonId, opt.courseId, opt.eid);
  207. opt.method = "get";
  208. httpHelper.request(opt, function (error, res, body)
  209. {
  210. let obj = JSON.parse(body);
  211. var lessonJsonObj = JSON.parse(body);
  212. if (lessonJsonObj.code == 200)
  213. {
  214. /*课件-Begin*/
  215. //课程集合
  216. var courseList = lessonJsonObj.data.list;
  217. //课程数
  218. var courseListSize = courseList.length;
  219. //遍历课件
  220. for (var i = 0; i < courseListSize; i++)
  221. {
  222. //资源集合
  223. var resourceList = courseList[i].list;
  224. //资源数
  225. var resourceSize = resourceList.length;
  226. for (var j = 0; j < resourceSize; j++)
  227. {
  228. logHelper.debug(resourceList[j].url);
  229. var url = resourceList[j].url;
  230. fileHelper.downloadFile(url)
  231. }
  232. }
  233. /*课件-End*/
  234. //保存记录
  235. process.nextTick(addDownload);
  236. return "ok";
  237. }
  238. else
  239. {
  240. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  241. return "error";
  242. }
  243. });
  244. });
  245. var addDownload = function(opt)
  246. {
  247. opt.url = apiHelper.getApiForAddDownload(opt.lessonId,opt.courseId, null);
  248. opt.method = "post";
  249. opt.path = "/callback/download/add";
  250. httpHelper.request(opt, function (error, res, body)
  251. {
  252. var retObj = JSON.parse(body);
  253. logHelper.debug(retObj);
  254. if (retObj.code == 200)
  255. {
  256. logHelper.info("Succeed to call api[" + opt.path + "]");
  257. return true;
  258. }
  259. else
  260. {
  261. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]");
  262. return false;
  263. }
  264. });
  265. }
  266. var delDownload = function(opt)
  267. {
  268. opt.url = apiHelper.getApiForDelDownload(opt.lessonId, null);
  269. opt.method = "post";
  270. opt.path = "/callback/download/delete";
  271. httpHelper.request(opt, function (error, res, body)
  272. {
  273. var retObj = JSON.parse(body);
  274. logHelper.debug(retObj);
  275. if (retObj.code == 200)
  276. {
  277. logHelper.info("Succeed to call api[" + opt.path + "]");
  278. return true;
  279. }
  280. else
  281. {
  282. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]");
  283. return false;
  284. }
  285. });
  286. }
  287. module.exports = router;