lesson.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. var express = require('express');
  2. var mac = require('getmac');//获取mac地址
  3. var os = require('os');
  4. var fs = require('fs');
  5. var url = require('url');
  6. var router = express.Router();
  7. var ljConst = require("../common/LjConst");
  8. var ljRetState = require("../common/LjRetState");
  9. var logHelper = require("../helper/LjLogHelper.js");
  10. var httpHelper = require("../helper/LjHttpHelper");
  11. var fileHelper = require("../helper/LjFileHelper");
  12. var apiHelper = require("../helper/LjApiHelper");
  13. var ljStringHelper = require("../helper/LjStringHelper");
  14. /**
  15. * 获取MAC地址
  16. */
  17. router.get("/getMac", function (req, res)
  18. {
  19. var mac = apiHelper.getApiForGetMac();
  20. ljRetState.code = 200;
  21. ljRetState.data = mac;
  22. ljRetState.success = true;
  23. res.setHeader("Access-Control-Allow-Origin", "*");
  24. res.writeHead(200, {'Access-Control-Allow-Origin':'*'});
  25. res.json(ljRetState);
  26. return;
  27. });
  28. /**
  29. * 下载文件
  30. */
  31. router.get("/downloadFile", function (req, res, next)
  32. {
  33. var arg = url.parse(req.url, true).query;
  34. var courseId = arg.courseId;
  35. var eid = arg.eid;
  36. var uid = arg.uid;
  37. var token = arg.token;
  38. var lessonId = arg.lessonId;
  39. //获取参数
  40. logHelper.debug(arg);
  41. //校验参数
  42. if (ljStringHelper.isNull(courseId))
  43. {
  44. logHelper.info("null object of courseId");
  45. ljRetState.code = 2;
  46. ljRetState.msg = "null object of courseId"
  47. res.json(ljRetState);
  48. return;
  49. }
  50. if (ljStringHelper.isNull(lessonId))
  51. {
  52. logHelper.info("null object of lessonId");
  53. ljRetState.code = 2;
  54. ljRetState.msg = "null object of lessonId"
  55. res.json(ljRetState);
  56. return;
  57. }
  58. if (ljStringHelper.isNull(eid))
  59. {
  60. logHelper.info("null object of eid");
  61. ljRetState.code = 2;
  62. ljRetState.msg = "null object of eid"
  63. res.json(ljRetState);
  64. return;
  65. }
  66. if (ljStringHelper.isNull(uid))
  67. {
  68. logHelper.info("null object of uid");
  69. ljRetState.code = 2;
  70. ljRetState.msg = "null object of uid"
  71. res.json(ljRetState);
  72. return;
  73. }
  74. if (ljStringHelper.isNull(token))
  75. {
  76. logHelper.info("null object of token");
  77. ljRetState.code = 2;
  78. ljRetState.msg = "null object of token"
  79. res.json(ljRetState);
  80. return;
  81. }
  82. var opt = {};
  83. opt.eid = eid;
  84. opt.uid = uid;
  85. opt.token = token;
  86. opt.lessonId = lessonId;
  87. opt.courseId = courseId;
  88. opt.path = "/lesson/" + opt.lessonId;
  89. opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid);
  90. opt.method = "get";
  91. httpHelper.request(opt, function (error, res, body)
  92. {
  93. let obj = JSON.parse(body);
  94. var lessonJsonObj = JSON.parse(body);
  95. if (lessonJsonObj.code == 200)
  96. {
  97. /*课件-Begin*/
  98. //课程集合
  99. var courseList = lessonJsonObj.data.list;
  100. //课程数
  101. var courseListSize = courseList.length;
  102. //获取总共资源数
  103. var res_total = 0;
  104. for (var i = 0; i < courseListSize; i++)
  105. {
  106. var courseObj = courseList[i];
  107. if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
  108. {
  109. //资源集合
  110. var resourceList = courseList[i].list;
  111. //资源数
  112. var resourceSize = resourceList.length;
  113. res_total += resourceSize
  114. }
  115. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  116. {
  117. if (courseObj.list.length > 0)
  118. {
  119. var audioObj = courseObj.list[0];
  120. var image = audioObj.img;
  121. var audio = audioObj.audio;
  122. if (image != null && image.url != "")
  123. {
  124. res_total += 1;
  125. }
  126. if (audio != null && audio.url != "")
  127. {
  128. res_total += 1;
  129. }
  130. }
  131. }
  132. }
  133. logHelper.info("res_total:" + res_total);
  134. /*课件-End*/
  135. //添加下载记录
  136. opt.resTotal = res_total;
  137. opt.resSize = 0;
  138. addDownload(opt);
  139. logHelper.info("Begin to download file with lessonId【" + opt.lessonId + "】");
  140. //添加下载记录
  141. //遍历课件
  142. for (var i = 0; i < courseListSize; i++)
  143. {
  144. var courseObj = courseList[i];
  145. if (courseObj.type == ljConst.RES_TYPE_IMAGE || courseObj.type == ljConst.RES_TYPE_VIDEO)
  146. {
  147. //资源集合
  148. var resourceList = courseList[i].list;
  149. //资源数
  150. var resourceSize = resourceList.length;
  151. for (var j = 0; j < resourceSize; j++)
  152. {
  153. logHelper.debug(resourceList[j].url);
  154. var url = resourceList[j].url;
  155. fileHelper.downloadFile(url, opt);
  156. }
  157. }//处理语音
  158. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  159. {
  160. if (courseObj.list.length > 0)
  161. {
  162. var audioObj = courseObj.list[0];
  163. var image = audioObj.img;
  164. var audio = audioObj.audio;
  165. //下载图片
  166. if (image != null && image.url != "")
  167. {
  168. fileHelper.downloadFile(image.url, opt);
  169. }
  170. //下载语音
  171. if (audio != null && audio.url != "")
  172. {
  173. fileHelper.downloadFile(audio.url, opt);
  174. }
  175. }
  176. }
  177. }
  178. /*课件-End*/
  179. return "ok";
  180. }
  181. else
  182. {
  183. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  184. return "error";
  185. }
  186. })
  187. ljRetState.code = 200;
  188. ljRetState.msg = "null object of uid"
  189. ljRetState.success = true;
  190. res.setHeader("Access-Control-Allow-Origin", "*");
  191. res.json(ljRetState);
  192. return;
  193. });
  194. /**
  195. * 读取文件
  196. */
  197. router.get("/readFile", function (req, res)
  198. {
  199. var arg = url.parse(req.url, true).query;
  200. var fileDirStr = arg.n;
  201. var platform = os.platform();
  202. var fileDirNew = "";
  203. if (platform == "win32")
  204. {
  205. fileDirNew = fileDirStr.replace(/\*/g, '\\');
  206. }
  207. else
  208. {
  209. fileDirNew = fileDirStr.replace(/\*/g, '/');
  210. }
  211. var fileDir = fileHelper.getFileDir(fileDirNew)
  212. var fileProp = fileHelper.getFileProperty(fileDir);
  213. console.info(fileDir);
  214. //判断文件是否存在
  215. if (!fs.existsSync(fileDir))
  216. {
  217. ljRetState.code = 1;
  218. ljRetState.msg = "文件没有找到"
  219. ljRetState.success = false;
  220. res.setHeader("Access-Control-Allow-Origin", "*");
  221. res.json(ljRetState);
  222. return false;
  223. }
  224. logHelper.info(fileProp.fileType);
  225. if (fileProp.fileType == ljConst.VIDEO_TYPE_MP4)
  226. {
  227. logHelper.info(fileDir);
  228. fileHelper.readDecryptedStream(res, fileDir, "video/mp4")
  229. }
  230. else
  231. {
  232. fileHelper.readDecryptedStream(res, fileDir, "image/jpeg")
  233. }
  234. });
  235. /**
  236. * 删除下载文件
  237. */
  238. router.get("/delFile", function (req, res)
  239. {
  240. var arg = url.parse(req.url, true).query;
  241. var courseId = arg.courseId;
  242. var eid = arg.eid;
  243. var uid = arg.uid;
  244. var token = arg.token;
  245. var lessonId = arg.lessonId;
  246. //获取参数
  247. logHelper.debug(arg);
  248. //校验参数
  249. if (ljStringHelper.isNull(courseId))
  250. {
  251. logHelper.info("null object of courseId");
  252. ljRetState.code = 2;
  253. ljRetState.msg = "null object of courseId"
  254. res.json(ljRetState);
  255. return;
  256. }
  257. if (ljStringHelper.isNull(lessonId))
  258. {
  259. logHelper.info("null object of lessonId");
  260. ljRetState.code = 2;
  261. ljRetState.msg = "null object of lessonId"
  262. res.json(ljRetState);
  263. return;
  264. }
  265. if (ljStringHelper.isNull(eid))
  266. {
  267. logHelper.info("null object of eid");
  268. ljRetState.code = 2;
  269. ljRetState.msg = "null object of eid"
  270. res.json(ljRetState);
  271. return;
  272. }
  273. if (ljStringHelper.isNull(uid))
  274. {
  275. logHelper.info("null object of uid");
  276. ljRetState.code = 2;
  277. ljRetState.msg = "null object of uid"
  278. res.json(ljRetState);
  279. return;
  280. }
  281. if (ljStringHelper.isNull(token))
  282. {
  283. logHelper.info("null object of token");
  284. ljRetState.code = 2;
  285. ljRetState.msg = "null object of token"
  286. res.json(ljRetState);
  287. return;
  288. }
  289. var opt = {};
  290. opt.eid = eid;
  291. opt.uid = uid;
  292. opt.token = token;
  293. opt.lessonId = lessonId;
  294. opt.courseId = courseId;
  295. opt.path = "/lesson/" + opt.lessonId;
  296. opt.url = apiHelper.getApiForQueryCourse(opt.lessonId, opt.courseId, opt.eid);
  297. opt.method = "get";
  298. httpHelper.request(opt, function (error, res, body)
  299. {
  300. let obj = JSON.parse(body);
  301. var lessonJsonObj = JSON.parse(body);
  302. if (lessonJsonObj.code == 200)
  303. {
  304. /*课件-Begin*/
  305. //课程集合
  306. var courseList = lessonJsonObj.data.list;
  307. //课程数
  308. var courseListSize = courseList.length;
  309. //遍历课件
  310. for (var i = 0; i < courseListSize; i++)
  311. {
  312. var courseObj = courseList[i];
  313. //处理图片删除
  314. if (courseObj.type == ljConst.RES_TYPE_IMAGE)
  315. {
  316. //资源集合
  317. var resourceList = courseObj.list;
  318. //资源数
  319. var resourceSize = resourceList.length;
  320. for (var j = 0; j < resourceSize; j++)
  321. {
  322. var url = resourceList[j].url;
  323. var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
  324. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
  325. //判断文件是否存在
  326. if (fs.existsSync(delFileDir))
  327. {
  328. //删除路径
  329. fs.unlinkSync(delFileDir);
  330. }
  331. }
  332. }//处理视频删除
  333. else if (courseObj.type == ljConst.RES_TYPE_VIDEO)
  334. {
  335. //资源集合
  336. var resourceList = courseObj.list;
  337. //资源数
  338. var resourceSize = resourceList.length;
  339. for (var j = 0; j < resourceSize; j++)
  340. {
  341. var url = resourceList[j].url;
  342. var delFileProp = fileHelper.getFilePropertyNoMkdir(url);
  343. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp4";
  344. //判断文件是否存在
  345. if (fs.existsSync(delFileDir))
  346. {
  347. //删除路径
  348. fs.unlinkSync(delFileDir);
  349. }
  350. }
  351. }
  352. //处理语音删除
  353. else if (courseObj.type == ljConst.RES_TYPE_AUDIO)
  354. {
  355. if (courseObj.list.length > 0)
  356. {
  357. var audioObj = courseObj.list[0];
  358. var image = audioObj.img;
  359. var audio = audioObj.audio;
  360. //删除图片
  361. if (image != null && image.url != "")
  362. {
  363. var delFileProp = fileHelper.getFilePropertyNoMkdir(image.url);
  364. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new." + delFileProp.fileType;
  365. //判断文件是否存在
  366. if (fs.existsSync(delFileDir))
  367. {
  368. //删除路径
  369. fs.unlinkSync(delFileDir);
  370. }
  371. }
  372. //删除语音
  373. if (audio != null && audio.url != "")
  374. {
  375. var delFileProp = fileHelper.getFilePropertyNoMkdir(audio.url);
  376. var delFileDir = delFileProp.folderDir + delFileProp.fileName + "_new.mp3";
  377. //判断文件是否存在
  378. if (fs.existsSync(delFileDir))
  379. {
  380. //删除路径
  381. fs.unlinkSync(delFileDir);
  382. }
  383. }
  384. }
  385. }
  386. }
  387. /*课件-End*/
  388. //删除记录
  389. delDownload(opt);
  390. return "ok";
  391. }
  392. else
  393. {
  394. logHelper.error("failed to query lesson with lessonId[" + opt.lessonId + "],caused by error:" + lessonJsonObj);
  395. return "error";
  396. }
  397. });
  398. ljRetState.code = 2;
  399. ljRetState.msg = "null object of uid"
  400. ljRetState.success = true;
  401. res.setHeader("Access-Control-Allow-Origin", "*");
  402. res.json(ljRetState);
  403. return "";
  404. });
  405. var addDownload = function (opt)
  406. {
  407. var macAddressStr = "";
  408. mac.getMac(function (err, macAddress)
  409. {
  410. if (err) throw err;
  411. macAddressStr = macAddress; //获取mac地址
  412. });
  413. opt.url = apiHelper.getApiForAddDownload(opt.lessonId, opt.courseId, opt.resTotal, opt.resSize);
  414. opt.method = "post";
  415. opt.path = "/callback/download/add";
  416. httpHelper.request(opt, function (error, res, body)
  417. {
  418. var retObj = JSON.parse(body);
  419. logHelper.debug(retObj);
  420. if (retObj.code == 200)
  421. {
  422. logHelper.info("Succeed to call api[" + opt.path + "] with lessonId["+opt.lessonId+"]");
  423. return true;
  424. }
  425. else
  426. {
  427. logHelper.error("Failed to call api[" + opt.path + "] with lessonId["+opt.lessonId+"],caused by error[" + retObj + "]");
  428. return false;
  429. }
  430. });
  431. }
  432. var delDownload = function (opt)
  433. {
  434. opt.url = apiHelper.getApiForDelDownload(opt.lessonId, null);
  435. opt.method = "delete";
  436. opt.path = "/callback/download/delete";
  437. httpHelper.request(opt, function (error, res, body)
  438. {
  439. var retObj = JSON.parse(body);
  440. logHelper.debug(retObj);
  441. if (retObj.code == 200)
  442. {
  443. logHelper.info("Succeed to call api[" + opt.path + "]");
  444. return true;
  445. }
  446. else
  447. {
  448. logHelper.error("Failed to call api[" + opt.path + "],caused by error[" + retObj + "]");
  449. return false;
  450. }
  451. });
  452. }
  453. module.exports = router;